# 避免混淆Android基本组件,下面是兼容性比较高的规则 -keep public class * extends android.app.Activity -keep public class * extends android.app.Fragment -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 -keep public class * extends android.view.View -keep public class com.android.vending.licensing.ILicensingService
# 保留support下的所有类及其内部类 -keep class android.support.** {*;} -keep interface android.support.** {*;} -keep public class * extends android.support.v4.** -keep public class * extends android.support.v7.** -keep public class * extends android.support.annotation.** -dontwarn android.support.**
# 保留androidx下的所有类及其内部类 -keep class androidx.** {*;} -keep public class * extends androidx.** -keep interface androidx.** {*;} -keep class com.google.android.material.** {*;} -dontwarn androidx.** -dontwarn com.google.android.material.** -dontnote com.google.android.material.**
# 保持Activity中与View相关方法不被混淆 -keepclassmembers class * extends android.app.Activity{ public void *(android.view.View); }
# 避免混淆所有native的方法,涉及到C、C++ -keepclasseswithmembernames class * { native <methods>; }
# 避免混淆自定义控件类的get/set方法和构造函数 -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); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); public <init>(android.content.Context, android.util.AttributeSet, int); }
# 避免混淆枚举类 -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }
# 避免混淆序列化类 # 不混淆Parcelable和它的实现子类,还有Creator成员变量 -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; }
# 不混淆Serializable和它的实现子类、其成员变量 -keep public class * implements java.io.Serializable {*;} -keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); }
#(可选)避免Log打印输出 -assumenosideeffects class android.util.Log { public static *** v(...); public static *** d(...); public static *** i(...); public static *** w(...); public static *** e(...); }
# ==================glide start=================== -keep public class * implements com.bumptech.glide.module.GlideModule -keep public class * extends com.bumptech.glide.module.AppGlideModule -keep public enum com.bumptech.glide.load.ImageHeaderParser$** { **[] $VALUES; public *; } #glide如果你的API级别<=Android API 27 则需要添加 -dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder # for DexGuard only #-keepresourcexmlelements manifest/application/meta-data@value=GlideModule # ==================glide end=====================
# ==================okhttp start=================== -dontwarn okhttp3.** -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn org.conscrypt.** # A resource is loaded with a relative path so the package of this class must be preserved. -keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase # Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java. -dontwarn org.codehaus.mojo.animal_sniffer.* # OkHttp platform used only on JVM and when Conscrypt dependency is available. -dontwarn okhttp3.internal.platform.ConscryptPlatform # ==================okhttp end=====================
# ==================retrofit2 start=================== # Retain generic type information for use by reflection by converters and adapters. -keepattributes Signature # Retain service method parameters. -keepclassmembernames,allowobfuscation interface * { @retrofit2.http.* <methods>; } # Ignore annotation used for build tooling. -dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Only required if you use AsyncExecutor -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { <init>(java.lang.Throwable); } # ==================eventbus end===================