Proguard启用构建版本不适用于Gson AndroidKotlin

ukqbszuj  于 2022-11-06  发布在  Android
关注(0)|答案(1)|浏览(158)

一旦启用了proguard,我就无法在gson库的帮助下转换POJO类中的JSON字符串,我得到的是空值,但在禁用proguard时,它工作正常

Log.d(TAG, "JSON: ${StringUtils.data}")
    try {
       val quickReplyButton = Gson().fromJson(StringUtils.data, QuickReplyButton::class.java)
        Log.e(TAG, "quickReplyButtondata:" +quickReplyButton)
    }
    catch (e: Exception){
        e.printStackTrace()
    }

波若级

package com.mypackage.sdk.data.model

    data class QuickReplyButton(
        val nodes: List<Node>,
        val showMessageText: Boolean,
        val transitions: List<Transition>,
        val widgetAction: String,
        val widgetType: String,
        val widgetView: String
    )

    data class Node(
        val controls: Controls,
        val id: String
    )

    data class Transition(
        val from: String,
        val name: String,
        val to: To,
        val trigger: String
    )

    data class Controls(
        val `0`: X0
    )

    data class X0(
        val event: Event,
        val id: String,
        val text: List<String>,
        val type: String,
        val values: List<String>
    )

    data class Context(
        val border: String,
        val color: String,
        val textColor: String,
        val textStyle: String
    )

    data class Event(
        val name: String
    )

    data class To(
        val sendMessage: SendMessage
    )

    data class SendMessage(
        val displayText: String,
        val nvaaId: String,
        val nvaaType: String,
        val selected: String,
        val selectedText: String,
        val selectedValue: String
    )

程序规则:

-keep class com.mypackage.sdk.data.model.QuickReplyButton { *; }
-keep class com.mypackage.sdk.data.model.SendMessage { *; }
-keep class com.mypackage.sdk.data.model.To { *; }
-keep class com.mypackage.sdk.data.model.Event { *; }
-keep class com.mypackage.sdk.data.model.Context { *; }
-keep class com.mypackage.sdk.data.model.X0 { *; }
-keep class com.mypackage.sdk.data.model.Controls { *; }
-keep class com.mypackage.sdk.data.model.Transition { *; }
-keep class com.mypackage.sdk.data.model.Node { *; }
-keep class com.mypackage.sdk.**{ *; }

-keep class com.mypackage.sdk.ui.**{ *; }
-keep class com.mypackage.sdk.Utils.**{ *; }
-keep class com.mypackage.sdk.Care.**{ *; }
-keep class com.mypackage.sdk.CareApplication.**{ *; }

## ---------------Begin: proguard configuration for Gson  ----------

# Gson uses generic type information stored in a class file when working with fields. Proguard

# removes such information by default, so configure it to keep all of it.

-keepattributes Signature

# For using GSON @Expose annotation

-keepattributes *Annotation*

# Gson specific classes

-dontwarn sun.misc.**

# -keep class com.google.gson.stream.**{ *; }

# Application classes that will be serialized/deserialized over Gson

-keep class com.google.gson.examples.android.model.**{ *; }
-keep class com.google.gson.**{ *; }

-dontwarn java.lang.reflect.**
-keep class kotlin.**{ *; }

-keepattributes InnerClasses
-keepattributes EnclosingMethod
uqjltbpv

uqjltbpv1#

您需要将“SerializedName”注解添加到所有属性中。

@SerializedName("data")
@Expose
var data: Data? = null

相关问题