android - 由 java.lang.RuntimeException : Missing type parameter 引起

标签 android kotlin gson proguard typetoken

我正在检索 json当我使用 gson 将其转换为列表时,应用程序崩溃。 proguard开启,问题就在那里。

fun getQuestions(): List<Question>? {
    val json = getQuestionsJsonData()
    return GsonBuilder().create().fromJson(
        json,
        object : TypeToken<List<Question>?>() {}.type
    )
}
由于我混淆了我的代码,我无法看到 crash登录 logcat ,所以我把它发送到firebase crashlitycs .错误消息是 - Caused by java.lang.RuntimeException: Missing type parameter.也许 Question输入 get 被混淆或发生类似的事情。
我的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
也许我必须在 proguard 文件中添加一些东西?
附言问题仅在 Gradle 7.1.0

最佳答案

在我的情况下,只是将以下内容添加到 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.TypeToken
这是 Gson 所需的全套选项 -> https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

关于android - 由 java.lang.RuntimeException : Missing type parameter 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70969756/

相关文章:

java - gson:使用复合键反序列化 json

java - 如何确保我的线程/进程在不同的内核上运行

android - Detox - DetoxManager 在运行时找不到 kotlin/jvm/functions/Function1

android - 将 Lazy Delegate 与 Gson 一起使用时,Kotlin 抛出 NPE

java - Espresso 测试android中的多场比赛

java - Android MotionLayout : How to handle both click and swipe events on same view?(包括视频示例)

java - 使用 Google 的 Gson 进行严格的 JSON 解析?

android - 更改抽屉导航的字体,所以试图找到 textview

android - 无法启动 Activity ,setOnClickListener

android - 如何更改 Textview 上的文本