ProGuard 是什么?
可以把 ProGuard 理解為是對(duì)代碼和資源壓縮的一個(gè)工具,它能夠提供對(duì) Java 類文件的壓縮、優(yōu)化、混淆,和預(yù)校驗(yàn)。壓縮的步驟是檢測(cè)并移除未使用的類、字段、方法和屬性。優(yōu)化的步驟是分析和優(yōu)化方法的字節(jié)碼?;煜牟襟E是使用短的毫無(wú)意義的名稱重命名剩余的類、字段和方法。壓縮、優(yōu)化、混淆使得代碼更小,更高效。
AndroidStudio 怎么使用 ProGuard ?
代碼壓縮
要通過(guò) ProGuard 啟用代碼壓縮,請(qǐng)?jiān)?build.gradle 文件內(nèi)相應(yīng)的構(gòu)建類型中添加 minifyEnabled true。
android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
ProGuard 會(huì)移除所有 (并且只會(huì)移除) 未使用的代碼。不過(guò) , ProGuard 難以對(duì)許多情況進(jìn)行正確分析,可能會(huì)移除應(yīng)用真正需要的代碼。比如需要反射、動(dòng)態(tài)加載所引用的類等情況,可能因?yàn)镻roGuard 移除或者混淆了這部分沒(méi)使用的類,而導(dǎo)致錯(cuò)誤。所以有時(shí)需要編寫混淆優(yōu)化配置文件。在 gradle 中的 proguardFiles 能夠讓我們傳遞File文件或者文件路徑交給 proguard 來(lái)執(zhí)行。
配置 ProGuard 規(guī)則
現(xiàn)在我們開(kāi)啟了混淆,但是還沒(méi)有配置混淆,我們可以在 build/intermediates/proguard-files/proguard-defaults.txt 來(lái)查看默認(rèn)的配置,現(xiàn)在我們可以根據(jù)默認(rèn)的配置來(lái)進(jìn)行我們項(xiàng)目的配置。
# 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 #######################
# 混淆時(shí)所采用的算法(谷歌推薦算法)
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
# 指定代碼的壓縮級(jí)別(在0~7之間,默認(rèn)為5)
-optimizationpasses 5
# 提高優(yōu)化步驟
-allowaccessmodification
# 包名不混合大小寫
-dontusemixedcaseclassnames
# 不忽略非公共的庫(kù)類
-dontskipnonpubliclibraryclasses
# 輸出混淆日志
-verbose
# 保持 Google 原生服務(wù)需要的類不被混淆
-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
# 混淆注意事項(xiàng)第二條,保持 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 中的方法參數(shù)是 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>;
}
# 支持庫(kù)包含對(duì)較新版本版本的引用。
# 不要警告那些情況下,這個(gè)應(yīng)用程序鏈接到舊的
# 平臺(tái)版本。我們知道他們是安全的。
-dontnote android.support.**
-dontnote androidx.**
-dontwarn android.support.**
-dontwarn androidx.**
# 此類已棄用,但仍保留向后兼容性。
-dontwarn android.util.FloatMath
# Support包規(guī)則
# 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 之間重復(fù)
-dontnote org.apache.http.**
-dontnote android.net.http.**
# 排除 android.jar 和核心-lambda-stubs.jar 之間重復(fù)。
-dontnote java.lang.invoke.**
以上就是默認(rèn)配置文件那么這樣生成出來(lái)的 APK 到底是什么樣的勒?下面放上一張圖片來(lái)看下。

發(fā)現(xiàn)上面的 類 已經(jīng)變成不易閱讀的類了。下面我們就來(lái)單獨(dú)分析下 Proguard 的配置
基本指令
指定代碼的壓縮級(jí)別(在0~7之間,默認(rèn)為5)
-optimizationpasses 5
是否使用大小寫混合(windows大小寫不敏感,建議加入)
-dontusemixedcaseclassnames
是否混淆非公共的庫(kù)的類
-dontskipnonpubliclibraryclasses
是否混淆非公共的庫(kù)的類的成員
-dontskipnonpubliclibraryclassmembers
混淆時(shí)是否做預(yù)校驗(yàn)(Android不需要預(yù)校驗(yàn),去掉可以加快混淆速度)
-dontpreverify
混淆時(shí)是否記錄日志(混淆后會(huì)生成映射文件)
-verbose
指定外部模糊字典
-obfuscationdictionary dictionary_path
指定 class 模糊字典
-classobfuscationdictionary dictionary_path
指定 package 模糊字典
-packageobfuscationdictionary dictionary_path
混淆時(shí)所采用的算法(谷歌推薦算法)
-optimizations !code/simplification/arithmetic,!field/,!class/merging/,!code/allocation/variable
添加支持的jar(引入libs下的所有jar包)
-libraryjars libs(*.jar;)
將文件來(lái)源重命名為 SourceFile”`字符串
-renamesourcefileattribute SourceFile
保持注解不被混淆
-keepattributes Annotation
-keep class * extends java.lang.annotation.Annotation {*;}
-keep interface * extends java.lang.annotation.Annotation { *; }
保持泛型不被混淆
-keepattributes Signature-keep class * extends java.lang.annotation.Annotation {*;}
保持反射不被混淆
-keepattributes EnclosingMethod
保持異常不被混淆
-keepattributes Exceptions
保持內(nèi)部類不被混淆
-keepattributes InnerClasses
拋出異常時(shí)保留代碼行號(hào)
-keepattributes SourceFile,LineNumberTable
keep 命令說(shuō)明
指令 說(shuō)明
-keep 保持類和類成員,防止被移除或者被重命名
-keepnames 保持類和類成員,防止被重命名
-keepclassmembers 保持類成員,防止被移除或者被重命名
-keepclassmembernames 保持類成員,防止被重命名
-keepclasseswithmembers 保持擁有該成員的類和成員,防止被移除或者被重命名
-keepclasseswithmembernames 保持擁有該成員的類和成員,防止被重命名
保持元素不參與混淆的規(guī)則的命令格式:
保持元素不參與混淆的規(guī)則的命令格式:
[保持命令] [類] {
[成員]
}
具體的類
訪問(wèn)修飾符(public、protected、private)
通配符*,匹配任意長(zhǎng)度字符,但不含包名分隔符(.)
通配符**,匹配任意長(zhǎng)度字符,并且包含包名分隔符(.)
extends,即可以指定類的基類
implement,匹配實(shí)現(xiàn)了某接口的類
$,內(nèi)部類
“成員”代表類成員相關(guān)的限定條件,它將最終定位到某些符合該限定條件的
類成員。它的內(nèi)容可以使用:
<init> 匹配所有構(gòu)造器
<fields> 匹配所有域
<methods> 匹配所有方法
通配符*,匹配任意長(zhǎng)度字符,但不含包名分隔符(.)
通配符**,匹配任意長(zhǎng)度字符,并且包含包名分隔符(.)
通配符***,匹配任意參數(shù)類型
…,匹配任意長(zhǎng)度的任意類型參數(shù)。比如void test(…)就能匹配任意 void
test(String a) 或者是 void test(int a, String b) 這些方法。
訪問(wèn)修飾符(public、protected、private)
不混淆某個(gè)類
-keep public class com.example.proxy_core.ProxyApplication { *; }
不混淆某個(gè)包所有的類
-keep public class com.example.proxy_core.** { *; }
不混淆某個(gè)類的子類
-keep public class * extends class
com.example.proxy_core.ProxyApplication { *; }
```
不混淆所有類名中包含了 model的類及其成員
-keep public class **.*model*.** {*;}
不混淆某個(gè)接口的實(shí)現(xiàn)
-keep class * implements com.example.proxy_core.LoadCallBack { *; }
不混淆某個(gè)類的特定的方法
-keepclassmembers class com.example.proxy_core.ProxyApplication {
public void getVersion(java.lang.String);
}
不混淆某個(gè)類的內(nèi)部類
-keep class com.example.proxy_core.ProxyApplication$* {
*;
}
Proguard 注意事項(xiàng)
基本組件不被混淆
-keep public class * extends android.app.Fragment
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
保持 Google 原生服務(wù)需要的類不被混淆
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
Support 包規(guī)則
-dontwarn android.support.**
-keep public class * extends android.support.v4.**
-keep public class * extends android.support.v7.**
-keep public class * extends android.support.annotation.**
保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
}
保留自定義控件 ( 繼承自 View ) 不被混淆
-keep public class * extends android.view.View {
*** get*();
void set*(***);
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
保留指定格式的構(gòu)造方法不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
保留在 Activity 中的方法參數(shù)是 view 的方法(避免布局文件里面 onClick 被影響)
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
保持枚舉 enum 類不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
保持 R (資源)下的所有類及其方法不能被混淆
-keep class **.R$* { *; }
保持 Parcelable 序列化的類不被混淆(注:aidl 文件不能去混淆)
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
需要序列化和反序列化的類不能被混淆(注:Java 反射用到的類也不能被混淆)
-keepnames class * implements java.io.Serializable
保持 Serializable 序列化的類成員不被混淆
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
保持 Adapter 類不被混淆
-keep public class * extends android.widget.BaseAdapter { *; }
保持 CusorAdapter 類不被混淆
-keep public class * extends android.widget.CusorAdapter{ *; }
編寫基礎(chǔ)通用版本混淆規(guī)則
# 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.
# --------------------------------------------基本指令區(qū)-------------------------------------------# 指定代碼的壓縮級(jí)別(在0~7之間,默認(rèn)為5)
-optimizationpasses 5
# 是否使用大小寫混合(windows大小寫不敏感,建議加入)
-dontusemixedcaseclassnames
# 是否混淆非公共的庫(kù)的類
-dontskipnonpubliclibraryclasses
# 是否混淆非公共的庫(kù)的類的成員
-dontskipnonpubliclibraryclassmembers
# 混淆時(shí)是否做預(yù)校驗(yàn)(Android不需要預(yù)校驗(yàn),去掉可以加快混淆速度)
-dontpreverify
# 混淆時(shí)是否記錄日志(混淆后會(huì)生成映射文件)
-verbose
###################################### 如果有就添加 ############################################
#指定外部模糊字典
-obfuscationdictionary dictionary1.txt
#指定class模糊字典
-classobfuscationdictionary dictionary1.txt
#指定package模糊字典
-packageobfuscationdictionary dictionary2.txt
#########################################################################################
# 混淆時(shí)所采用的算法(谷歌推薦算法)
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!code/allocation/variable
# 添加支持的jar(引入libs下的所有jar包)
-libraryjars libs(*.jar;)
# 將文件來(lái)源重命名為“SourceFile”字符串
-renamesourcefileattribute SourceFile
# 保持注解不被混淆
-keepattributes *Annotation*
-keep class * extends java.lang.annotation.Annotation {*;}
# 保持泛型不被混淆
-keepattributes Signature
# 保持反射不被混淆
-keepattributes EnclosingMethod
# 保持異常不被混淆
-keepattributes Exceptions
# 保持內(nèi)部類不被混淆
-keepattributes Exceptions,InnerClasses
# 拋出異常時(shí)保留代碼行號(hào)
-keepattributes SourceFile,LineNumberTable
# --------------------------------------------默認(rèn)保留區(qū)--------------------------------------------#
# 保持基本組件不被混淆
-keep public class * extends android.app.Fragment
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
# 保持 Google 原生服務(wù)需要的類不被混淆
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
# Support包規(guī)則
-dontwarn android.support.**
-keep public class * extends android.support.v4.**
-keep public class * extends android.support.v7.**
-keep public class * extends android.support.annotation.**
# 保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
}
# 保留自定義控件(繼承自View)不被混淆
-keep public class * extends android.view.View {
*** get*();
void set*(***);
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
# 保留指定格式的構(gòu)造方法不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
# 保留在Activity中的方法參數(shù)是view的方法(避免布局文件里面onClick被影響)
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
# 保持枚舉 enum 類不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# 保持R(資源)下的所有類及其方法不能被混淆
-keep class **.R$* { *; }
# 保持 Parcelable 序列化的類不被混淆(注:aidl文件不能去混淆)
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# 需要序列化和反序列化的類不能被混淆(注:Java反射用到的類也不能被混淆)
-keepnames class * implements java.io.Serializable
# 保持 Serializable 序列化的類成員不被混淆
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
# 保持 BaseAdapter 類不被混淆
-keep public class * extends android.widget.BaseAdapter { *; }
# 保持 CusorAdapter 類不被混淆
-keep public class * extends android.widget.CusorAdapter{ *; }
# --------------------------------------------webView區(qū)--------------------------------------------#
# WebView處理,項(xiàng)目中沒(méi)有使用到webView忽略即可
# 保持Android與JavaScript進(jìn)行交互的類不被混淆
-keep class **.AndroidJavaScript { *; }
-keepclassmembers class * extends android.webkit.WebViewClient {
public void *(android.webkit.WebView,java.lang.String,android.graphics.Bitmap);
public boolean *(android.webkit.WebView,java.lang.String);
}
-keepclassmembers class * extends android.webkit.WebChromeClient {
public void *(android.webkit.WebView,java.lang.String);
}
# 網(wǎng)絡(luò)請(qǐng)求相關(guān)
-keep public class android.net.http.SslError
# --------------------------------------------刪除代碼區(qū)--------------------------------------------#
# 刪除代碼中Log相關(guān)的代碼
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int i(...);
public static int w(...);
public static int d(...);
public static int e(...);
}
第三方需要的混淆規(guī)則
################alipay###############
-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IAlixPay$Stub{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keep class com.alipay.sdk.app.PayTask{ public *;}
-keep class com.alipay.sdk.app.AuthTask{ public *;}
################retrofit###############
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
################butterknife###############
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
################gson###############
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.sunloto.shandong.bean.** { *; }
################glide###############
-keep public class * implements com.bumptech.glide.module.AppGlideModule
-keep public class * implements com.bumptech.glide.module.LibraryGlideModule
-keep class com.bumptech.glide.** { *; }
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
################okhttp###############
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn com.squareup.okhttp.**
################androidEventBus###############
-keep class org.simple.** { *; }
-keep interface org.simple.** { *; }
-keepclassmembers class * {
@org.simple.eventbus.Subscriber <methods>;
}
-keepattributes *Annotation*
################EventBus###############
-keepclassmembers class * {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep class org.greenrobot.eventbus.EventBus { *; }
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
################autolayout###############
-keep class com.zhy.autolayout.** { *; }
-keep interface com.zhy.autolayout.** { *; }
################RxJava and RxAndroid###############
-dontwarn org.mockito.**
-dontwarn org.junit.**
-dontwarn org.robolectric.**
-keep class io.reactivex.** { *; }
-keep interface io.reactivex.** { *; }
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-dontwarn okio.**
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn io.reactivex.**
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
@retrofit.http.* <methods>;
}
-keep class sun.misc.Unsafe { *; }
-dontwarn java.lang.invoke.*
-keep class io.reactivex.schedulers.Schedulers {
public static <methods>;
}
-keep class io.reactivex.schedulers.ImmediateScheduler {
public <methods>;
}
-keep class io.reactivex.schedulers.TestScheduler {
public <methods>;
}
-keep class io.reactivex.schedulers.Schedulers {
public static ** test();
}
-keepclassmembers class io.reactivex.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class io.reactivex.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
long producerNode;
long consumerNode;
}
-keepclassmembers class io.reactivex.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
io.reactivex.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class io.reactivex.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
io.reactivex.internal.util.atomic.LinkedQueueNode consumerNode;
}
-dontwarn io.reactivex.internal.util.unsafe.**
################espresso###############
-keep class android.support.test.espresso.** { *; }
-keep interface android.support.test.espresso.** { *; }
################annotation###############
-keep class android.support.annotation.** { *; }
-keep interface android.support.annotation.** { *; }
################RxLifeCycle#################
-keep class com.trello.rxlifecycle2.** { *; }
-keep interface com.trello.rxlifecycle2.** { *; }
################RxPermissions#################
-keep class com.tbruyelle.rxpermissions2.** { *; }
-keep interface com.tbruyelle.rxpermissions2.** { *; }
################RxCache#################
-dontwarn io.rx_cache2.internal.**
-keep class io.rx_cache2.internal.Record { *; }
-keep class io.rx_cache2.Source { *; }
-keep class io.victoralbertos.jolyglot.** { *; }
-keep interface io.victoralbertos.jolyglot.** { *; }
################RxErrorHandler#################
-keep class me.jessyan.rxerrorhandler.** { *; }
-keep interface me.jessyan.rxerrorhandler.** { *; }
################Timber#################
-dontwarn org.jetbrains.annotations.**
################Canary#################
-dontwarn com.squareup.haha.guava.**
-dontwarn com.squareup.haha.perflib.**
-dontwarn com.squareup.haha.trove.**
-dontwarn com.squareup.leakcanary.**
-keep class com.squareup.haha.** { *; }
-keep class com.squareup.leakcanary.** { *; }
# Marshmallow removed Notification.setLatestEventInfo()
-dontwarn android.app.Notification
混淆后的代碼錯(cuò)誤?;謴?fù)方法
把 outputs/mapping/debug/mapping.txt 文件保存 (保存了混淆前后的對(duì)應(yīng)關(guān)系)。
使用工具 sdk/tools/groguard/bin/retrace.bat 先配置 -keepattributes SourceFile,LineNumberTable 再執(zhí)行 retrace.bat -verbose mappint 文件 bug 文件
資源壓縮
資源壓縮只與代碼壓縮協(xié)同工作。代碼壓縮器移除所有未使用的代碼后,資源壓縮器便可確定應(yīng)用仍然使用的資源。這在您添加包含資源的代碼庫(kù)時(shí)體現(xiàn)得尤為明顯 - 您必須移除未使用的庫(kù)代碼,使庫(kù)資源變?yōu)槲匆觅Y源,才能通過(guò)資源壓縮器將它們移除
要啟用資源壓縮,請(qǐng)?jiān)?build.gradle 文件中將 shrinkResources 屬性設(shè)置為 true(在用于代碼壓縮的 minifyEnabled 旁邊)。例如:
android {
...
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
如果您尚未使用代碼壓縮用途的 minifyEnabled 構(gòu)建應(yīng)用,請(qǐng)先嘗試使用它,然后再啟用 shrinkResources,因?yàn)槟赡苄枰庉?proguard-rules.pro 文件以保留動(dòng)態(tài)創(chuàng)建或調(diào)用的類或方法,然后再開(kāi)始移除資源。
移除未使用的備用資源
1、移除僅支持中文和英語(yǔ)
android {
defaultConfig {
...
resConfigs "zh","en"
}
}
2、合并重復(fù)資源
默認(rèn)情況下,Gradle 還會(huì)合并同名資源,例如可能位于不同資源文件夾中的同名可繪制對(duì)象。這一行為不受 shrinkResources 屬性控制,也無(wú)法停用,因?yàn)樵谟卸鄠€(gè)資源匹配代碼查詢的名稱時(shí),有必要利用這一行為來(lái)避免錯(cuò)誤。
只有在兩個(gè)或更多個(gè)文件具有完全相同的資源名稱、類型和限定符時(shí),才會(huì)進(jìn)行資源合并。Gradle 會(huì)在重復(fù)項(xiàng)中選擇其視為最佳選擇的文件(根據(jù)下述優(yōu)先順序),并只將這一個(gè)資源傳遞給 AAPT,以供在 APK 文件中分發(fā)。
Gradle 會(huì)在下列位置尋找重復(fù)資源:
與主源集關(guān)聯(lián)的主資源,一般位于 src/main/res/ 中。
變體疊加,來(lái)自構(gòu)建類型和構(gòu)建風(fēng)味。
3、庫(kù)項(xiàng)目依賴項(xiàng)。
Gradle 會(huì)按以下級(jí)聯(lián)優(yōu)先順序合并重復(fù)資源:
依賴項(xiàng) → 主資源 → 構(gòu)建風(fēng)味 → 構(gòu)建類型
例如,如果某個(gè)重復(fù)資源同時(shí)出現(xiàn)在主資源和構(gòu)建風(fēng)味中,Gradle 會(huì)選擇構(gòu)建風(fēng)味中的重復(fù)資源。
如果完全相同的資源出現(xiàn)在同一源集中,Gradle 無(wú)法合并它們,并且會(huì)發(fā)出資源合并錯(cuò)誤。如果您在 build.gradle 文件的 sourceSet 屬性中定義了多個(gè)源集,則可能會(huì)發(fā)生這種情況,例如,如果 src/main/res/ 和 src/main/res2/ 包含完全相同的資源,就可能會(huì)發(fā)生這種情況。
4、排查資源壓縮問(wèn)題
當(dāng)您壓縮資源時(shí),Gradle Console 會(huì)顯示它從應(yīng)用軟件包中移除的資源的摘要。例如:
:android:shrinkDebugResources Removed unused resources: Binary resource data reduced from 2570KB to 1711KB: Removed 33% :android:validateDebugSigning
Gradle 還會(huì)在 <module-name>/build/outputs/mapping/release/(ProGuard 輸出文件所在的文件夾)中創(chuàng)建一個(gè)名為 resources.txt 的診斷文件。該文件包括諸如哪些資源引用了其他資源以及使用或移除了哪些資源等詳情。