Andy
Andy

Reputation: 810

Serializer for data class Kotlin not found at build release

I want convert my json string response from API to object:

val obj = Json.decodeFromString<MyModel>(jsonResponseString)

My data class:

@Serializable
data class MyModel(
    @SerializedName("field") val field: String
)

It look like very simple and it works on debug mode!

But when a compiled the AppBundle, builded in release mode and download app from Play Store internal testing, I got the following error :

Serializer for class '...' is not found. Mark the class as @serializable or provide the 
serializer explicitly.
kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered

Upvotes: 12

Views: 7845

Answers (4)

MakinTosH
MakinTosH

Reputation: 642

Starting from 1.5.0-RC proguard rules are shipped with library.

Bundled Proguard rules The kotlinx-serialization-core-jvm JAR file now includes consumer Proguard >rules, so manual Proguard configuration is no longer necessary for most of the setups.

Upvotes: 1

Saeed Ir
Saeed Ir

Reputation: 2332

I have fixed that with these changes in the Gradle files, in the build project gradle add this line to dependencies:

classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.21"

Add also these to the build app gradle:

plugins {
    ...
    id 'kotlinx-serialization'
}
dependencies {
    ...
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2'
}

Please note version numbers might be different.

Upvotes: 1

Rosenpin
Rosenpin

Reputation: 872

You should add this to your proguard.pro if you're using minifyEnabled true

-keepattributes *Annotation*, InnerClasses
-dontnote kotlinx.serialization.AnnotationsKt # core serialization annotations

# kotlinx-serialization-json specific. Add this if you have java.lang.NoClassDefFoundError kotlinx.serialization.json.JsonObjectSerializer
-keepclassmembers class kotlinx.serialization.json.** {
    *** Companion;
}
-keepclasseswithmembers class kotlinx.serialization.json.** {
    kotlinx.serialization.KSerializer serializer(...);
}

# Change here com.yourcompany.yourpackage
-keep,includedescriptorclasses class com.yourcompany.yourpackage.**$$serializer { *; } # <-- change package name to your app's
-keepclassmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's
    *** Companion;
}
-keepclasseswithmembers class com.yourcompany.yourpackage.** { # <-- change package name to your app's
    kotlinx.serialization.KSerializer serializer(...);
}

Make sure you replace the placeholder package name with your app package name

Source

Upvotes: 14

Andy
Andy

Reputation: 810

I found the next solution:

First step, I added @Keep anotation. Keep anotation denotes that the annotated element should not be removed when the code is minified at build time:

@Keep
@Serializable
data class MyModel(
    @SerializedName("field") val field: String
)

Second step, I converted my json to object making a static reference to the serializer:

val objError = Json {ignoreUnknownKeys = true}.decodeFromString(MyModel.serializer(), jsonResponseString)

Dont forget import and implement last version of:

'org.jetbrains.kotlin.plugin.serialization'

And it worked and it save my day!!

Upvotes: 9

Related Questions