Kotlinx.serialization: Serializer for class is not found even marked the class as @Serializable

Created on 29 Sep 2020  ·  10Comments  ·  Source: Kotlin/kotlinx.serialization

Describe the bug

To Reproduce
Attach a code snippet or test data if possible.

Having a data class JwtResult

@Keep
@Serializable
data class JwtResult(
        val nonce: String,
        val ctsProfileMatch: Boolean,
        val basicIntegrity: Boolean
)

and trying to parse the Json to JwtResult.class by
val returnedPayload = Json { ignoreUnknownKeys = true }.decodeFromString<JwtResult>(decodedPayload)

Exception was thrown in release build (minifyEnabled), but not debug build.
Seem the class was gone during the build process.

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

Expected behavior
successfully parsing the JSON

Environment

  • Kotlin version: 1.4.10
  • Library version: 1.4.10
  • Kotlin platforms: Android
  • Gradle version: 6.6.1
  • IDE version (if bug is related to the IDE) Android Studio 4.2 Canary 12
  • Other relevant context [e.g. OS version, JRE version, ... ]
android question

Most helpful comment

🤦‍♂️ It is fixed by adding the following the instruction about proguard from readme.md
I was expecting the @Keep annotation will handle this well.

-keepattributes Annotation, InnerClasses
-dontnote kotlinx.serialization.SerializationKt
-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(...);
}

All 10 comments

Try this:
val returnedPayload = Json { ignoreUnknownKeys = true }.decodeFromString(JwtResult.serializer(), decodedPayload)

This seems to be a proguard configuration problem. The suggested solution should fix it as it certainly makes a static reference to the serializer. However decodeFromString should be inline reified and do this too.

Yes. I believe it is an issue from R8.

Stumbled upon this one as well. Downgraded the version to 1.0.0-RC (was RC2) and the issue disappeared.

It seems to be an Android issue. Could you please provide a reproducing project with your R8 configuration?

🤦‍♂️ It is fixed by adding the following the instruction about proguard from readme.md
I was expecting the @Keep annotation will handle this well.

-keepattributes Annotation, InnerClasses
-dontnote kotlinx.serialization.SerializationKt
-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(...);
}

I had same issue that yours! Serializables data clases seemed dissapear only during build release and I got the same error.
In my case, I just added Keep anotation and try with @Arutyun2312 static serializer solution. It works perfectly!!!
Thank you very much for open this issue!! :)

Note, you will get this error if you have not added serializations to your gradle plugins section:

plugins {
kotlin("plugin.serialization") version "1.4.31"
}

I assume this can be closed now

Was this page helpful?
0 / 5 - 0 ratings