【问题标题】:Gson Crashing Enum Fields When Enabling Proguard启用 Proguard 时 Gson 崩溃枚举字段
【发布时间】:2019-09-05 13:10:47
【问题描述】:

就我而言

启用 Proguard 时,Proguard Gson 在枚举字段中崩溃。

我的整个代码都在 kotlin 中。我通过将枚举保留在下面的 proguard 规则中得到了解决方案。

-keepclassmembers enum * {
   public *;
}

但我想将 proguard 应用于枚举,那么我该怎么做呢?下面是我的代码。

data class Notification(
        @SerializedName("createdate")
        val createdate: String = "",
        @SerializedName("lastupdate")
        val lastupdate: String? = null,
        @SerializedName("member_id")
        val memberId: Int = 0,
        @SerializedName("notification_source")
        val notificationSource: PushType,
    ): Serializable

枚举类

enum class PushType {
   Calendar,
   Reminder,
   Friend,
   Timeline,
   Savings,
   Email,
   MemoryBox,
   InstantMessage;
}

我也试过下面的枚举

enum class PushType {
   @SerializedName("Calendar")
   Calendar,
   @SerializedName("Reminder")
   Reminder,
   @SerializedName("Friend")
   Friend,
   @SerializedName("Timeline")
   Timeline,
   @SerializedName("Savings")
   Savings,
   @SerializedName("Email")
   Email,
   @SerializedName("MemoryBox")
   MemoryBox,
   @SerializedName("InstantMessage")
   InstantMessage;
 }

我尝试了以下 2 个解决方案

  1. https://medium.com/@hanru.yeh/gson-will-crash-at-enum-fields-in-custom-class-with-proguard-bbcf5ad1b623

  2. https://medium.com/@hanru.yeh/custom-typeadapterfactory-for-enum-ignores-annotation-serializedname-3a3be550b6a8

在尝试上述解决方案崩溃问题修复但再次尝试时 获取详细信息,它给了我以下错误。

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int java.lang.Enum.ordinal()”

【问题讨论】:

  • 您是在尝试使用签名 apk 还是调试 apk?
  • 我已经用签名的apk试过了
  • 发布您的proguard-rules.pro

标签: android kotlin enums proguard


【解决方案1】:

将“notification_source”的数据类型更改为字符串并像这样使用枚举

@StringDef(Calendar, Reminder, Friend, Timeline, Savings, Email, MemoryBox, InstantMessage)
@Retention(AnnotationRetention.SOURCE)
annotation class PushType {
    companion object {
        const val Calendar = "Calendar"
        const val Reminder = "Reminder"
        const val Friend = "Friend"
        const val Timeline = "Timeline"
        const val Savings = "Savings"
        const val Email = "Email"
        const val MemoryBox = "MemoryBox"
        const val InstantMessage = "InstantMessage"
    }
}

【讨论】:

    猜你喜欢
    • 2015-03-31
    • 2013-04-21
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 2022-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多