涉及json转换,数据库存储,以及第三方的库都不要混淆
建议实体bean都不要混淆
event bus通过方法名识别的方法,不要混淆
layout里绑定类文件中的方法不要混淆
混淆keep可以控制以下层级:
1、包级,包名以下的类都不混淆
-keep class xxx.xxx.**{*;}
2、类级,整个类都不混淆
-keep class xxx.xxx.className;
3、类成员级,类的方法、属性、内部类不做混淆
-keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; }
-keepclassmembers class * { void *(**on*Event); pulic *; }
附上完整的混淆方案:
# Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile #保持 native 的方法不去混淆 -keepclasseswithmembernames class * { native <methods>; } -keep class com.baidu.** {*;} -keep class vi.com.** {*;} -dontwarn com.baidu.** -keep class com.nxa.secsdk.** {*;} -keep class com.nesun.carmate.greendao.** {*;} -keep class com.umeng.** {*;} -keep class com.uc.** {*;} -keep class okhttp3.** {*;} -keep class io.reactivex.* {*;} -keepclassmembers class * { public <init> (org.json.JSONObject); } -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class com.zui.* {*;} -keep class com.miui.* {*;} -keep class com.heytap.* {*;} -keep class a.* {*;} -keep class com.vivo.* {*;} -keep xx.xx.实体 #ijkplayer -keep class tv.danmaku.ijk.media.player.** {*;} -keep class tv.danmaku.ijk.media.player.IjkMediaPlayer{*;} -keep class tv.danmaku.ijk.media.player.ffmpeg.FFmpegApi{*;} -keep public class com.nesun.carmate.R$*{ public static final int *; } # 代码混淆压缩比,在0~7之间,默认为5,一般不下需要修改 -optimizationpasses 5 # 混淆时不使用大小写混合,混淆后的类名为小写 # windows下的同学还是加入这个选项吧(windows大小写不敏感) -dontusemixedcaseclassnames # 指定不去忽略非公共的库的类 # 默认跳过,有些情况下编写的代码与类库中的类在同一个包下,并且持有包中内容的引用,此时就需要加入此条声明 -dontskipnonpubliclibraryclasses # 指定不去忽略非公共的库的类的成员 -dontskipnonpubliclibraryclassmembers # 不做预检验,preverify是proguard的四个步骤之一 # Android不需要preverify,去掉这一步可以加快混淆速度 -dontpreverify # 有了verbose这句话,混淆后就会生成映射文件 # 包含有类名->混淆后类名的映射关系 # 然后使用printmapping指定映射文件的名称 -verbose -printmapping priguardMapping.txt # 指定混淆时采用的算法,后面的参数是一个过滤器 # 这个过滤器是谷歌推荐的算法,一般不改变 -optimizations !code/simplification/artithmetic,!field/*,!class/merging/* # 保护代码中的Annotation不被混淆 # 这在JSON实体映射时非常重要 -keepattributes *Annotation* # 避免混淆泛型 # 这在JSON实体映射时非常重要 -keepattributes Signature # Gson specific classes -keep class com.google.gson.stream.** { *; } # Application classes that will be serialized/deserialized over Gson -keep class com.google.gson.examples.android.model.** { *; } -keep class com.google.gson.** { *;} # 抛出异常时保留代码行号 -keepattributes SourceFile,LineNumberTable # 保留所有的本地native方法不被混淆 -keepclasseswithmembernames class * { native <methods>; } # 保留了继承自Activity、Application这些类的子类 # 因为这些子类有可能被外部调用 # 比如第一行就保证了所有Activity的子类不要被混淆 -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 * extends android.view.View #support.v4/v7包不混淆 -keep class android.support.** { *; } -keep class android.support.v4.** { *; } -keep public class * extends android.support.v4.** -keep interface android.support.v4.app.** { *; } -keep class android.support.v7.** { *; } -keep public class * extends android.support.v7.** -keep interface android.support.v7.app.** { *; } -dontwarn android.support.** # 忽略警告 # 保留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 { 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* (); } # 保留Parcelable序列化的类不能被混淆 -keep class * implements android.os.Parcelable{ public static final android.os.Parcelable$Creator *; } # 不混淆实体类 -keep class com.jxty.app.garden.model.** { *; } # 保留Serializable 序列化的类不被混淆 -keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; !static !transient <fields>; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); } # 对R文件下的所有类及其方法,都不能被混淆 -keepclassmembers class **.R$* { *; } # 对于带有回调函数onXXEvent的,不能混淆 -keepclassmembers class * { void *(**on*Event); } #EventBus -keepattributes *Annotation* -keep class org.greenrobot.**{*;} -keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } #Retrofit # 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 #OkHttp -dontwarn okhttp3.** -dontwarn okio.** -dontwarn javax.annotation.** # A resource is loaded with a relative path so the package of this class must be preserved. -keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase #glide -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.resource.bitmap.ImageHeaderParser$** { **[] $VALUES; public *; } -dontwarn com.bumptech.glide.load.resource.bitmap.VideoDecoder #ucrop -dontwarn com.yalantis.ucrop** -keep class com.yalantis.ucrop** { *; } -keep interface com.yalantis.ucrop** { *; } #android-shape-imageview -keepattributes *Annotation,Signature -dontwarn com.github.siyamed.** -keep class com.github.siyamed.shapeimageview.**{ *; } # banner 的混淆代码 -keep class com.youth.banner.** { *; } #仿京东地址选择 -keep class chihane.jdaddressselector.** { *; } -keep class com.raizlabs.android.dbflow.** { *; } #BaseRecyclerViewAdapterHelper -keep class com.chad.library.adapter.** { *; } #uCrop -dontwarn com.yalantis.ucrop** -keep class com.yalantis.ucrop** { *; } -keep interface com.yalantis.ucrop** { *; } #微信sdk -dontwarn com.tencent.mm.opensdk.** -keep class com.tencent.mm.opensdk.**{*;} -dontwarn com.alipay.android.phone.mrpc.core.** -keep class com.alipay.android.phone.mrpc.core.** { *; } -keep class com.alipay.apmobilesecuritysdk.** { *; } -keep class com.alipay.mobile.framework.service.annotation.** { *; } -keep class com.alipay.mobilesecuritysdk.face.** { *; } -keep class com.alipay.tscenter.biz.rpc.** { *; } -keep class org.json.alipay.** { *; } -keep class com.alipay.tscenter.** { *; } -keep class com.ta.utdid2.** { *;} -keep class com.ut.device.** { *;} #bugly -dontwarn com.tencent.bugly.** -keep public class com.tencent.bugly.**{*;} -keep class android.support.**{*;} # 友盟 -dontwarn com.taobao.** -dontwarn anet.channel.** -dontwarn anetwork.channel.** -dontwarn org.android.** -dontwarn org.apache.thrift.** -dontwarn com.xiaomi.** -dontwarn com.huawei.** -dontwarn com.meizu.** -dontwarn com.umeng.message.** -keepattributes *Annotation* -keep class com.umeng.** {*;} -keep class org.repackage.** {*;} -keepclassmembers class * { public <init> (org.json.JSONObject); } -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class com.taobao.** {*;} -keep class org.android.** {*;} -keep class anet.channel.** {*;} -keep class com.xiaomi.** {*;} -keep class com.huawei.** {*;} -keep class com.meizu.** {*;} -keep class org.apache.thrift.** {*;} -keep class com.alibaba.sdk.android.**{*;} -keep class com.ut.**{*;} -keep class com.ta.**{*;} -keep public class **.R$*{ public static final int *; }