英文:
Moshi LinkedHashTreeMap class cast exception with Proguard
问题
当启用了ProGuard后,在使用Retrofit进行API调用后,我收到了一个LinkedHashTreeMap列表,导致了这个错误:
java.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap无法强制转换为...response.SearchItemResponse
在禁用ProGuard时,崩溃不会发生,列表的类型是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
我是否忽略了ProGuard规则中的某些内容?
英文:
When proguard is enabled, after an API call with retrofit, I recieve a list of LinkedHashMapTree that causes this error:
java.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap cannot be cast to
...response.SearchItemResponse
with proguard disabled the crash doesn't happen and the list is of type List<SearchItemResponse> instead of List<LinkedHashTreeMap>
My proguard file:
-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
Is there something I'm missing on the proguard rules?
答案1
得分: 1
问题通过在数据类及其子类上使用@Keep注释来解决。
来自@Keep的文档:
表示在构建时进行代码缩小时,不应删除带注释的元素。通常用于只通过反射访问的方法和类,以便编译器不会认为代码未使用。
英文:
The problem is resolved by using the @Keep annotation on the data class and it's sublcasses.
From the doc of @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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论