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?

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.