Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer for class is not found even marked the class as @Serializable #1105

Closed
seventhmoon opened this issue Sep 29, 2020 · 13 comments
Closed

Comments

@seventhmoon
Copy link

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, ... ]
@Arutyun2312
Copy link

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

@pdvrieze
Copy link
Contributor

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.

@seventhmoon
Copy link
Author

seventhmoon commented Sep 29, 2020

Yes. I believe it is an issue from R8.

@webfrea-k
Copy link

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

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Oct 1, 2020

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

@seventhmoon
Copy link
Author

https://github.com/seventhmoon/hello-kotlin-serialization

@seventhmoon
Copy link
Author

🤦‍♂️ 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(...);
}

@atorrico09
Copy link

atorrico09 commented Dec 24, 2020

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!! :)

@gandrewstone
Copy link

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"
}

@sandwwraith
Copy link
Member

I assume this can be closed now

@p4ulor
Copy link

p4ulor commented Oct 29, 2021

For anyone having issues using this in android studio, check gradle website
Get the latest version and use " instead of '
Like:
id "org.jetbrains.kotlin.plugin.serialization" version "1.6.0-RC"
This is the fundamental line of code you need to add to your plugins { } in the file build.gradle(:app) to fix this problem.

Also make sure you have, on the same file, in dependencies { } the line:
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0'

And remove comments (specially if they have links) near the build statements. That can interfere according to my tests.

Make sure you have the imports in the .kt files:
import kotlinx.serialization.*
import kotlinx.serialization.json.Json //if you're using json

IDK if this is also part of the problem, but also make sure you have this in your proguard-rules.pro file (which is part of the gradle scripts)
https://github.com/Kotlin/kotlinx.serialization#android

Finally, try using other grade JDK's by going to gradle settings. It's probably best to use Android's. And consider doing invalidate caches and restart. I've been trying on this issue for 5 hours. Yes.

@jmarks213
Copy link

jmarks213 commented Dec 19, 2021

I ran into this issue recently after I had refactored my android apps' package in a kotlin multiplatform project. I needed to add rules to keep the serializers from both the androidApp package and the core library. My proguard looks something like:

-keepattributes Annotation, InnerClasses
-dontnote kotlinx.serialization.SerializationKt

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

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

@cyb3rko
Copy link

cyb3rko commented May 14, 2022

For those where the solutions above were not working.
Here's what I used (taken from this issue here and StreetComplete):

-keepattributes Annotation, InnerClasses
-dontnote kotlinx.serialization.AnnotationsKt
-dontnote kotlinx.serialization.SerializationKt

# Keep Serializers

-keep,includedescriptorclasses class com.company.package.**$$serializer { *; }
-keepclassmembers class com.company.package.** {
    *** Companion;
}
-keepclasseswithmembers class com.company.package.** {
    kotlinx.serialization.KSerializer serializer(...);
}

# When kotlinx.serialization.json.JsonObjectSerializer occurs

-keepclassmembers class kotlinx.serialization.json.** {
    *** Companion;
}
-keepclasseswithmembers class kotlinx.serialization.json.** {
    kotlinx.serialization.KSerializer serializer(...);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests