48

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.

8
  • Just checked your config - everything works fine. Try to refresh Gradle (in IDE), or/and restart IDE. Commented Aug 29, 2019 at 5:16
  • Hmm, which version of IntelliJ do you have? Specifically this code is red: DataClassWithSerializableAnnotation.serializer() Commented Aug 29, 2019 at 7:26
  • Also, how do you "import" the project into IntelliJ? I run ./gradlew idea and then open the generated project. Commented Aug 29, 2019 at 7:31
  • IntelliJ idea ultimate 2019.2 EAP. I have just created new project and replaced build.gradle with *.kts Commented Aug 29, 2019 at 9:44
  • Heya @MaxFarsikov, I got this working. The trick was to import the project by opening the build.gradle.kts file rather than run ./gradlew idea. Thanks for your help! Commented Sep 3, 2019 at 0:55

8 Answers 8

37

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

22

In my case I needed to add jvm plugin at project-level gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.7.20' apply false   
}

then in app gradle I added

plugins {
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.20'
}

with this configuration I am able to see:

Record.serializer()
1
  • How is this done when using kotlin instead of groovy? For example, how do you translate id 'org.jetbrains.kotlin.jvm' version '1.7.20' apply false into the current stand of kotlin?
    – SMBiggs
    Commented Aug 14 at 21:26
5

So I got this working. The problem was that I was using the idea plugin to generate the IntelliJ project. When I instead imported the project by opening the build.gradle.kts file, everything works as expected.

The reason I didn't import it that way in the first place is because I had disabled Gradle support, so when I opened the build.gradle.kts file, it didn't import the project, it just opened it in an editor. If you have this situation, hit cmd+shift+a and search for Gradle and then toggle that switch to On. After a reboot of IntelliJ you should be able to import the project from the build.gradle.kts file and you should be good to go.

4

I got this error when I tried using a version of the plugin that didn't match my Kotlin version:

plugins {
    id("org.jetbrains.kotlin.android") version "1.9.10" apply false
    // other plugins
    kotlin("plugin.serialization") version "1.9.23" apply false
}

Once I changed it to match and re-synced Gradle the issue went away.

plugins {
    id("org.jetbrains.kotlin.android") version "1.9.10" apply false
    // other plugins
    kotlin("plugin.serialization") version "1.9.10" apply false
}
3

For those who are using Kotlin’s DSL with files such as build.gradle.kts and libs.versions.toml, here is the solution:

You need to specify the information about the plugin. To do so, add the relevant information both beneath the [versions] and [plugins] entries:

[versions]
kotlinSerialization = "2.0.0"
// ...
[plugins]
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlinSerialization"}

In your module’s build.gradle.kts, add the plugin into the plugins section, as follow:

plugins {
    // ...
    alias(libs.plugins.kotlinx.serialization)
}

Having done this, I have no more warning from Android Studio.

2

If you're using Maven to build then you need

    <configuration>
        <compilerPlugins>
            <plugin>kotlinx-serialization</plugin>
        </compilerPlugins>
    </configuration>

As described here: https://github.com/Kotlin/kotlinx.serialization#maven

I had deleted that bit from my pom.xml because it compiled fine from the command line but then I hit this error under IntelliJ

1

In my case, the main and test directories were no longer 'modules' in IntelliJ. Not sure what happened...

You can verify this by going to the menu -> File -> Project Structure...

Under the modules left side navigation the main and test should be modules under the parent app. If not, the easiest way to fix is

  • Close your project
  • Delete the .idea and .gradle directories
  • Select Menu -> File -> New project from existing source... and import as a gradle project

Otherwise, you could also likely recreate your modules by going to Project structure... shown above. Then + New Module under your parent app, then select Java, with Kotlin checked as the module type. Do this for both the main and test dirs (ensure your directories are marked as 'test' source directories in the latter)

1

In my case it was missed declaration in build.gradle.kts (:app) plugins:

plugins {
    ...
    id("kotlinx-serialization")
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.