I'm retrieving a json and when I convert it to List using gson, the app crashes. The proguard is on and the problem is there.
fun getQuestions(): List<Question>? {
val json = getQuestionsJsonData()
return GsonBuilder().create().fromJson(
json,
object : TypeToken<List<Question>?>() {}.type
)
}
As I've obfuscated my code, I'm not able to see crash log in logcat, so I send it to firebase crashlitycs. The error message is - Caused by java.lang.RuntimeException: Missing type parameter.
Maybe the Question type get's obfuscated or something similar happens.
My proguard file:
-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
Maybe I have to add something in proguard file?
P.S. The problem is only on Gradle 7.1.0
In my case was just adding the following to proguard configuration:
# 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.TypeToken
Here you go the full set of options that are needed for Gson -> https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
Well, after changing my TypeToken code, seems it's working.
Non working code:
return GsonBuilder().create().fromJson(
json,
object : TypeToken<List<Question>?>() {}.type
)
Working solution:
return GsonBuilder().create().fromJson(
json,
TypeToken.getParameterized(List::class.java, Question::class.java).type
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With