I want to use kotlin serialization in the build script itself (build.gradle.kts).
So I added the plugin to the “plugins” block:
val kotlinVersion = “1.5.31”
kotlin(“jvm”) version kotlinVersion
kotlin(“plugin.serialization”) version kotlinVersion
I also added the dependency in “buildscript” block:
classpath(group = “com.typesafe”, name = “config”, version = “1.4.1”)
classpath(group = “org.jetbrains.kotlinx”, name = “kotlinx-serialization-hocon”, version = “1.3.0”)
Then I defined my data classes and I use “@Serializable” annotation on them.
Then I try to extract configuration like this:
val typesafeConf = com.typesafe.config.ConfigFactory.parseFile(File(“src/env.conf”))
val envConf = Hocon.decodeFromConfig(typesafeConf)
The “decodeFromConfig” fails with the following error:
Serializer for class ‘Configuration’ is not found.
Mark the class as @Serializable or provide the serializer explicitly.
But I do use @Serializable. When I hover the mouse over it, I see this:
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.
So the problem is, the plugin is not applied to the buildscript itself.
I tried to put the “apply” inside the buildscript block, could not make it work. It doesn’t like the “plugins” block there; the “apply” statements could not find the plugin anywhere.
How can I make it work?