androidkotlinserialization

Serializer for class foo is not found. Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied


This question has been here countless times, but I just can't figure this one out.

I followed the documentation and other SO answers.

// My class
@Serializable
class DSFile(val name: String, val data: ByteArray)
// build.gradle
ext {
    kotlin_version = '1.9.24'
}
dependencies {
    classpath 'com.android.tools.build:gradle:8.1.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}

// app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'kotlinx-serialization'

dependencies {
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"
}

IDE doesn't complain about anything. In runtime I get

Serializer for class DSFile is not found. Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.

I have applied the plugin and added it to my classpath. The version is the same as Kotlin version, annotation has been used in the class. What else am I missing?

Higher version of org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3 requires kotlin 2.0 and that's not an option for me.


Solution

  • The official steps didn't work for me.

    From the official documentation: (Under "Using apply plugin (the old way)")

    Then you can apply plugin (example in Groovy):

    apply plugin: 'kotlin' // or 'kotlin-multiplatform' for multiplatform projects

    apply plugin: 'kotlinx-serialization'

    Replace

    apply plugin: 'kotlinx-serialization'
    

    with

    apply plugin: 'org.jetbrains.kotlin.plugin.serialization'
    

    Then the serialization plugin works