Kotlinx.serialization: build.gradle.kts Gradle Kotlin DSL example

Created on 21 Nov 2018  路  12Comments  路  Source: Kotlin/kotlinx.serialization

Could you please provide a Gradle example using the Kotlin DSL? I am trying this:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.Coroutines

buildscript {
    val kotlin_version = "1.3.10"
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath(kotlin("gradle-plugin", kotlin_version))
        classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlin_version")
    }
}

plugins {
    kotlin("jvm") version "1.3.10"
    "kotlinx-serialization"
}

dependencies {
    compile(kotlin("stdlib-jdk8"))
    compile(kotlin("reflect"))
    testCompile(kotlin("test"))
    testCompile(kotlin("test-junit"))

    compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.5")
    testCompile("io.kotlintest:kotlintest-runner-junit5:3.1.7")
    compile("redis.clients:jedis:2.9.0")
    compile("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.0")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = listOf("-Xjsr305=strict")
    }
}

repositories {
    mavenCentral()
    maven (url="https://kotlin.bintray.com/kotlinx" )
}

But when I try to compile the example like JSON.stringify(Data.serializer(), Data(42)), I get the error:

e: src/main/kotlin/HelloWorld.kt: (260, 45): Unresolved reference: serializer
> Task :compileKotlin FAILED

FAILURE: Build failed with an exception.
question

Most helpful comment

Here's how I got it working:

// settings.gradle.kts

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "kotlinx-serialization") {
                useModule("org.jetbrains.kotlin:kotlin-serialization:${requested.version}")
            }
        }
    }

    repositories {
        gradlePluginPortal()
        maven("https://kotlin.bintray.com/kotlinx")
    }
}

rootProject.name = "projectName"

and then in the build.gradle.kts file I included it like this:

// build.gradle.kts

plugins {
    kotlin("jvm") version "1.3.40"
    id("kotlinx-serialization") version "1.3.40"
}

dependencies {
    ...
    implementation ("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.1")
    ...
}

All 12 comments

I suppose that plugin was not applied because you need something like id("kotlinx-serialization") version ("1.3.0") instead of just "kotlinx-serialization" line which does nothing. Consult Kotlin Gradle DSL documentation for a precise syntax of applying plugin.

I have a working multi-platform example over at reitzig/kovalscj. I imagine that a subset of that Gradle file will work for single-platform projects?

@reitzig I think this is a correct assumption

Hi,

I am also interested to have an example because it is not working straight away :(

When I try to setup kotlinx.serialization using gradle with kotlin dsl I have a gradle error :

Plugin [id: 'org.jetbrains.kotlin.kotlinx-serialization', version: '1.3.11'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.kotlin.kotlinx-serialization:org.jetbrains.kotlin.kotlinx-serialization.gradle.plugin:1.3.11')
  Searched in the following repositories:
    Gradle Central Plugin Repository

This is my configuration :

buildscript {

    val kotlinVersion: String by extra { "1.3.11" }

    repositories {
        jcenter()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion")
    }
}

plugins {
    application
    kotlin("jvm") version "1.3.11"
    kotlin("kotlinx-serialization") version "1.3.11"
}

repositories {
    jcenter()
    maven { setUrl("https://kotlin.bintray.com/kotlinx") }
}

dependencies {
    val ktorVersion = "1.1.1"

    fun ktor(module: String) = "io.ktor:ktor-$module:$ktorVersion"
    fun ktor() = "io.ktor:ktor:$ktorVersion"

    compile(kotlin("stdlib-jdk8"))
    compile(ktor())
    compile(ktor("server-netty"))
    compile(ktor("server-core"))
    compile(ktor("jackson"))
    compile(ktor("locations"))
    compile(ktor("metrics"))
    compile("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1")

}

I tries several ways to solve this issue like using
When you can鈥檛 use the plugins {} block

But I have no way to fix it.

I find something weird, if I look at the repository : https://kotlin.bintray.com/kotlinx

In the directory https://kotlin.bintray.com/kotlinx/org/jetbrains/kotlinx/, there is no kotlinx-serialization subdirectory ?

So if someone can help, it would be great ! :)

Thanks.

@herveDarritchon Have you added pluginManagement block to settings.gradle as here?

Sample from conversation above: https://github.com/reitzig/kovalscj/blob/master/settings.gradle#L1

Btw, https://github.com/Kotlin/kotlinx.serialization#gradle-with-plugins-block actually is Gradle Kotlin DSL example so I suppose this can be closed.

Here's how I got it working:

// settings.gradle.kts

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "kotlinx-serialization") {
                useModule("org.jetbrains.kotlin:kotlin-serialization:${requested.version}")
            }
        }
    }

    repositories {
        gradlePluginPortal()
        maven("https://kotlin.bintray.com/kotlinx")
    }
}

rootProject.name = "projectName"

and then in the build.gradle.kts file I included it like this:

// build.gradle.kts

plugins {
    kotlin("jvm") version "1.3.40"
    id("kotlinx-serialization") version "1.3.40"
}

dependencies {
    ...
    implementation ("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.1")
    ...
}

I tried the above and for me maven(..) in pluginManagement::repositories threw error. I have my repositories block in the module build.gradle and is now working fine.
On Android this works right now:
settings.gradle:

pluginManagement {  
    resolutionStrategy {  
        eachPlugin {  
            if (requested.id.id == "kotlinx-serialization") {  
                useModule("org.jetbrains.kotlin:kotlin-serialization:${requested.version}")  
            }  
        }  
    }  
}  
//...

module build.gradle:
```
plugins {
id("com.android.application")
id("kotlin-android")
id("kotlinx-serialization") version "1.3.11"
}

repositories {
mavenCentral()
google()
gradlePluginPortal()

maven { url "https://kotlin.bintray.com/kotlinx" }  
//etc

}

//...

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.1"
//...
}
```

Btw, https://github.com/Kotlin/kotlinx.serialization#gradle-with-plugins-block actually is Gradle Kotlin DSL example so I suppose this can be closed.

This is not Gradle Kotlin DSL - this example is in Groovy, only using the plugins block...

Please, can someone attach project sample with working kotlinx.serialization..

tnx.

Was this page helpful?
0 / 5 - 0 ratings