简体   繁体   English

ProGuard让Gson返回LinkedTreeMap而不是我的类型

[英]ProGuard makes Gson return LinkedTreeMap instead of my type

The following line of code works for me normally: 以下代码行通常适用于我:

val users: Array<Catalog> = com.google.gson.Gson().fromJson(data, Array<MyUserClass>::class.java)

But when I enable ProGuard, I get this error: 但是当我启用ProGuard时,我收到此错误:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.app.Models.MyModel java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换为com.example.app.Models.MyModel

MyUserClass is as follows: MyUserClass如下:

data class MyUserClass(var posts: List<UserPosts>)

So Gson properly makes users be MyUserClass - but instead of MyUserClass being a list of UserPosts , it ends up being a list of LinkedTreeMap 所以GSON妥善使得users可以MyUserClass -但不是MyUserClass是列表UserPosts ,它最终被名单LinkedTreeMap

I've been trying to solve this for a while now, and all the answers I found related to this have to do with generics, but I'm not using generics, I'm passing the class directly to Gson. 我一直试图解决这个问题一段时间了,我发现与此相关的所有答案都与泛型有关,但我没有使用泛型,我将这个类直接传递给Gson。

I'm completely lost at this point, so any guidance would be apprecciated 我现在完全迷失了,所以任何指导都会受到赞赏

Here's the related ProGuard rules: 以下是相关的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

Make sure your proguard.cfg contains all of the rules: 确保您的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM