androidkotlingsonproguardtypetoken

Caused by java.lang.RuntimeException: Missing type parameter


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


Solution

  • 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
    )