问题描述
最近改sdk,发现我整个模块都参与了混淆,里面所有的类,都已经变成不规则的名字了,但是整个包的路径依旧还在。
一个Bug发现的问题
服务器说收不到基础参数信息,比如userId,token,我们的基础参数,都由一个BaseModel去接收,然后将model转map,然后再转json,最后传递给服务器
class BaseModel(
@SerializedName("userId")
var userId: String = "",
@SerializedName("token")
var token: String = ""
)
我在测试中发现,如果我在最后请求服务器的时候,先去打印userId信息,形如
BaseModel baseModel = new BaseModel();
baseModel.setUserId("");
baseModel.setToken("");
Log.d("Tag",baseModel.getUserId());//打印信息
String jsonStr = new GsonBuilder().create().toJson(baseModel);
服务器竟然就能收到userId的参数信息,但是无法收到token信息,从此我怀疑是打release的时候,get被移除了
当时想的是,更新一下版本就可以了吧
更新Gson版本
目前使用的Gson版本是2.10.1,使用的混淆规则为
-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版本,使用最新版的混淆规则
##---------------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文件混淆规则。
使用到最新的版本后,问题就出现了,我的模块 com.gxx.http这路径下面的所有文件都参与了混淆,文件信息都被混淆了,但是路径依旧保留的是com.gxx.http,没有参与到混淆,从这里,我就感觉到时混淆配置文件出了问题,
最终,我从这里发现了端倪
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
我的源代码里面,有使用TypeToken,再结合上面最新版本的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!!
}
再来看看,反编译
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
1、-keep:指示 ProGuard 保持指定的类及其成员不被混淆或删除。也就是说,TypeToken 类将不会被移除或改变其名称。
2、allowobfuscation:允许该类在混淆过程中被混淆。这意味着 TypeToken 类的名称可以被修改,但其结构和功能保持不变。
3、allowshrinking:允许在最终构建中删除未使用的代码。如果 TypeToken 不再被引用,其声明仍然会被允许删除,前提是其他部分的代码不依赖于它。
我大概可以知道,如果要全部都参与到混淆里面(包括包名),那么需要所有的类+属性+静态内部类,不能有任何一个满足以上混淆配置条件的才行
解决方案
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、使用如下混淆规则
##---------------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注解后,参数被移除问题
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
##---------------END: proguard configuration for Gson gxx ----------