Gson更改密钥

Jav*_*oba 5 arrays android json gson

我有一个自定义列表,我正在使用 Gson 转换为 jsonArray。

问题是,如果我使用调试版本 apk,它工作正常,但如果我使用发布版本 apk,密钥会发生变化。

例子:

Debug version -> "name", "Mary"
Release version -> "a", "Mary"
Run Code Online (Sandbox Code Playgroud)

所有键都变为“a、b、c...”

我在两个版本中都有 proguard。

我的代码:

Gson gson = new Gson();
JsonArray jsonArray = gson.toJsonTree(myCustomList).getAsJsonArray();
Run Code Online (Sandbox Code Playgroud)

等级代码:

buildTypes {
    release {
        debuggable true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    }
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
Run Code Online (Sandbox Code Playgroud)

混淆器代码:

-dontwarn okhttp3.**
-dontwarn okio.**

##---------------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
Run Code Online (Sandbox Code Playgroud)

我添加了-keep class yourPackageName.data.model.** { *; } 我的包名称,但遇到了同样的问题。

san*_*alu 5

我猜你的调试版本没有启用 minify。因此,当发生混淆时,您需要保留模型类。此示例旨在将所有模型类保留在指定包内-keep class yourPackageName.data.model.** { *; }