一、通用模板
#-------------------------------------------基本不用动区域----------------------------------------
#忽略警告 不忽略可能打包不成功
-ignorewarnings
#---------------------------------基本指令区----------------------------------
-optimizationpasses 5
-dontskipnonpubliclibraryclassmembers
-printmapping proguardMapping.txt
-optimizations !code/simplification/cast,!field/*,!class/merging/*
-keepattributes *Annotation*,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
#----------------------------------------------------------------------------
#---------------------------------默认保留区---------------------------------
-ignorewarnings
# 保留我们使用的四大组件,自定义的Application等等这些类不被混淆
# 因为这些子类都有可能被外部调用
-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下的所有类及其内部类
-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中的方法参数是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();
}
# 对于带有回调函数的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);
}
#----------------------------------------------------------------------------------------------------------
#-------------------------------------------定制化区域----------------------------------------------
#---------------------------------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互相调用的类(如果没有可忽略)------------------------
#-keep class 你的类所在的包.** { *; }
#如果是内部类的话
#-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(类名的路径)
注:现在app都会进行加固,加固的时候,其实都进行混淆了,拿走不谢~