Gson混淆規(guī)則,引發(fā)某個(gè)module所有類名都被混淆了,但是依舊保留包的路徑信息

問題描述

最近改sdk,發(fā)現(xiàn)我整個(gè)模塊都參與了混淆,里面所有的類,都已經(jīng)變成不規(guī)則的名字了,但是整個(gè)包的路徑依舊還在。

一個(gè)Bug發(fā)現(xiàn)的問題

服務(wù)器說收不到基礎(chǔ)參數(shù)信息,比如userId,token,我們的基礎(chǔ)參數(shù),都由一個(gè)BaseModel去接收,然后將model轉(zhuǎn)map,然后再轉(zhuǎn)json,最后傳遞給服務(wù)器

class BaseModel( 
    @SerializedName("userId")
    var userId: String = "",
    @SerializedName("token")
    var token: String = ""
)

我在測(cè)試中發(fā)現(xiàn),如果我在最后請(qǐng)求服務(wù)器的時(shí)候,先去打印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的時(shí)候,get被移除了


image.png

當(dāng)時(shí)想的是,更新一下版本就可以了吧

更新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會(huì)自動(dòng)配置混淆,gson.pro文件混淆規(guī)則。

image.png

使用到最新的版本后,問題就出現(xiàn)了,我的模塊 com.gxx.http這路徑下面的所有文件都參與了混淆,文件信息都被混淆了,但是路徑依舊保留的是com.gxx.http,沒有參與到混淆,從這里,我就感覺到時(shí)混淆配置文件出了問題,
最終,我從這里發(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 類將不會(huì)被移除或改變其名稱。
2、allowobfuscation:允許該類在混淆過程中被混淆。這意味著 TypeToken 類的名稱可以被修改,但其結(jié)構(gòu)和功能保持不變。
3、allowshrinking:允許在最終構(gòu)建中刪除未使用的代碼。如果 TypeToken 不再被引用,其聲明仍然會(huì)被允許刪除,前提是其他部分的代碼不依賴于它。

我大概可以知道,如果要全部都參與到混淆里面(包括包名),那么需要所有的類+屬性+靜態(tài)內(nèi)部類,不能有任何一個(gè)滿足以上混淆配置條件的才行

解決方案

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 ----------
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容