我有以下依赖项:
moshi-codegen: 1.10.0
kotlin: 1.4.10
Android Gradle Plugin: 4.0.1
R8 在构建中启用。
在运行时,当 Moshi 尝试解析枚举时,我得到以下堆栈跟踪
java.lang.AssertionError: Missing field in e.f.a.k.c.b.a
at com.squareup.moshi.StandardJsonAdapters$EnumJsonAdapter.<init>(SourceFile:246)
at com.squareup.moshi.StandardJsonAdapters$1.create(SourceFile:67)
at com.squareup.moshi.Moshi.adapter(SourceFile:141)
at com.tsystems.tpay.data.client.models.ContactApiModelJsonAdapter.<init>(SourceFile:30)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at com.squareup.moshi.internal.Util.generatedAdapter(SourceFile:553)
at com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory.create(SourceFile:193)
at com.squareup.moshi.Moshi.adapter(SourceFile:141)
at com.squareup.moshi.Moshi.adapter(SourceFile:101)
at com.squareup.moshi.Moshi.adapter(SourceFile:71)
at com.squareup.moshi.CollectionJsonAdapter.newArrayListAdapter(SourceFile:52)
at com.squareup.moshi.CollectionJsonAdapter$1.create(SourceFile:36)
at com.squareup.moshi.Moshi.adapter(SourceFile:141)
at com.squareup.moshi.Moshi.adapter(SourceFile:101)
at p.z.a.a.a(SourceFile:91)
at p.u.a(SourceFile:352)
at p.u.b(SourceFile:335)
at p.k.a(SourceFile:113)
at p.k.a(SourceFile:82)
at p.v.a(SourceFile:37)
at p.u.a(SourceFile:192)
at p.u$a.invoke(SourceFile:149)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy14.c(Unknown Source)
at e.f.a.k.b.f$g.a(SourceFile:87)
at i.b0.j.a.a.b(SourceFile:33)
at j.a.v0.run(SourceFile:241)
at j.a.g3.a.a(SourceFile:594)
at j.a.g3.a.a(SourceFile:60)
at j.a.g3.a$b.run(SourceFile:740)
Caused by: java.lang.NoSuchFieldException: PERSONAL
at java.lang.Class.getField(Class.java:1604)
at com.squareup.moshi.StandardJsonAdapters$EnumJsonAdapter.<init>(SourceFile:240)
Run Code Online (Sandbox Code Playgroud)
根据自述文件,我不必手动添加 R8 规则,但也许枚举是例外?
是的 enum 应该有一点不同的处理方式,目前有一个待定的 PR 来更新自述文件(截至撰写本文时)https://github.com/square/moshi/pull/1216。
您有 2 个选择:
@JsonClass(generateAdapter = false)在您的枚举定义之上。-keepclassmembers enum your.model.package.YourEnum {
<fields>;
**[] values();
}
Run Code Online (Sandbox Code Playgroud)
原因:
根据这里的代码https://github.com/square/moshi/blob/0c85eae34af00ecbee46beaa5b25fb4af00fb9f2/moshi/src/main/resources/META-INF/proguard/moshi.pro#L10,集成的EnumJsonAdapter. 也values()由 Kotlin 编译器合成并被EnumJsonAdapter间接使用。
还要注意在同一个文件中,Moshi 生成了一个预制的 proguard 规则,该规则由您的应用程序继承:
-keepclassmembers @com.squareup.moshi.JsonClass class * extends java.lang.Enum {
<fields>;
**[] values();
}
Run Code Online (Sandbox Code Playgroud)
This rule is basically applied to all classes annotated by the @JsonClass annotation, so if you add the annotation (Option#1), your class would be covered by this rule.
Alternatively if you don't want adding the annotation, you can add the rule specifically for your class aka Option#2.
| 归档时间: |
|
| 查看次数: |
673 次 |
| 最近记录: |