Moshi LinkedHashTreeMap类转换异常与Proguard

huangapple go评论56阅读模式
英文:

Moshi LinkedHashTreeMap class cast exception with Proguard

问题

当启用了ProGuard后,在使用Retrofit进行API调用后,我收到了一个LinkedHashTreeMap列表,导致了这个错误:

java.lang.ClassCastException: com.squareup.moshi.LinkedHashTreeMap无法强制转换为...response.SearchItemResponse

在禁用ProGuard时,崩溃不会发生,列表的类型是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

我是否忽略了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&lt;SearchItemResponse> instead of List&lt;LinkedHashTreeMap>

My proguard file:

 -keepclassmembers public class * extends androidx.lifecycle.ViewModel { public &lt;init&gt;(...); }
 -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.

huangapple
  • 本文由 发表于 2023年7月28日 01:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定