下面的代碼行爲我工作正常:的ProGuard使得GSON回報LinkedTreeMap,而不是我喜歡的類型的
val users: Array<Catalog> = com.google.gson.Gson().fromJson(data, Array<MyUserClass>::class.java)
但是,當我讓ProGuard的,我得到這個錯誤:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.app.Models.MyModel
MyUserClass
如下:
data class MyUserClass(var posts: List<UserPosts>)
所以GSON正確使users
是MyUserClass
- 但不是MyUserClass
是的UserPosts
列表,它最終被的LinkedTreeMap
我一直在試圖解決現在這一段時間的列表,以及所有的答案我發現與此相關的必須與泛型有關,但我不使用泛型,我直接將該類傳遞給Gson。
我完全失去了在這一點上,因此任何指導,將apprecciated
下面是相關的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規則嗎? –
你可以試試:https://stackoverflow.com/a/30138142/1754020 –
我不是,這是我第一次與ProGuard,所以我可以搞砸了。更新回答 – Parker