反編譯過別人的jar或者apk的小伙伴肯定會發(fā)現(xiàn)大量的類名,方法名,變量名等等,都被替換成了a,b,c,d之類的字母,造成難以閱讀。代碼混淆是保障我們代碼安全的一種有力手段,但是并不是所有代碼都需要被混淆的,不恰當?shù)幕煜赡軐е马椖繜o法正常運行。下面收集一些開發(fā)當中常用的一些三方依賴的混淆代碼。
1.ButterKnife
# Retain generated class which implement Unbinder.
-keep public class * implements butterknife.Unbinder { public <init>(...); }
# Prevent obfuscation of types which use ButterKnife annotations since the simple name
# is used to reflectively look up the generated ViewBinding.
-keep class butterknife.*
-keepclasseswithmembernames class * { @butterknife.* <methods>; }
-keepclasseswithmembernames class * { @butterknife.* <fields>; }
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
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
##---------------End: proguard configuration for Gson ----------
注意上面代碼中的-keep class com.google.gson.examples.android.model.** { *; }僅僅是一個示例,照抄過去是沒有作用的。這里我們需要避免混淆的是需要經(jīng)過Gson序列化的實體類,開發(fā)過程中我們通常將實體類放在同一個包下,這里直接寫包路徑即可。
3.Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
4.支付寶支付
-libraryjars libs/alipaySDK-20150602.jar
-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IAlixPay$Stub{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keep class com.alipay.sdk.app.PayTask{ public *;}
-keep class com.alipay.sdk.app.AuthTask{ public *;}
第一行中alipaySDK-20150602.jar為jar包名稱,注意替換為自己使用的jar包。
5.微信支付
-keep class com.tencent.mm.sdk.** {*;}
6.EventBus
-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);
}
7.xUtils3
-keepattributes Signature,*Annotation*
-keep public class org.xutils.** {
public protected *;
}
-keep public interface org.xutils.** {
public protected *;
}
-keepclassmembers class * extends org.xutils.** {
public protected *;
}
-keepclassmembers @org.xutils.db.annotation.* class * {*;}
-keepclassmembers @org.xutils.http.annotation.* class * {*;}
-keepclassmembers class * {
@org.xutils.view.annotation.Event <methods>;
}
8.個推
-dontwarn com.igexin.**
-keep class com.igexin.**{*;}
8.Retrofit2.1.0
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
9.高德地圖
3D 地圖
-keep class com.amap.api.maps.**{*;}
-keep class com.autonavi.amap.mapcore.*{*;}
-keep class com.amap.api.trace.**{*;}
定位
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}
搜索
-keep class com.amap.api.services.**{*;}
2D地圖
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}
導航
-keep class com.amap.api.navi.**{*;}
-keep class com.autonavi.**{*;}
10.Bugly
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}
有任何疑問,歡迎加群討論:261386924
文章同步更新于微信公眾號:
秉心說, 專注 Java 、 Android 原創(chuàng)知識分享,LeetCode 題解,歡迎關(guān)注!

qrcode_for_gh_de090eed4f33_258.jpg