Hi, what is the best practice for the following pr...
# gradle
w
Hi, what is the best practice for the following problem: I'm developing a library and a corresponding Gradle plugin, both in Kotlin. In the library I want to use the latest Kotlin version (2.2.21). If I put the Gradle plugin in the same root project as the library module, this would make the Gradle plugin also depend on the latest Kotlin version. This in turn makes the Gradle plugin incompatible with Gradle 8.x because that embeds an older Kotlin version and you might get such an error:
Copy code
Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 2.2.0, expected version is 2.0.0.
That's why I split the project up into two separate root projects (each with settings.gradle.kts): one for the lib which uses Kotlin 2.2.21, and one for the Gradle plugin which uses Kotlin 2.0.21. But this requires lots of Gradle machinery and maintaining two different build-logic variants. When I look at other open-source projects containing Gradle plugins, they don't seem to make such a split even though they use the latest Kotlin version, too. Somehow they don't seem to be running into this problem. What's different/wrong with my setup: https://github.com/ensody/kotlin-default-throws-plugin
t
I usually do like you are already doing. composite build. gradle plugin just references the embedded version, the other project pulls from the toml config. Actual root in my case is the actual gradle plugin project (and a few other supporting libraries). shared-elements uses the same as the gradle plugin project so everything there is built with the least common denom. Then server-elements represents a containers that end up in AWS and build using the latest version of kotlin. Shared in this case is mostly just some data classes that are used for the gradle plugin DSL and some proto defs that get shared between plugin/server
then there is also the convention plugins to use across all of it. that gets included in all the different builds
m
You can use KGP 2.2 but target Kotlin 2.0
👆 1
with
apiVersion
/`languageVersion`
w
I tried with apiVersion/languageVersion, but the stdlib seemed to still use the new Kotlin version and I still got that version incompatibility error
m
You need to set
coreLibrairiesVersion
w
Oh wow, thank you! 😄 It's not documented in the Kotlin Gradle Plugin docs (only apiVersion and languageVersion are mentioned). Apparently, coreLibrariesVersion is documented in the Gradle Cookbook instead: https://cookbook.gradle.org/plugin-development/kotlin-plugins/ With this change it works. Really nice.
m
You might also be interested in this: https://mbonnin.net/2025-07-10_the_case_for_kgp/
w
Thanks, yes, that's definitely worth a try