Gson更改键名

4

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

问题是,如果我使用调试版本apk,它可以完美地工作,但是如果我使用发布版本apk,则键会改变。

例如:

Debug version -> "name", "Mary"
Release version -> "a", "Mary"

所有的键都更改为"a, b, c..."

我在两个版本中都使用了Proguard。

我的代码:

Gson gson = new Gson();
JsonArray jsonArray = gson.toJsonTree(myCustomList).getAsJsonArray();

Gradle代码:

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'
    }
}

ProGuard代码:

-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

我添加了-keep class yourPackageName.data.model.** { *; }与我的包名,但我仍然遇到了同样的问题。
2个回答

4

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


但是我也在调试版本中使用了Proguard。 - JavierSegoviaCordoba
两个也太奇怪了,我要发布Proguard设置。 - JavierSegoviaCordoba
不可能的。因为在调试版本中,你也应该重命名字段或方法。 - Fatih Santalu
我添加了更多细节。 - JavierSegoviaCordoba
“-keep class com.google.gson.examples.android.model.** { *; }” 这是错误的包名。这应该是你的包名或者你放置模型类的位置。 - Fatih Santalu
让我们在聊天中继续这个讨论。点击此处进入聊天室 - JavierSegoviaCordoba

3
保留你的成员类:
class Name{

 @SerializedName("fNmae")

   private String fNmae;

}


-keepclassmembers class com.integration.Name{
private *;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接