首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gson更改键

Gson更改键
EN

Stack Overflow用户
提问于 2017-07-15 20:08:13
回答 2查看 772关注 0票数 3

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

问题是,如果我使用的是调试版本apk,它运行得很好,但是如果我使用的是版本apk,那么键就会发生变化。

示例:

代码语言:javascript
复制
Debug version -> "name", "Mary"
Release version -> "a", "Mary"

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

两种版本我都有护卫。

我的代码:

代码语言:javascript
复制
Gson gson = new Gson();
JsonArray jsonArray = gson.toJsonTree(myCustomList).getAsJsonArray();

分级代码:

代码语言:javascript
复制
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'
    }
}

前守卫代码:

代码语言:javascript
复制
-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.** { *; },但也遇到了同样的问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-15 22:09:32

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

票数 4
EN

Stack Overflow用户

发布于 2019-11-21 12:18:08

保持您的会员班:

代码语言:javascript
复制
class Name{

 @SerializedName("fNmae")

   private String fNmae;

}


-keepclassmembers class com.integration.Name{
private *;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45122322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档