Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 628
Viktor Nordling Avatar asked Aug 29 '19 01:08

Viktor Nordling


3 Answers

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

like image 196
Maximiliano S. Avatar answered Oct 18 '22 07:10

Maximiliano S.


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.

like image 5
Viktor Nordling Avatar answered Oct 18 '22 07:10

Viktor Nordling


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()
like image 2
Nicola Gallazzi Avatar answered Oct 18 '22 08:10

Nicola Gallazzi