I have a simple usage of Serializable on a data class
@Serializable
data class Message(
val type: String,
val topics: List<String>,
val content: String
)
but the IDE shows the following warning
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.
At runtime I receive
Serializer for class 'Message' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
I've read countless other StackOverflow posts where the issue is incorrect setup in their build.gradle.kts, but I'm triple checked that I'm importing the plugin and dependency. Below is my build.gradle.kts
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
plugins {
kotlin("jvm") version "1.9.0"
kotlin("plugin.serialization") version "1.9.0"
id("io.ktor.plugin") version "2.3.2"
}
group = "com.ryandyoon"
version = "0.0.1"
application {
mainClass.set("com.ryandyoon.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("io.ktor:ktor-server-websockets:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
If it makes a difference, yes I am using this in a ktor project, but I'm simply calling
Json.decodeFromString<Message>(messageString)
I was able to resolve the warning "kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project." by adding the serialization plugin in the project-level build.gradle.kts, and then applying it in the module-level build.gradle.kts.
Project build.gradle.kts:
plugins {
...
kotlin("plugin.serialization") version "1.9.0" apply false
}
Module build.gradle.kts:
plugins {
...
kotlin("plugin.serialization")
}
...
dependencies {
...
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With