1

I can't use Kotlinx serialization with the Kotlin JVM plugin

In the instructions for Groovy DSL:

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

Because I already use the org.jetbrains.kotlin.jvm, I want to use it with the JVM plugin but the instructions do not explicitly show how.

In build.gradle, I tried using:

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

In code:

import kotlinx.serialization.*

And, I get a compilation error (Unresolved reference: serialization). How can I use kotlinx serialization with the JVM plugin? Also, I need it in the Groovy DSL syntax. The instructions already show it for the Kotlin DSL but I don't use it. Can anyone help?

Alperen
  • 3,772
  • 3
  • 27
  • 49

1 Answers1

4

The plugin is not sufficient to use Kotlinx Serialization, you also need the runtime library to use classes from the kotlinx.serialization.* packages. This is covered in the docs: https://github.com/Kotlin/kotlinx.serialization#dependency-on-the-json-library

In Gradle, this means you need to add Kotlinx serialization as a dependency in the dependencies block:

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.1"
}

If you need other formats than JSON, you'll need to add the corresponding artifact instead.

Joffrey
  • 32,348
  • 6
  • 68
  • 100