0

I have a simple Gradle project that includes the following configuration:

plugins {
    kotlin("jvm")
}

This does different things when running with different gradle versions:

$ ./gradlew wrapper --gradle-version=7.5
$ ./gradlew --version
Kotlin:       1.6.21

$ ./gradlew wrapper --gradle-version=7.4.2
$ ./gradlew --version
Kotlin:       1.5.31

$ ./gradlew wrapper --gradle-version=7.2
$ ./gradlew --version
Kotlin:       1.5.21

OK, so Gradle is determining the version of the Kotlin plugin. Bonus questions: Why does Gradle do this? Is this version link documented somewhere?

I'd like to specify the Kotlin version myself. So I change my config:

plugins {
    kotlin("jvm") version "1.6.10"
}

Now Gradle complains:

Plugin request for plugin already on the classpath must not include a version

I've checked, and nowhere else in my project do I define a Gradle version explicitly.

Main question: How do I make Gradle use a Kotlin version of my choice?

Jorn
  • 20,612
  • 18
  • 79
  • 126

1 Answers1

2

From my experience the below configuration works fine for a single-project Gradle project with most gradle version.

plugins {
    kotlin("jvm") version "1.6.10"
}

Example: https://github.com/jimmyiu/demo-spring-cache. I changed the kotlin version from 1.6.21 to 1.7.10 which works fine.


Are you working on a multi-project Gradle project with buildSrc folder? Since buildSrc is a special subproject which build your custom kotlin-dsl which can be used by Gradle.

This can answer your bonus question: for simplicity, buildSrc should be built to something that can be understood by your project specified Gradle version, hence it must have the same kotlin version as your selected Gradle.

If you root project set the Kotlin version to a version that not align with your Gradle, buildSrc project cannot be built.

To properly configure this, you can:

// root project’s build.gradle.kts
plugins {
    kotlin("jvm") version "1.6.10" apply false
}

// in each subproject (except buildSrc):
plugins {
    java
    kotlin("jvm")
}

About the output of ./gradlew --version, the Kotlin version showing in the output is the build information of that Gradle version, but not your project.

$./gradlew --version

------------------------------------------------------------
Gradle 6.6.1
------------------------------------------------------------

Build time:   2020-08-25 16:29:12 UTC
Revision:     f2d1fb54a951d8b11d25748e4711bec8d128d7e3

Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          11.0.15 (Private Build 11.0.15+10-Ubuntu-0ubuntu0.20.04.1)
OS:           Linux 5.13.0-52-generic amd64

As an example, Gradle 6.6.1 is built by Kotlin 1.3.72 so that the buildSrc folder should use Kotlin 1.3.72, but the rest of your project can use another Kotlin version.


Reference:

Jimmy Iu
  • 81
  • 2
  • 1
    The `apply false` trick doesn't work. I still get an error message `Error resolving plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.6.10', apply: false] > The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.` – Jorn Sep 01 '22 at 17:28
  • do you have a minimal reproducible example? and do you have the `buildSrc` folder in your project? – Jimmy Iu Sep 06 '22 at 14:15