Для меня обычно работает следующая строка кода:
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 правильно делает users
MyUserClass
но вместо MyUserClass
является списком UserPosts
, он становится списком LinkedTreeMap
Я пытался решить это некоторое время сейчас, и все ответы, которые я нашел связанным с этим, связаны с генериками, но я не использую generics, я передаю класс непосредственно 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
Убедитесь, что ваш 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