打包
build工程苟径,確保Module/build/intermediates/class/{debug | release}/中生成了 .class文件在Module Gradle中添加task
task jarMyLib(type: Jar, dependsOn: ['build']) {
archiveName = 'my-lib.jar'
from('build/intermediates/classes/release')
destinationDir = file('build/libs')
exclude('com/XXX/XXX/BuildConfig.class')
exclude('com/XXX/XXX/BuildConfig\$*.class')
exclude('**/R.class')
exclude('**/R\$*.class')
include('com/XXX/XXX/*.class')
include('com/XXX/XXX/*/*.class')
}
- 打開Terminal,輸入
gradlew jarMyLib
,將生成my-lib.jar
混淆
- 引入
@Keep
注解
dependencies {
compile 'com.android.support:support-annotations:23.3.0'
}
代碼使用
@Keep
注解需要保留的類、方法、成員變量移怯、內(nèi)部類等編輯
proguard-rules.pro
文件,添加混淆規(guī)則
#不跳過非公共類
-dontskipnonpubliclibraryclasses
#不跳過非公共類的成員
-dontskipnonpubliclibraryclassmembers
#打印配置文件
-printconfiguration
#保留@Keep注解
-keep,allowobfuscation @interface android.support.annotation.Keep
-keep @android.support.annotation.Keep class *
-dontwarn android.support.annotation.Keep
-keepclassmembers class * {
@android.support.annotation.Keep *;
}
#保留內(nèi)部類这难,異常舟误,簽名
-keepattributes Exceptions,InnerClasses,Signature
#保留方法參數(shù)名
-keepparameternames
#保留注解
-keepattributes *Annotation*
#不裁剪多余代
-dontshrink
#不優(yōu)化代碼
-dontoptimize
#輸出所有消息
-verbose
- 在Module Gradle中添加task
def androidSDKDir = plugins.getPlugin('com.android.library').sdkHandler.getSdkFolder()
def androidJarDir = androidSDKDir.toString() + '/platforms/' + "${android.compileSdkVersion}" + '/android.jar'
task proguardTask(type: ProGuardTask, dependsOn: ['jarMyLib']) {
// 未混淆的jar
injars('build/libs/my-lib.jar')
// 混淆后的jar路徑
outjars('build/libs/progard.jar')
libraryjars(androidJarDir)
// 具體需要keep住的類
configuration 'proguard-rules.pro'
}
- 打開Terminal,輸入
gradlew proguardTask
姻乓,將自動生成混淆后的progard.jar