question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

build.gradle.kts Gradle Kotlin DSL example

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

13reactions
sandwwraithcommented, Jan 14, 2019

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

7reactions
chadmorrowcommented, Jul 5, 2019

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")
    ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Gradle Kotlin DSL Primer
Everything you can read in a Kotlin DSL script is Kotlin code compiled and executed by Gradle. Many of the objects, functions and...
Read more >
kotlin-dsl-samples/build.gradle.kts at master · gradle ... - GitHub
Samples builds using the Gradle Kotlin DSL. Contribute to gradle/kotlin-dsl-samples development by creating an account on GitHub.
Read more >
A deep dive into an initial Kotlin build.gradle.kts - Medium
This is the third article in a series about building a Kotlin project with Gradle configured using the Kotlin DSL. In the previous...
Read more >
Kotlin DSL for Gradle - Baeldung
2. How to Create a Kotlin DSL Script ·.gradle.kts instead of .gradle for our build scripts ·.settings.gradle.kts instead of .settings.gradle for ...
Read more >
The New Way of Writing Build Gradle with Kotlin DSL
the basic thing when you change Gradle using Kotlin script is the use of single-quotes to double-quotes. For minSdkVersion and targetSdkVersion is function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found