Android Studio 生成Jar并混淆
jopen
9年前
初探:
android studio生成Jar.
android studio 自动生成aar.
1.android studio 自动生成.jar, 首先将工程作为依赖库,build即可。
2.手动打包jar
1> 仅打包class文件
task makeJar(type: Jar, dependsOn:['build', 'targetJar']) { from file('build/intermediates/classes/release') //目标文件名称 archiveName = 'un_proguard.jar' destinationDir = file('build/libs') exclude "lbs/tmplibary/BuildConfig.class" exclude "lbs/tmplibary/BuildConfig\$*.class" exclude "**/R.class" exclude "**/R\$*.class" //只导入资源路径集下的部分资源 include "lbs/**/*.class" }
2>添加assets文件
(1)直接includes assets文件即可
task makeJar(type: Jar, dependsOn:['build', 'targetJar']) { from file('build/intermediates/classes/release') from fileTree(dir: 'src/main', includes: ['assets/**']) //目标文件名称 archiveName = 'un_proguard.jar' destinationDir = file('build/libs') exclude "lbs/tmplibary/BuildConfig.class" exclude "lbs/tmplibary/BuildConfig\$*.class" exclude "**/R.class" exclude "**/R\$*.class" //只导入资源路径集下的部分资源 include "lbs/**/*.class" include "assets/**" }
(2)使用zipTree
将所需打包的assets文件打包成.zip,如下:
def zipFile = "src/main/assets.zip" task makeJar(type: Jar, dependsOn:['build', 'targetJar']) { from zipTree(zipFile) from file('build/intermediates/classes/release') //目标文件名称 archiveName = 'un_proguard.jar' destinationDir = file('build/libs') exclude "lbs/tmplibary/BuildConfig.class" exclude "lbs/tmplibary/BuildConfig\$*.class" exclude "**/R.class" exclude "**/R\$*.class" //只导入资源路径集下的部分资源 include "lbs/**/*.class" }
3.混淆
task proguard(type: proguard.gradle.ProGuardTask, dependsOn: makeJar) { // 输入路径 injars "build/libs/un_proguard.jar" // 输出路径 outjars 'libs/app_proguard.jar' // 添加配置信息 configuration 'proguard-rules.pro' }
proguard-rules.pro需要配置一下路径
-libraryjars <java.home>/lib/rt.jar -libraryjars xxxxx/android/sdk/platforms/android-22/android.jar
注:代码编译出错的情况下 添加
-dontpreverify
尽可能的时候使用
-dontwarn
忽略一些不必要的警告,以便成功打包。
完结,~_~
来自: http://my.oschina.net/u/2531612/blog/591366?fromerr=Xd1Kd7dY