Android Studio playing jar package has always been a hassle, follow the online existing tutorial, packaging a confusing jar needs to complete the following steps:
1. Modify the plugin to build the AAR after the library and extract the Classes.jar from the AAR
2. Use tools such as Jarjar to remove excess class
3. Confusing the jar obtained in the second step
No matter what step, do a lot of work. Personally, it was rather troublesome, so I spent some time researching the next Gradle to hit the Jar.
Code
Don't say much, first on the code (note: only Gradle Android Plugin 1.2.3 tested)
Build.gradle
Import Com.android.build.gradle.AppPlugin
Import Proguard.gradle.ProGuardTask
Apply plugin: ' Com.android.application '
Android {
Compilesdkversion 22
Buildtoolsversion "22.0.1"
Defaultconfig {
ApplicationID "Org.chaos.demo.jar"
Minsdkversion 22
Targetsdkversion 22
Versioncode 1
Versionname "1.0"
}
Buildtypes {
Release {
minifyenabled true
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '
}
}
}
dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
}
DependsOn can be added or changed according to actual needs
Task Buildjar (dependsOn: [' Compilereleasejava '], Type:jar) {
Appendix = "Demo"
BaseName = "Androidjar"
Version = "1.0.0"
classifier = "Release"
Suffix name
Extension = "jar"
The final Jar package name, if not set, defaults to [Basename]-[appendix]-[version]-[classifier]. [Extension]
Archivename = "Androidjardemo.jar"
The path set where the resource to be packaged is located
def srcclassdir = [Project.buildDir.absolutePath + "/intermediates/classes/release"];
Initializing the resource path set
From Srcclassdir
Remove resources from the lower part of the path set
Exclude "Org/chaos/demo/jar/mainactivity.class"
Exclude "org/chaos/demo/jar/mainactivity$*.class"
Exclude "Org/chaos/demo/jar/buildconfig.class"
Exclude "Org/chaos/demo/jar/buildconfig$*.class"
Exclude "**/r.class"
Exclude "*/r$. Class"
Import only some of the resources under the resource path set
Include "org/chaos/demo/jar/*/. Class"
Note: Exclude include supports variable length parameters
}
Task Proguardjar (dependsOn: [' Buildjar '], type:proguardtask) {
Android default Proguard file
Configuration android.getdefaultproguardfile (' Proguard-android.txt ')
The Jar is confused based on the file, note that the component that needs to be registered in manifest is also included in the file
Configuration ' Proguard-rules.pro '
String Injar = BuildJar.archivePath.getAbsolutePath ()
Input jar
Injars Injar
Output jar
Outjars injar.substring (0, Injar.lastindexof ('/')) + "/proguard-${buildjar.archivename}"
Setting does not delete unreferenced resources (classes, methods, etc.)
Dontshrink
Appplugin Appplugin = Getplugins (). Findplugin (Appplugin)
if (appplugin! = null) {
List runtimejarlist
if (Appplugin.getmetaclass (). Getmetamethod ("Getruntimejarlist")) {
Runtimejarlist = Appplugin.getruntimejarlist ()
} else if (Android.getmetaclass (). Getmetamethod ("Getbootclasspath")) {
Runtimejarlist = Android.getbootclasspath ()
} else {
Runtimejarlist = Appplugin.getbootclasspath ()
}
for (String runtimejar:runtimejarlist) {
Add Runtime to Proguard
Libraryjars (Runtimejar)
}
}
}
Why do components that have been registered in manifest need to declare the corresponding obfuscation rules in the. Pro file?
It may be noted that the second line of Proguardjar task comments, during the packaging process of the APK, AAPT in parsing manifest into a rule file that does not confuse the registered components in manifest. The code for configuring the above AAPT generated rule files in Gradle Android Plugin is as follows:
Baseplugin.groovy
Protected File createproguardtasks (@NonNull basevariantdata VariantData,
@Nullable Basevariantdata testedvariantdata) {
......
Also the config file output by aapt
Proguardtask.configuration (VariantData.processResourcesTask.proguardOutputFile)
......
}
Due to personal ability reasons, get not processresourcestask instances, so at present can only add the corresponding components to the rule file, but also hope to know how to get a friend to share under, thank you.
How to use
Run the command without confusion
Gradle Buildjar
Or
./gradlew Buildjar
Run if you need to confuse
Gradle Proguardjar
Or
./gradlew Proguardjar
At last
Buildjar This part is relatively simple, many content online has the tutorial. The key is to confuse, because each team has their own installation habits, JDK, Android SDK path is not necessarily consistent, and can not directly write dead runtime path, finally directly to see the Android Plugin source to write the Proguardjar task.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Studio uses Gradle to package jars