Search code examples
intellij-ideakotlin

Kotlin @Serializable annotation not working in IntelliJ


I'm trying to use the @Serializable annotation in Kotlin. I can build the project with Gradle, but it's showing up red in IntelliJ and when I hover on the @Serializable annotation, there's a message saying:

 kotlinx.serializable compiler plugin is not applied to the module, so this
 annotation would not be processed. Make sure you've setup your buildscript
 correctly and re-import project.

My build.gradle.kts file looks like this:

plugins {
    id("org.jetbrains.kotlin.jvm").version("1.3.50")
    id("org.jetbrains.kotlin.plugin.serialization") version "1.3.50"
    idea
}

repositories {
    jcenter()
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation(group = "com.charleskorn.kaml", name = "kaml", version = "0.12.0")

    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

I have the most recent version of the Kotlin plugin (1.3.50) installed.


Solution

  • You need to add :

    plugins {
        application
        kotlin("jvm") version "1.4.21"
        kotlin("plugin.serialization") version "1.4.21"
    }
    

    This is specified by JetBrains here, linked to gradle:

    https://github.com/Kotlin/kotlinx.serialization#setup

    https://ktor.io/docs/kotlin-serialization.html#add_dependencies