Add project specific特定的 ProGuard rules here.
You can control the set of applied应用的 configuration files using the proguardFiles setting in build.gradle.
For more details, see http://developer.android.com/guide/developing/tools/proguard.html
If your project uses WebView with JS, uncomment取消注释 the following and specify指定 the fully qualified class name to the javascript interface class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Uncomment this to preserve the line number information for debugging stack traces 保留调试堆栈跟踪的行号信息.
#-keepattributes SourceFile,LineNumberTable
If you keep the line number information, uncomment this to hide the original source file name.
#-renamesourcefileattribute SourceFile
在 Google Play 上发布应用时,您可以上传每个 APK 版本的mapping.txt文件。Google Play 将根据用户报告的问题对收到的堆叠追踪进行去混淆处理,以便您在 Google Play Developer Console 中进行检查。如需了解详细信息,请参阅帮助中心有关如何 对崩溃堆叠追踪进行去混淆处理的文章。
要自行将混淆过的堆叠追踪转换成可读的堆叠追踪,请使用retrace脚本(在 Windows 上为retrace.bat;在 Mac/Linux 上为retrace.sh)。它位于<sdk-root>/tools/proguard/bin目录中。
指定要舍弃的资源可能看似愚蠢,因为您本可将它们删除,但在使用 build variants 时,这样做可能很有用。例如,如果您明知给定资源表面上会在代码中使用[appears to be used in code](并因此不会被压缩器移除),但实际不会用于给定构建变体,就可以将所有资源放入公用项目目录,然后为每个构建变体创建一个不同的 keep.xml文件。It‘s also possible that 构建工具无法根据需要正确识别资源,这是因为编译器会添加内联资源 ID[adds the resource IDs inline],而资源分析器可能不知道真正引用的资源和恰巧具有相同值的代码中的整数值之间的差别。
启用严格引用检查
正常情况下,资源压缩器可准确判定系统是否使用了资源。不过,如果您的代码调用 Resources.getIdentifier(),或您的任何库进行了这一调用,例如 AppCompat 库会执行该调用,这就表示您的代码将根据动态生成的字符串查询资源名称。当您执行这一调用时,默认情况下资源压缩器会采取防御性行为[behaves defensively],将所有具有匹配名称格式[with a matching name format]的资源标记为可能已使用[potentially used],无法移除。
例如,以下代码会使所有带img_前缀的资源标记为已使用。
String name = String.format("img_%1d", angle + 1);
int res = getResources().getIdentifier(name, "drawable", getPackageName());
资源压缩器还会浏览代码以及各种res/raw/资源中的所有字符串常量[looks through all the string constants in your code],寻找格式类似于[looking for resource URLs in a format similar to]file:///android_res/drawable//ic_plus_anim_016.png的资源网址。如果它找到与其类似的字符串,或找到其他看似可用来构建与其类似的网址的字符串,则不会将它们移除。
例如,如果您使用的库包含语言资源(例如使用的是 AppCompat 或 Google Play 服务),则 APK 将包括这些库中消息的所有已翻译语言字符串,无论应用的其余部分是否翻译为同一语言。如果您想只保留应用正式支持的语言,则可以利用resConfig属性指定这些语言。系统会移除未指定语言的所有资源。
Similarly, you can customize自定义 which screen density or ABI resources to include in your APK by building multiple APKs that each target a different device configuration.
合并重复资源
Merge duplicate resources
默认情况下,Gradle 还会合并同名资源[merges identically named resources],例如可能位于不同资源文件夹中的同名可绘制对象。这一行为不受shrinkResources属性控制,也无法停用,因为在有多个资源匹配代码查询的名称时,有必要利用这一行为来避免错误。
如果完全相同的资源出现在同一 source set 中,Gradle 无法合并它们,并且会发出资源合并错误。如果您在build.gradle文件的sourceSet属性中定义了多个源集,则可能会发生这种情况,例如,如果src/main/res/和src/main/res2/包含完全相同的资源,就可能会发生这种情况。
排查资源压缩问题
当您压缩资源时,Gradle Console 会显示它从应用软件包中移除的资源的摘要。例如:
:android:shrinkDebugResources
Removed unused resources: Binary resource data reduced from 2570KB to 1711KB: Removed 33%
:android:validateDebugSigning
如果您使用的不是严格检查,则存在看似可用于为动态加载资源构建资源名称的字符串常量时,可将资源 ID 标记为可访问。在这种情况下,如果您在构建输出中搜索资源名称,可能会找到类似下面这样的消息:
10:32:50.590 [QUIET] [system.out] Marking drawable:ic_plus_anim_016:2130837506
used because it format-string matches string pool constant ic_plus_anim_%1$d.
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# This file is no longer maintained维护 and is not used by new (2.2+) versions of the
# Android plugin for Gradle. Instead相反, the Android plugin for Gradle generates the
# default rules at build time and stores them in the build directory.
-dontusemixedcaseclassnames
-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 class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native <methods>;
}
# keep setters in Views so that animations can still work. see http://proguard.sourceforge.net/manual/examples.html#beans
-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
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
# For enumeration枚举 classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
# 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.
-dontwarn android.support.**
# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep @android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
@android.support.annotation.Keep <init>(...);
}
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# This file is no longer maintained and is not used by new (2.2+) versions of the
# Android plugin for Gradle. Instead, the Android plugin for Gradle generates the
# default rules at build time and stores them in the build directory.
# Optimizations优化: If you don‘t want to optimize, use the proguard-android.txt configuration file
# instead of this one, which turns off the optimization flags. 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路线.
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* #*/
-optimizationpasses 5
-allowaccessmodification
-dontpreverify
# The remainder其余部分 of this file is identical相同 to the non-optimized非优化 version of the Proguard
# configuration file (except除了 that the other file has flags to turn off optimization).
...
# 后面的与 proguard-android.txt 完全一样