'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 1:[1]
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
Solution 2:[2]
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.
Solution 3:[3]
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)
Solution 4:[4]
Have you tried going to File -> Invalidate Caches?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Maximiliano S. |
Solution 2 | Viktor Nordling |
Solution 3 | aaronvargas |
Solution 4 | Rick Tilley |