android - Moshi LinkedHashTreeMap 类使用 Proguard 抛出异常

标签 android retrofit proguard moshi

启用 proguard 后,在使用改造的 API 调用后,我收到导致此错误的 LinkedHashMapTree 列表:

java.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap cannot be cast to 
 ...response.SearchItemResponse

禁用混淆器后,不会发生崩溃,并且列表的类型为 List 而不是 List

我的 proguard 文件:

 -keepclassmembers public class * extends androidx.lifecycle.ViewModel { public <init>(...); }
 -keep,allowobfuscation,allowshrinking interface retrofit2.Call
 -keep,allowobfuscation,allowshrinking class retrofit2.Response
 -keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation
 -keep, allowobfuscation,allowshrinking class com.squareup.moshi.JsonAdapter

我在混淆器规则中遗漏了什么吗?

最佳答案

通过在数据类及其子类上使用 @Keep 注释解决了该问题。

来自@Keep的文档:

Denotes that the annotated element should not be removed when the code is minified at build time. This is typically used on methods and classes that are accessed only via reflection so a compiler may think that the code is unused.

关于android - Moshi LinkedHashTreeMap 类使用 Proguard 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76782249/

相关文章:

java - 如何确定我的目标 SDK 需要哪个 Android Gradle 插件版本?

java - 在android中,如何备份java类的内容并恢复它

java - 改造不适用于特定版本的 android

android - 在 Android App 中将 Retrofit Adapter 放在哪里?

android - Retrofit 1.9 - 相同的变量名,不同的 JSON 类型 - CONVERSION 异常

Android Proguard Release导致不执行for-loop

flutter - 我的apk重150 mb,有什么办法可以减少这种情况?

android - 如何获取 Android 平台代号,如 Honeycomb

Android 和 SQlite - 缺少一列?

android - 带有 proguard 问题的发布版本 - GSON