2017-07-14 1 views
1

のLinkedTreeMapを返します:ProGuardのはGsonは、次のコード行は、通常、私の作品の代わりに私のタイプ

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なります - しかし、MyUserClassUserPostsのリストされる代わりに、それは私が今しばらくの間、この問題を解決しようとしてきたLinkedTreeMap

のリストされて終わると、すべての答えIこれに関連するのはジェネリックスと関係がありますが、私はジェネリックスを使用していないので、クラスを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

すべてのプロガイドルールが設定されていますか? –

+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

問題ありません、受け入れていただきありがとうございます:)幸運! –

関連する問題