3

I’m getting a NoClassDefFoundError when trying to invoke the Foo.serializer() method on a @Serializable class.

Here's my test case:

@Serializable
data class Foo(val data: String)

val jsonString = json.stringify(
  Foo.serializer(), // <= Error happens here
  Foo(data = "foo")
)

Attempting to run the code results in the following stack trace:

java.lang.NoSuchMethodError: 'void kotlinx.serialization.internal.SerialClassDescImpl.<init>(java.lang.String, kotlinx.serialization.internal.GeneratedSerializer, int)'
    at com.example.Foo$$serializer.<clinit>(Foo.kt:7)
    at com.example.Foo$Companion.serializer(Foo.kt)
rharter
  • 2,495
  • 18
  • 34

1 Answers1

7

This is the result of version mismatches between Kotlin and Kotlinx.serialization, as they are relatively tightly coupled. In my case I was using Kotlin 1.3.71 and kotlinx.serialization 0.14.0, so the solution was to upgrade kotlinx.serialization to 0.20.0.

rharter
  • 2,495
  • 18
  • 34
  • In my case I had the same issue, to fix it I set the Kotlin version and the kotlinx.serialization version to "1.3.70" – MrPowerGamerBR May 19 '20 at 15:06
  • This seems to fix for some folks, but the issue still exists even after using 1.3.72 and 0.20.0. Make sure to generate a clean build (as a workaround for now) - https://github.com/Kotlin/kotlinx.serialization/issues/576#issuecomment-637020824 – Gurupad Mamadapur Jun 25 '20 at 08:05
  • awesome, upgrade kotlinx.serialization to 0.20.0 and Kotlin 1.3.71 fix my problem – Tony Aug 16 '20 at 03:48