# 保持哪些类不被混淆 -keep public class * extends android.app.Activity -keep public class * extends android.app.Application
-keep public class * extends android.app.Fragment -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 com.android.vending.licensing.ILicensingService # 保持 native 方法不被混淆 -keepclasseswithmembernames class * { native <methods>; } # 保持自定义控件类不被混淆 -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); } # 保持自定义控件类不被混淆 -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet, int); } # 保持自定义控件类不被混淆 -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } # 保持枚举 enum 类不被混淆 -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } # 保持 Parcelable 不被混淆 -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } # 保持自己定义的类不被混淆 -keep class MyClass;
2. 代码混淆举例一
#-----------------混淆配置设定-------------------------------------- #指定代码压缩级别 -optimizationpasses 5 #混淆时不会产生形形色色的类名 -dontusemixedcaseclassnames #指定不忽略非公共类库 -dontskipnonpubliclibraryclasses #不预校验,如果需要预校验,是-dontoptimize -dontpreverify #屏蔽警告 -ignorewarnings #混淆时记录日志 -verbose #优化 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* #----导入第三方包,但是在当前版本中使用会报 input jar file is specified twice 错误,所以注释掉 #-libraryjars libs/android.support.v4.jar #-libraryjars libs/BaiduLBS_Android.jar #-libraryjars libs/commons-httpclient-3.1.jar #-libraryjars libs/jackson-annotations-2.1.4.jar #-libraryjars libs/jackson-core-2.1.4.jar #-libraryjars libs/jackson-databind-2.1.4.jar #-libraryjars libs/xUtils-2.6.14.jar #-----------------不需要混淆第三方类库------------------------------------------------------------------ #去掉警告 -dontwarn android.support.v4.** #过滤android.support.v4 -keep class android.support.v4.** { *; } -keep interface android.support.v4.app.** { *; } -keep public class * extends android.support.v4.** -keep public class * extends android.app.Fragment #过滤commons-httpclient-3.1.jar -keep class org.apache.**{*;} #过滤jackson-core-2.1.4.jar等 -keep class com.fasterxml.jackson.**{*;} #去掉警告 -dontwarn com.lidroid.xutils.** #过滤xUtils-2.6.14.jar -keep class com.lidroid.xutils.**{*;} #这是xUtils文档中提到的过滤掉注解 -keep class * extends java.lang.annotation.Annotation{*;} #去掉警告 -dontwarn com.baidu.** -dontwarn com.baidu.mapapi.** #过滤BaiduLBS_Android.jar -keep class com.baidu.** {*;} -keep class vi.com.gdi.bgl.android.**{*;} -keep class com.baidu.platform.**{*;} -keep class com.baidu.location.**{*;} -keep class com.baidu.vi.**{*;} #-----------------不需要混淆系统组件等------------------------------------------------------------------- -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.preference.Preference -keep public class com.android.vending.licensing.ILicensingService #过滤掉自己编写的实体类 -keep class com.classtc.test.entity.**{*;} #------保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在----------- -keepclasseswithmembernames class * { public <init>(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembernames class * { public <init>(android.content.Context, android.util.AttributeSet, int); }
3. 代码混淆举例二
#指定代码的压缩级别 -optimizationpasses 5 #包明不混合大小写 -dontusemixedcaseclassnames #不去忽略非公共的库类 -dontskipnonpubliclibraryclasses #优化 不优化输入的类文件 -dontoptimize #不混淆输入的类文件 -dontobfuscate #预校验 -dontpreverify # 有了verbose这句话,混淆后就会生成映射文件 # 包含有类名->混淆后类名的映射关系 # 然后使用printmapping指定映射文件的名称 -verbose -printmapping priguardMapping.txt # 混淆时所采用的算法 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* #保护注解 -keepattributes *Annotation* # 抛出异常时保留代码行号 -keepattributes SourceFile,LineNumberTable # 保持哪些类不被混淆 -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 -keep public class com.android.vending.licensing.ILicensingService #如果有引用v4包可以添加下面这行 -keep public class * extends android.support.v4.app.Fragment #如果引用了v4或者v7包 -dontwarn android.support.** # 保留Activity中的方法参数是view的方法, # 从而我们在layout里面编写onClick就不会影响 -keepclassmembers class * extends android.app.Activity { public void * (android.view.View); } -keep public class * extends android.view.View { public <init>(android.content.Context); public <init>(android.content.Context, android.util.AttributeSet); public <init>(android.content.Context, android.util.AttributeSet, int); public void set*(...); *** get* (); } #保持 native 方法不被混淆 -keepclasseswithmembernames class * { native <methods>; } #保持自定义控件类不被混淆 -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); } #保持 Parcelable 不被混淆 -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } #保持 Serializable 不被混淆 -keep public class * implements java.io.Serializable { public *; } -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(); } #不混淆资源类 -keepclassmembers class **.R$* { public static <fields>; } # 对于带有回调函数onXXEvent的,不能混淆 -keepclassmembers class * { void *(**On*Event); } #移除log 测试了下没有用还是建议自己定义一个开关控制是否输出日志 -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(...); } -dontwarn com.squareup.** -keep class com.squareup.**{*; } -dontwarn org.apache.** -keep class org.apache.**{*;} -dontwarn com.tencent.** -keep class com.tencent.**{*;} -dontwarn com.sina.** -keep class com.sina.**{*;} -dontwarn com.baidu.** -keep class com.baidu.**{*;} -dontwarn com.alipay.** -keep class com.alipay.**{*;} -dontwarn org.json.alipay.** -keep class org.json.alipay.**{*;} -dontwarn org.apache.http.entity.mime.** -keep class org.apache.http.entity.mime.**{*;} -dontwarn com.rongkecloud.** -keep class com.rongkecloud.**{*;} -dontwarn com.rongke.** -keep class com.rongke.**{*;} -dontwarn org.** -keep class org.**{*;} -dontwarn a.a.a.a.a.** -keep class a.a.a.a.a.**{*;} -dontwarn chat.demo.entity.** -keep class chat.demo.entity.**{*;} -dontwarn av.demo.entity.** -keep class av.demo.entity.**{*;} -dontwarn com.google.zxing.** -keep class com.google.zxing.**{*;} -dontwarn com.lking.demo.entity.** -keep class com.lking.demo.entity.**{*;} -keep class com.youth.banner.** { *; } -keepattributes *JavascriptInterface* -keepclassmembers class com.lking.demo.ui.find.JstoAndroid$JsInterface { public *; } -keepattributes *JavascriptInterface* -keepclassmembers class com.lking.androidlib.widget.WebView$*{ *; } #facebook fresco -keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip # Do not strip any method/class that is annotated with @DoNotStrip -keep @com.facebook.common.internal.DoNotStrip class * -keepclassmembers class * { @com.facebook.common.internal.DoNotStrip *; } # Keep native methods -keepclassmembers class * { native <methods>; } -dontwarn okio.** -dontwarn javax.annotation.** -dontwarn com.android.volley.toolbox.**