2017-07-14 33 views
1

下面的代碼行爲我工作正常:的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正確使usersMyUserClass - 但不是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 
+1

你確定你已經設置了所有的proguard規則嗎? –

+1

你可以試試:https://stackoverflow.com/a/30138142/1754020 –

+0

我不是,這是我第一次與ProGuard,所以我可以搞砸了。更新回答 – Parker

回答

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 
+0

再次感謝我讓我檢查規則,它最終成爲我的一個錯字,但它仍然有幫助! – Parker

+0

沒問題,謝謝接受:)祝你好運! –

相關問題