ProGuard 是什么?
可以把 ProGuard 理解为是对代码和资源压缩的一个工具,它能够提供对 Java 类文件的压缩、优化、混淆,和预校验。压缩的步骤是检测并移除未使用的类、字段、方法和属性。优化的步骤是分析和优化方法的字节码。混淆的步骤是使用短的毫无意义的名称重命名剩余的类、字段和方法。压缩、优化、混淆使得代码更小,更高效。
AndroidStudio 怎么使用 ProGuard ?
代码压缩
要通过 ProGuard 启用代码压缩,请在 build.gradle
文件内相应的构建类型中添加 minifyEnabled true
。
android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
ProGuard 会移除所有 (并且只会移除) 未使用的代码。不过 , ProGuard 难以对许多情况进行正确分析,可能会移除应用真正需要的代码。比如需要反射、动态加载所引用的类等情况,可能因为ProGuard 移除或者混淆了这部分没使用的类,而导致错误。所以有时需要编写混淆优化配置文件。在 gradle 中的 proguardFiles 能够让我们传递File文件或者文件路径交给 proguard 来执行。
配置 ProGuard 规则
现在我们开启了混淆,但是还没有配置混淆,我们可以在 build/intermediates/proguard-files/proguard-defaults.txt
来查看默认的配置,现在我们可以根据默认的配置来进行我们项目的配置。
-
分析默认配置文件
proguard-defaults.txt-3.4.0
# This is a configuration file for ProGuard. # http://proguard.sourceforge.net/index.html#manual/usage.html # # Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with # the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and # will be ignored by new version of the Android plugin for Gradle. # Optimizations can be turned on and off in the 'postProcessing' DSL block. # The configuration below is applied if optimizations are enabled. # Adding optimization introduces certain risks, since for example not all optimizations performed by # ProGuard works on all versions of Dalvik. The following flags turn off various optimizations # known to have issues, but the list may not be complete or up to date. (The "arithmetic" # optimization can be used if you are only targeting Android 2.0 or later.) Make sure you test # thoroughly if you go this route. ####################### START ####################### # 混淆时所采用的算法(谷歌推荐算法) -optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* # 指定代码的压缩级别(在0~7之间,默认为5) -optimizationpasses 5 # 提高优化步骤 -allowaccessmodification # 包名不混合大小写 -dontusemixedcaseclassnames # 不忽略非公共的库类 -dontskipnonpubliclibraryclasses # 输出混淆日志 -verbose # 保持 Google 原生服务需要的类不被混淆 -keep public class com.google.vending.licensing.ILicensingService -keep public class com.android.vending.licensing.ILicensingService -keep public class com.google.android.vending.licensing.ILicensingService -dontnote com.android.vending.licensing.ILicensingService -dontnote com.google.vending.licensing.ILicensingService -dontnote com.google.android.vending.licensing.ILicensingService # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native # 混淆注意事项第二条,保持 native 方法不被混淆 -keepclasseswithmembernames class * { native <methods>; } # Keep setters in Views so that animations can still work. # 保留自定义控件(继承自View)不被混淆 -keepclassmembers public class * extends android.view.View { void set*(***); *** get*(); } # We want to keep methods in Activity that could be used in the XML attribute onClick. # 保留在 Activity 中的方法参数是 view 的方法(避免布局文件里面 onClick 被影响) -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations # 保持枚举 enum 类不被混淆 -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } # 保持 Parcelable 序列化的类不被混淆(注:aidl 文件不能去混淆) -keepclassmembers class * implements android.os.Parcelable { public static final ** CREATOR; } # 保持R(资源)下的所有类及其方法不能被混淆 -keepclassmembers class **.R$* { public static <fields>; } # Preserve annotated Javascript interface methods. -keepclassmembers class * { @android.webkit.JavascriptInterface <methods>; } # 支持库包含对较新版本版本的引用。 # 不要警告那些情况下,这个应用程序链接到旧的 # 平台版本。我们知道他们是安全的。 -dontnote android.support.** -dontnote androidx.** -dontwarn android.support.** -dontwarn androidx.** # 此类已弃用,但仍保留向后兼容性。 -dontwarn android.util.FloatMath # Support包规则 # Understand the @Keep support annotation. -keep class android.support.annotation.Keep -keep class androidx.annotation.Keep -keep @android.support.annotation.Keep class * {*;} -keep @androidx.annotation.Keep class * {*;} # 保持 support Keep 类成员不被混淆 -keepclasseswithmembers class * { @android.support.annotation.Keep <methods>; } # 保持 androidx Keep 类成员不被混淆 -keepclasseswithmembers class * { @androidx.annotation.Keep <methods>; } # 保持 support Keep 类成员不被混淆 -keepclasseswithmembers class * { @android.support.annotation.Keep <fields>; } # 保持 androidx Keep 类成员不被混淆 -keepclasseswithmembers class * { @androidx.annotation.Keep <fields>; } # 不混淆所有类及其类成员中的使用注解的初始化方法 -keepclasseswithmembers class * { @android.support.annotation.Keep <init>(...); } # 不混淆所有类及其类成员中的使用注解的初始化方法 -keepclasseswithmembers class * { @androidx.annotation.Keep <init>(...); } # 排除 android.jar 和 org.apache.http.legacy.jar 之间重复 -dontnote org.apache.http.** -dontnote android.net.http.** # 排除 android.jar 和核心-lambda-stubs.jar 之间重复。 -dontnote java.lang.invoke.**
以上就是默认配置文件那么这样生成出来的 APK