How to define the Kotlin version for both buildSrc and an application module?
Asked Answered
P

3

10

I am using a buildSrc module in a multi-module Kotlin project to manage dependency definitions and versions. The module makes use of kotlin-dsl as shown in the build.gradle.kts:

plugins {
    `kotlin-dsl`
}

Alternative declaration:

plugins {
    id("org.gradle.kotlin.kotlin-dsl") version "0.16.2"
}

I would like to use the same Kotlin version for compiling the buildSrc module as well as within the application module/s. My first attempt was to simply add the JVM artifact:

plugins {
    `kotlin-dsl`
    kotlin("jvm") version "1.2.31"
}

This however leads to a build error which is discussed here:

Error resolving plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.2.31']
Plugin request for plugin already on the classpath must not include a version

What is a convenient way to only define once the Kotlin version used throughout the project?

Related

Paulpaula answered 27/3, 2018 at 20:50 Comment(0)
P
3

Each Gradle release is meant to be used with a specific version of the kotlin-dsl plugin and compatibility between arbitrary Gradle releases and kotlin-dsl plugin versions is not guaranteed.

Using an unexpected version of the kotlin-dsl plugin in a build can cause hard to diagnose problems.

Starting with Gradle 5.4, a warning is emitted whenever an unexpected version of the kotlin-dsl plugin is detected.

Paul Merlin @eskatos, 25.04.2019

Therefore, I removed the version:

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}
Paulpaula answered 26/4, 2019 at 9:43 Comment(1)
This makes sense, BUT it also forces my application to use that version which is plain ridiculous.Cruickshank
A
3

The only way I'm aware of is putting it into gradle.properties (or any other config) and reading it in settings.gradle.kts pluginManagement. Like this:

pluginManagement {
    repositories {
        gradlePluginPortal()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id.startsWith("org.jetbrains.kotlin")) {
                useVersion(gradle.rootProject.extra["kotlin.version"] as String)
            }
        }
    }
}
Atalie answered 9/4, 2018 at 22:12 Comment(0)
P
3

Each Gradle release is meant to be used with a specific version of the kotlin-dsl plugin and compatibility between arbitrary Gradle releases and kotlin-dsl plugin versions is not guaranteed.

Using an unexpected version of the kotlin-dsl plugin in a build can cause hard to diagnose problems.

Starting with Gradle 5.4, a warning is emitted whenever an unexpected version of the kotlin-dsl plugin is detected.

Paul Merlin @eskatos, 25.04.2019

Therefore, I removed the version:

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}
Paulpaula answered 26/4, 2019 at 9:43 Comment(1)
This makes sense, BUT it also forces my application to use that version which is plain ridiculous.Cruickshank
V
0

An error speaks for itself: you can't specify version of plugin in plugins folder if you already specified it in buildscript block.

You can put kotlinVersion into gradle.properties and symlink buildSrc/gradle.propeties to ./gradle.properties

Vardar answered 10/4, 2018 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.