Estoy recuperando un json y cuando lo convierto a List usando gson , la aplicación falla. El proguard está encendido y el problema está ahí.
fun getQuestions(): List<Question>? { val json = getQuestionsJsonData() return GsonBuilder().create().fromJson( json, object : TypeToken<List<Question>?>() {}.type ) } Como he ofuscado mi código, no puedo ver el registro de logcat crash así que lo envío a firebase crashlitycs . El mensaje de error es - Caused by java.lang.RuntimeException: Missing type parameter.
Tal vez el tipo de Question se ofusque o suceda algo similar. Mi archivo proguard:
-keepclassmembers,allowobfuscation class * { @com.google.gson.annotations.SerializedName <fields>; } -keepclassmembers class **.R$* { public static <fields>; } #Serialized -keepnames class * implements java.io.Serializable -keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; !static !transient <fields>; !private <fields>; !private <methods>; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); } # Uncomment this to preserve the line number information for # debugging stack traces. -keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. -renamesourcefileattribute SourceFile¿Tal vez tengo que agregar algo en el archivo proguard?
PD: el problema solo está en Gradle 7.1.0
Bueno, después de cambiar mi código TypeToken , parece que funciona.
Código que no funciona:
return GsonBuilder().create().fromJson( json, object : TypeToken<List<Question>?>() {}.type )Solución de trabajo:
return GsonBuilder().create().fromJson( json, TypeToken.getParameterized(List::class.java, Question::class.java).type )En mi caso, solo estaba agregando lo siguiente a la configuración de proguard:
# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher. -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeTokenAquí tienes el conjunto completo de opciones que se necesitan para Gson -> https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg