应用开发完了,测试没问题了,快上市场了,这时候就要混淆加固多渠道打包加上线了,所以,混淆就来了:
混淆是什么,就是把类名,方法名,变量名给弄成简单的单词或者字母来代替,从而防止别人反编译后轻松的得到源码。与其说我们配置混淆,不如说我们来配置哪些包里面的哪些类不要被混淆,也就是在下面的那个文件里声明一下,哪些不要给我混淆:
1.在module的build.gradle的android标签里
buildTypes { release { signingConfig signingConfigs.release shrinkResources true minifyEnabled true zipAlignEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { signingConfig signingConfigs.release shrinkResources true minifyEnabled true zipAlignEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
2.在proguard-rules.pro里
-optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassnames # 是否使用大小写混合 -dontpreverify # 混淆时是否做预校验 -verbose # 混淆时是否记录日志 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法 #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.ImageHeaderParser$** { **[] $VALUES; public *; } # for DexGuard only #-keepresourcexmlelements manifest/application/meta-data@value=GlideModule #okgo混淆 #okhttp -dontwarn okhttp3.** -keep class okhttp3.**{*;} #okio -dontwarn okio.** -keep class okio.**{*;} #腾讯bugly -dontwarn com.tencent.bugly.** -keep public class com.tencent.bugly.**{*;} #butterknife -keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * { @butterknife.* <fields>; } -keepclasseswithmembernames class * { @butterknife.* <methods>; } ## GSON 2.2.4 specific rules ## # 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* -keepattributes EnclosingMethod # Gson specific classes -dontwarn com.google.gson.** #EventBus2.0 -keep class de.greenrobot.event.** {*;} -keepclassmembers class ** { public void onEvent*(**); void onEvent*(**); } ## New rules for EventBus 3.0.x ## # http://greenrobot.org/eventbus/documentation/proguard/ -keepattributes *Annotation* -keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } # Only required if you use AsyncExecutor -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { <init>(java.lang.Throwable); } # fastjson proguard rules # https://github.com/alibaba/fastjson -dontwarn com.alibaba.fastjson.** -keepattributes Signature -keepattributes *Annotation* #support-v7-appcompat -keep public class android.support.v7.widget.** { *; } -keep public class android.support.v7.internal.widget.** { *; } -keep public class android.support.v7.internal.view.menu.** { *; } -keep public class * extends android.support.v4.view.ActionProvider { public <init>(android.content.Context); } #support-design -dontwarn android.support.design.** -keep class android.support.design.** { *; } -keep interface android.support.design.** { *; } -keep public class android.support.design.R$* { *; } # 枚举也不要混淆Also keep - Enumerations. Keep the special static methods that are required in # enumeration classes. -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } #native方法不做混淆 Keep names - Native method names. Keep all native class/method names. -keepclasseswithmembers,allowshrinking class * { native <methods>; } # 保持 Parcelable 不被混淆 -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } #实体类不参与混淆 -keep class com.hejun.solidtext.model.** { *; } #不混淆R类 -keep public class com.jph.android.R$*{ public static final int *; }这里面我也还有一些不是很明白,比如android的很多系统依赖,比如通过gradle配置的一些v4,v7包里面的依赖是不是也要配置不要被混淆呢?但是,我的应用小,做了上面的事情就够了。