Gson.toJson() ignores @SerializedName for release build with minify option enabled

83 Views Asked by At

Model to serialize:

data class Settings(
    @SerializedName("custom_field_name_1")
    val field1: Int,

    @SerializedName("custom_field_name_2")
    val field2: Int,
   
    ...

Code to serialize:

gson.toJson(settings).also {
    Log.i("TEST", "settingsToJson $it")
}

In release build (with minifyEnabled): I get:

settingsToJson {"a":7,"b":0,"c":0,"d":"{"a":8,"b":30}","e":10,"f":1.0,"g":false,"h":1,"i":1,"j":5,"k":0,"l":false,"m":0.0}

How can I fix it?

It seems it works only for deserialization (json string to Object) but not otherwise

0

There are 0 best solutions below