ProGuard 让 Gson 返回 LinkedTreeMap 而不是我的类型
Posted
技术标签:
【中文标题】ProGuard 让 Gson 返回 LinkedTreeMap 而不是我的类型【英文标题】:ProGuard makes Gson return LinkedTreeMap instead of my type 【发布时间】:2017-12-19 16:40:11 【问题描述】:以下代码行对我正常工作:
val users: Array<Catalog> = com.google.gson.Gson().fromJson(data, Array<MyUserClass>::class.java)
但是当我启用 ProGuard 时,我得到了这个错误:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为 com.example.app.Models.MyModel
MyUserClass
如下:
data class MyUserClass(var posts: List<UserPosts>)
所以 Gson 正确地使 users
成为 MyUserClass
- 但不是 MyUserClass
是 UserPosts
的列表,它最终是 LinkedTreeMap
的列表
我一直在尝试解决这个问题,我找到的所有与此相关的答案都与泛型有关,但我没有使用泛型,我将类直接传递给 Gson。
此时我完全迷失了,所以任何指导都将不胜感激
以下是相关的 ProGuard 规则:
##---------------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.** *;
-keep class com.example.Models.** *;
# 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
##---------------End: proguard configuration for Gson ----------
-keep public class MyUserClass
-keep public class UserPosts
【问题讨论】:
你确定你设置了所有的proguard规则吗? 你可以试试:***.com/a/30138142/1754020 我不是,这是我第一次使用 ProGuard,所以我可能搞砸了。更新答案 哦,伙计...我有-keep class com.example.Models.** *;
而不是 -keep class com.example.app.Models.** *;
谢谢你们提到 ProGuard 规则!否则我不会再看它们了:)
【参考方案1】:
确保您的 proguard.cfg 包含所有规则:
##---------------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
##---------------End: proguard configuration for Gson ----------
-keep public class MyUserClass
-keep public class UserPosts
【讨论】:
再次感谢您让我检查规则,这最终是我的一个错字,但它仍然有帮助!以上是关于ProGuard 让 Gson 返回 LinkedTreeMap 而不是我的类型的主要内容,如果未能解决你的问题,请参考以下文章
Gson fromJson 在 Proguard 之后返回 null