Specifying the Kotlin version in Gradle multi-project builds

For single-project Gradle builds, it is straightforward to set the language version that we want to use to compile Kotlin code. For instance, as described here, we can just do the following:

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

After running gradle init to generate a multi-project build, we are presented with a directory structure that looks like this:

demo
├── app
│   ├── build.gradle.kts
│   └── src
│
├── buildSrc
│   ├── build.gradle.kts
│   └── src
│       └── main
│           └── kotlin
│               ├── demo.kotlin-application-conventions.gradle.kts
│               ├── demo.kotlin-common-conventions.gradle.kts
│               └── demo.kotlin-library-conventions.gradle.kts
├── list
│   ├── build.gradle.kts
│   └── src
│
├── settings.gradle.kts
└── utilities
    ├── build.gradle.kts
    └── src

I would like to specify the Kotlin version that Gradle uses to compile the actual code, i.e., in this case, the app, list, and utilities project. My initial attempt was to just modify demo.kotlin-common-conventions.gradle.kts accordingly:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.6.10"
}

However, doing so fails with the following message:

Invalid plugin request [id: ‘org.jetbrains.kotlin.jvm’, version: ‘1.6.10’]. Plugin requests from precompiled scripts must not include a version number. Please remove the version from the offending request and make sure the module containing the requested plugin ‘org.jetbrains.kotlin.jvm’ is an implementation dependency of project ‘:buildSrc’.

Trying to specify the version individually for each of the three projects fails as well:

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

I also tried to set the Kotlin version in the buildSrc/build.gradle.kts file, but this seems to affect the plugin compilation process (rather than the actual code compilation). Understandably, this leads to the following warning:

WARNING: Unsupported Kotlin plugin version.
The embedded-kotlin and kotlin-dsl plugins rely on features of Kotlin 1.5.31 that might work differently than in the requested version 1.6.10.

Is there a way to achieve the above-mentioned goal of specifying the Kotlin version, preferably at one single location in the directory structure?

1 Like

one way of achieving this …
In root project’s build.gradle.kts

plugins {
    java
    kotlin("jvm") version "1.6.10" apply false
}

And then in each subproject, we could do:

plugins {
    java
    kotlin("jvm")
}
1 Like

You need to add a dependency in buildSrc/build.gradle.kts:

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
}

For me, convention plugins have a great potential and they are really a proper way to handle building, they will be awesome someday. Unfortunately, for now they are kind of messy and it’s often hard to do even simple things.

5 Likes

I was unable to identify the root project’s build.gradle.kts in the generated sources, so I couldn’t give the first approach a try. Adding the desired version as a dependency, however, did exactly what I wanted to achieve.

For some reason, I did not associate the recommendation in the first error message with kotlin-gradle-plugin at all. Anyways, it works great now.

Thank you to both of you!

2 Likes

I think the first solution is really for projects with “traditional” approach to sharing the build logic. It doesn’t fit projects using convention plugins.

2 Likes