proguard使gson返回linkedtreemap而不是我的类型

2q5ifsrm  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(481)

以下代码行通常适用于我: 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做得很好 usersMyUserClass -但不是 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
hgncfbus

hgncfbus1#

确保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

相关问题