By using new AGP v8.0 and full R8 enabled, when I'm trying to deserialize a nested object from a JSON string, the type of nested object is incorrectly identified as LinkedTreeMap by gson.
actions_list.json:
[
{
"name": "actionList1",
"listA": [
{
"name": "action1"
},
{
"name": "action2"
}
] // end of listA
}, // end of actionList1
... // actionList1 to actionListN
]
Action class:
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
and this how i converts json array to 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 )
When minifyEnabled is true in app build.gradle, List is converted to LinkedTreeMap. I have too many nested object, is there any way that i fix this without Custom Desrializer class, TypeConverters, disable full r8 mode and downgrade gradle version?
Adding these lines to the proguard-rules.pro fixed the problem:
-keepclassmembernames class path.to.MyActions {<fields>;}
-keepclassmembernames class path.to.Action{<fields>;}