见上图:
1.build types-minifyenabled=true
2 .添加以下代码
taskmakejar(type:copy ) {
delete 'build/libs/myjar.jar' //删除存在的
from (build/intermediates/AAR _ main _ jar/release/' ) /设置复制的文件
打入Into('build/libs/' ) jar包后的文件目录
将include(classes.jar ) classes.jar放入build/libs/目录下的//include,exclude参数中以设置过滤
//(我们只关心名为classes.jar的文件)
rename('classes.jar ',' myjar.jar ' )//重命名
}
makejar.Dependson(Build ) )。
3.proguard-android.txt的说明
在学习定制proguard-rules.pro之前,请先查看系统的缺省代码混淆规则proguard-android.txt文件。 如果在proguard-rules.pro文件中没有设置模糊化规则,则根据proguard-android.txt中的规则打开模糊化
#不使用混合大小写的类名进行混淆
-dontusemixedcaseclassnames
不跳过library中的非公共类
- dontskipnonpubliclibraryclasses
#打印混淆详细信息-verbose
# optimizationisturnedoffbydefault.dexdoesnotlikecoderun # throughtheproguardoptimizeandpreverifysteps (andperformssome # ome )
#不优化
- dont最佳化
#因为不进行事前检查,所以可以加快混淆
- dont preverify # notethatifyouwanttoenableoptimization,youcannotjust # includeoptimizationflagsinyourownprojectconfiguration
# insteadyouwillneedtopointtothe
# ' proguard-Android-optimize.txt ' fileinsteadofthisonefromyour # project.properties file。
#不混淆注释参数-keepattributes *Annotation*
#不混淆特定类- keeppublicclasscom.Google.vending.licensingservice-keeppublicclasscom.Android.vending
# For native methods,see http://proguard.source forge.net/manual/examples.html
不要将包含#native#native方法的类与native方法混淆
- keepclasseswithmembernamesclass * { native; }
# keepsettersinviewssothatanimationscanstillwork。
# see http://proguard.source forge.net/manual/examples.html # beans
#避免混淆继承的View类的getXX (和setXX )方法,以确保属性动画正常工作。
- keepclassmemberspublicclass * extends Android.view.view { voidset * *; *** get* (; }
# wewanttokeepmethodsinactivitythatcouldbeusedinthexmlattributeonclick
避免混淆包含由Activity公共void限定的View参数的方法,以便xml配置的onclick正常工作- keepclassmembersclass * extends Android.app .
# For enumeration classes,see http://proguard.source forge.net/manual/examples.html # enumerations
不要混淆枚举类的values (和valueOf )方法
- keepclassmembersenum * { public static * * [ ] values (; public static * * value of (Java.lang.string ); }
不要混淆实现可编程接口的类的CREATOR成员
- keepclassmembersclass * implements Android.OS.parcelable { publicstaticfinalandroid.OS.parcelable $ creator creator; }
不要混淆r文件中的所有公共类成员,如android.R类
- keepclassmembersclass *.r $ * { public static; }
# thesupportlibrarycontainsreferencestonewerplatformversions.# don ' twarnaboutthoseincasethisappislinkingagainstanolder
# platform version.weknowaboutthem,and they are safe。
不警告android.support包下的代码。 例如,如果app使用的support包版本比某些类旧,则会出现警告问题。
- dontwarnandroid.support.* * # understandthe @ keepsupportannotation。
不混淆Keep注释- keepclassandroid.support.annotation.keep
不要混淆带Keep注释的类
- keep @ Android.support.annotation.keep class * { *; }
如果#类的方法使用Keep注释,请不要将类和类方法混淆
- keepclasseswithmembersclass * { @ Android.support.annotation.keep; }
#当类的属性使用Keep注释时,不要混淆类和类成员
- keepclasseswithmembersclass * { @ Android.support.annotation.keep; }
#如果在类的生成方法中使用Keep注释,则不要混淆类和类成员
- keepclasseswithmembersclass * { @ Android.support.annotation.keep (. ); )。