問題描述
最近改sdk,發(fā)現(xiàn)我整個模塊都參與了混淆,里面所有的類,都已經(jīng)變成不規(guī)則的名字了,但是整個包的路徑依舊還在。
一個Bug發(fā)現(xiàn)的問題
服務(wù)器說收不到基礎(chǔ)參數(shù)信息,比如userId,token,我們的基礎(chǔ)參數(shù),都由一個BaseModel去接收,然后將model轉(zhuǎn)map,然后再轉(zhuǎn)json,最后傳遞給服務(wù)器
class BaseModel(
@SerializedName("userId")
var userId: String = "",
@SerializedName("token")
var token: String = ""
)
我在測試中發(fā)現(xiàn),如果我在最后請求服務(wù)器的時候,先去打印userId信息,形如
BaseModel baseModel = new BaseModel();
baseModel.setUserId("");
baseModel.setToken("");
Log.d("Tag",baseModel.getUserId());//打印信息
String jsonStr = new GsonBuilder().create().toJson(baseModel);
服務(wù)器竟然就能收到userId的參數(shù)信息,但是無法收到token信息,從此我懷疑是打release的時候,get被移除了

image.png
當時想的是,更新一下版本就可以了吧
更新Gson版本
目前使用的Gson版本是2.10.1,使用的混淆規(guī)則為
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
更新到2.11.0版本,使用最新版的混淆規(guī)則
##---------------Begin: proguard configuration for Gson gxx ----------
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-dontwarn sun.misc.**
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
##---------------END: proguard configuration for Gson gxx ----------
而且2.11.0開始,gson會自動配置混淆,gson.pro文件混淆規(guī)則。

image.png
使用到最新的版本后,問題就出現(xiàn)了,我的模塊 com.gxx.http這路徑下面的所有文件都參與了混淆,文件信息都被混淆了,但是路徑依舊保留的是com.gxx.http,沒有參與到混淆,從這里,我就感覺到時混淆配置文件出了問題,
最終,我從這里發(fā)現(xiàn)了端倪
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
我的源代碼里面,有使用TypeToken,再結(jié)合上面最新版本的Gson v2.11.0混淆配置信息
private fun checkUserMap(): MutableMap<String, String> {
if (userMap == null) {
val typeToken: TypeToken<MutableMap<String, String>> = object : TypeToken<MutableMap<String, String>>() {}
userMap = GsonUtils.fromJson(JSON, typeToken.type)
}
return userMap!!
}
再來看看,反編譯

image.png
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
1、-keep:指示 ProGuard 保持指定的類及其成員不被混淆或刪除。也就是說,TypeToken 類將不會被移除或改變其名稱。
2、allowobfuscation:允許該類在混淆過程中被混淆。這意味著 TypeToken 類的名稱可以被修改,但其結(jié)構(gòu)和功能保持不變。
3、allowshrinking:允許在最終構(gòu)建中刪除未使用的代碼。如果 TypeToken 不再被引用,其聲明仍然會被允許刪除,前提是其他部分的代碼不依賴于它。
我大概可以知道,如果要全部都參與到混淆里面(包括包名),那么需要所有的類+屬性+靜態(tài)內(nèi)部類,不能有任何一個滿足以上混淆配置條件的才行
解決方案
1、回退到2.10.1版本
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if(requested.group == "com.google.code.gson" && requested.name == "gson"){
details.useVersion("2.10.1")
}
}
}
2、使用如下混淆規(guī)則
##---------------Begin: proguard configuration for Gson gxx ----------
-keepattributes Signature
-keepattributes *Annotation*
-dontwarn sun.misc.**
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
#gxx 防止使用了@SerializedName注解后,參數(shù)被移除問題
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
##---------------END: proguard configuration for Gson gxx ----------