一、通用模板
#-------------------------------------------基本不用動區(qū)域----------------------------------------
#忽略警告 不忽略可能打包不成功
-ignorewarnings
#---------------------------------基本指令區(qū)----------------------------------
-optimizationpasses 5
-dontskipnonpubliclibraryclassmembers
-printmapping proguardMapping.txt
-optimizations !code/simplification/cast,!field/*,!class/merging/*
-keepattributes *Annotation*,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
#----------------------------------------------------------------------------
#---------------------------------默認保留區(qū)---------------------------------
-ignorewarnings
# 保留我們使用的四大組件铅檩,自定義的Application等等這些類不被混淆
# 因為這些子類都有可能被外部調(diào)用
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Appliction
-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
-keep public class * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
# 保留support下的所有類及其內(nèi)部類
-keep class android.support.** {*;}
# 保留繼承的
-keep public class * extends android.support.v4.**
-keep public class * extends android.support.v7.**
-keep public class * extends android.support.annotation.**
#androidx包使用混淆
-keep class com.google.android.material.** {*;}
-keep class androidx.** {*;}
-keep public class * extends androidx.**
-keep interface androidx.** {*;}
-dontwarn com.google.android.material.**
-dontnote com.google.android.material.**
-dontwarn androidx.**
# 保留R下面的資源
-keep class **.R$* {*;}
# 保留本地native方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
}
# 保留在Activity中的方法參數(shù)是view的方法憎夷,
# 這樣以來我們在layout中寫的onClick就不會被影響
-keepclassmembers class * extends android.app.Activity{
public void *(android.view.View);
}
# 保留枚舉類不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# 保留我們自定義控件(繼承自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);
}
# 保留Parcelable序列化類不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# 保留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();
}
# 對于帶有回調(diào)函數(shù)的onXXEvent、**On*Listener的昧旨,不能被混淆
-keepclassmembers class * {
void *(**On*Event);
void *(**On*Listener);
}
#----------------------------------------------------------------------------
#---------------------------------webview (項目中沒有使用到webView忽略即可)-----------------------------------
-keepclassmembers class fqcn.of.javascript.interface.for.Webview {
public *;
}
-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.WebViewClient {
public void *(android.webkit.WebView, jav.lang.String);
}
#----------------------------------------------------------------------------------------------------------
#-------------------------------------------定制化區(qū)域----------------------------------------------
#---------------------------------1.實體類---------------------------------
#bean的包路徑(如下:)
-keep class com.droa.demo.bean.** { *; }
#-------------------------------------------------------------------------
#---------------------------------2.第三方包-------------------------------
#一些依賴庫的混淆(如:gson)
##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }
# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
##---------------End: proguard configuration for Gson ----------
#-------------------------------------------------------------------------
#---------------------------------3.與js互相調(diào)用的類(如果沒有可忽略)------------------------
#-keep class 你的類所在的包.** { *; }
#如果是內(nèi)部類的話
#-keepclasseswithmembers class 你的類所在的包.父類$子類 { <methods>; }如下:
-keepclasseswithmembers class com.dora.demo.ui.MainActivity$JSInterface {
<methods>;
}
#-------------------------------------------------------------------------
#---------------------------------4.反射相關的類和方法(如果沒有可忽略)-----------------------
#-keep class 你的類所在的包.** { *; }
#----------------------------------------------------------------------------
二拾给、相關語法
- 代碼混淆的壓縮比例,值在0-7之間
-optimizationpasses 5
- 混淆后類名都為小寫
-dontusemixedcaseclassnames
- 指定不去忽略非公共的庫的類
-dontskipnonpubliclibraryclasses
- 指定不去忽略非公共的庫的類的成員
-dontskipnonpubliclibraryclassmembers
- 不做預校驗的操作
-dontpreverify
- 生成原類名和混淆后的類名的映射文件
-verbose
-printmapping proguardMapping.txt
- 指定混淆是采用的算法
-optimizations !code/simplification/cast,!field/*,!class/merging/*
- 不混淆Annotation
-keepattributes *Annotation*,InnerClasses
- 不混淆泛型
-keepattributes Signature
- 拋出異常時保留代碼行號
-keepattributes SourceFile,LineNumberTable
- 保留類名不變兔沃,也就是類名不混淆蒋得,而類中的成員名不保證
-keep class XXXX(類名的路徑)
- 保留類名和成員名
-keepclasseswithmembers class XXXX(類名的路徑)
注:現(xiàn)在app都會進行加固,加固的時候乒疏,其實都進行混淆了额衙,拿走不謝~