通过使用新的AGP v8.0和完全启用的R8,当我试图从JSON字符串反序列化嵌套对象时,嵌套对象的类型被gson错误地标识为LinkedTreeMap。
actions_list.json:
[
{
"name": "actionList1",
"listA": [
{
"name": "action1"
},
{
"name": "action2"
}
] // end of listA
}, // end of actionList1
... // actionList1 to actionListN
]
行动类别:
data class Action(
@SerializedName("name") @Expose val name: String,
val actions: List<MyAction>,
var a: Int,
...
var n: Boolean,
)
MyActions.kt:
data class MyActions(
@SerializedName("name")
val name: String,
var state: MyActionState?
)
MyActionState.kt:
enum class MyActionState{
Nothing, Running, Done, Failed, Deactivate
}
proguard-rules.pro:
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
-dontwarn javax.annotation.**
-dontwarn kotlin.Unit
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*
-dontwarn okhttp3.**
-keep class okhttp3.** { *;}
-dontwarn okio.**
-keepattributes SourceFile,LineNumberTable
-keep class com.google.gson.*
-keep interface com.google.gson.*
-keep class * extends com.google.gson.reflect.TypeToken
-keep public class * implements java.lang.reflect.Type
-keep class path.to.all.app.class
-keep interface path.to.all.app.class
-keep enum path.to.all.app.class
-keepattributes *Annotation*
-keepattributes AnnotationDefault
-keepattributes Signature
下面是我如何将json数组转换为List:
val jsonString: String = this.assets.open("actions/actions_list.json")
.bufferedReader()
.use { it.readText() }
val type = TypeToken.getParameterized(List::class.java,Action::class.java).type
return Gson().fromJson(jsonString, type )
当app build.gradle中的minifyEnabled为true时,List将转换为LinkedTreeMap。我有太多的嵌套对象,有没有什么方法可以解决这个问题,而不需要自定义Desrializer类,TypeConverters,禁用完整的r8模式并降级gradle版本?
1条答案
按热度按时间kd3sttzy1#
将这些行添加到proguard-rules.pro解决了这个问题: