参考上图:
1.buildTypes -> minifyEnabled = true
2.添加如下代码
task makeJar(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中非public的类
-dontskipnonpubliclibraryclasses
#打印混淆的详细信息-verbose
# Optimization is turned off by default. Dex does not like code run# through the ProGuard optimize and preverify steps (and performs some# of these optimizations on its own).
#不进行优化
-dontoptimize
#不进行预校验,能加快混淆速度
-dontpreverify# Note that if you want to enable optimization, you cannot just# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your# project.properties file.
#不混淆注解中的参数-keepattributes *Annotation*
#不混淆特定的类-keep public classcom.google.vending.licensing.ILicensingService-keep public classcom.android.vending.licensing.ILicensingService
# For native methods, see http://proguard.sourceforge.net/manual/examples.html
#native#不混淆包含native方法的类和native方法
-keepclasseswithmembernames class * { native ;}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
#不混淆继承View类的getXX()和setXX()方法以保证属性动画正常运行。
-keepclassmembers public class * extends android.view.View{ voidset*(***);*** get*();}
# We want to keep methods in Activity that could be used in the XML attribute onClick
#不混淆Activity中public void修饰的含有View参数的方法以保证在xml配置的onclick正常使用-keepclassmembers class * extends android.app.Activity{ public void *(android.view.View);}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
#不混淆枚举类的values()和valueOf()方法
-keepclassmembers enum * { public static **[] values();public static ** valueOf(java.lang.String);}
#不混淆实现Parcelable接口的类的CREATOR成员
-keepclassmembers class * implements android.os.Parcelable{ public static final android.os.Parcelable$Creator CREATOR;}
#不混淆R文件的所有public类成员,如android.R类
-keepclassmembers class **.R$* { public static ;}
# The support library contains references to newer platform versions.# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
#不对android.support包下的代码警告。例如,app使用了低于某些类的support包版本导致出现警告问题。
-dontwarn android.support.**# Understand the @Keep support annotation.
#不混淆Keep注解-keep class android.support.annotation.Keep
#不混淆带Keep注解的类
-keep @android.support.annotation.Keepclass * {*;}
#如果类的方法使用了Keep注解,不混淆类与类方法
-keepclasseswithmembers class * { @android.support.annotation.Keep;}
#如果类的属性使用了Keep注解,不混淆类与类成员
-keepclasseswithmembers class * { @android.support.annotation.Keep;}
#如果类的构造方法使用了Keep注解,不混淆类与类成员
-keepclasseswithmembers class * { @android.support.annotation.Keep(...);}。