【问题标题】:Enum parsing fails with moshi and R8枚举解析失败,moshi 和 R8
【发布时间】:2021-04-14 17:30:35
【问题描述】:

我有以下依赖项:

moshi-codegen: 1.10.0
科特林:1.4.10
Android Gradle 插件: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)

根据README,我不必手动添加R8规则,但也许枚举是例外?

【问题讨论】:

  • 你能检查一下你的映射文件吗 - proguard 对你的枚举类做了什么?
  • 基本上,moshi 在这里失败了github.com/square/moshi/blob/master/moshi/src/main/java/com/…。这给我的印象是枚举不应该被混淆。我正在尝试查找 moshi proguard 配置。你能检查一下图书馆的罐子吗?

标签: moshi


【解决方案1】:

是的,应该对枚举进行不同的处理,目前有一个待处理的 PR 来更新 README 文件(截至撰写本文时)https://github.com/square/moshi/pull/1216

你有两个选择:

  1. 在枚举定义的顶部添加@JsonClass(generateAdapter = false)
  2. 添加一个 proguard 规则以保留您的枚举字段,例如,
-keepclassmembers enum your.model.package.YourEnum {
    <fields>;
    **[] values();
}

原因:

根据这里的代码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();
}

这条规则基本上适用于所有被@JsonClass注解的类,所以如果你添加注解(选项#1),你的类就会被这条规则覆盖。

或者,如果您不想添加注释,您可以专门为您的班级添加规则,也就是 Option#2。

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多