Search code examples
kotlinkotlinx.serializationkotlinx

Receiving NoClassDefFoundError when invoking generated serializer() method


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)

Solution

  • 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.