I have a multi-module Gradle build using Gradle Ko...
# build-tools
d
I have a multi-module Gradle build using Gradle Kotlin DSL. I would like to share a common configuration for the Kotlin plugin across the modules, for example the "used experimental annotations". For example I have this snippet:
Copy code
kotlin {
    sourceSets.all {
        languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
    }
}
I would like to move this into buildSrc so in the modules I can just do:
Copy code
kotlin {
    configureExperimentalAnnotations()
}
To do this, I'd want to do this as an extension function on
KotlinProjectExtension
, but I have no idea which dependency I need in the
buildSrc
Gradle script for that.
kotlin-gradle-plugin
doesn't have it. Any advice?
t
KotlinPluginExtension
should be available when you will add
kotlin-gradle-plugin
as a
buildSrc
module dependency.
could you past here you dependency declaration line?
d
Copy code
dependencies {
    implementation(kotlin("gradle-plugin"))
}
in the build.gradle.kts in buildSrc. I do have the jar as a dependency now, but the package that contains
KotlinPluginExtension
is empty in the jar file
t
which version?
1.4.31
?
d
Yes
This is the whole build.gradle.kts file in buildSrc:
Copy code
plugins {
    kotlin("jvm") version "1.4.31"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("gradle-plugin"))
}
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
This import doesn't work in the buildSrc code (it works in the build.gradle.kts file)
t
Plugins block should be:
Copy code
plugins {
    `kotlin-dsl`
}
and then dependency will have
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31")
notation
d
Now its telling me my
kotlin("jvm")
plugin on the main build.gradle.kts must not include a version. But if I remove that version, I am not specifying a version anywhere, which one will it use?
Okay, I've made those changes (and removed the version from the plugin application in my main build.gradle.kts). The situation however is unfortunately unchanged
I'll make a minimal reproduction project
t
probably even this dependency notation will be better to use (it will pull all plugin ids from Gradle plugin portal):
Copy code
implementation("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.4.31")
and you need to add
gradlePluginPortal()
to the
repositories { }
Also I've checked plugin jar file and
KotlinJvmProjectExtension
class file is there 🤔
t
I see
KotlinProjectExtension
in your sample project:
probably try to invalidate caches and restart
d
... the hell? Well, thanks for confirming that for me. I'll try to invalidate all the things
invalidating intellij caches didn't help. Time to bring out the big guns:
rm -rf ~/.gradle
😱 1
God bless the brute force, it worked.
🎉 1
😕 1
Thanks for helping me debug it.
👍 1
Okay, coming back to this thread, because this has stopped working when I updated to Kotlin 1.5.0. I have always had warnings to the following extend when doing this:
Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
With it then listing the Kotin stdlib that is shipped with Gradle (1.3.72) and my version (1.4.31, now 1.5.0). Now with Kotlin 1.5.0 this breaks completely:
Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16.
I have reinstated my test-repository here: https://github.com/diesieben07/kotlin-dsl-buildsrc-bug Any advice?
Updating to Gradle 7 "fixes" this, because Gradle 7 ships Kotlin 1.4.31 internally (no idea why Kotlin 1.4 accepts the 1.5 jars, but 1.3 doesn't?). But I still have that warning because in buildSrc I have 1.4.31 and 1.5.0 jars on the classpath... How to do this properly?
t
warning should be fixed with 1.5.10 release, see this issue
Gradle has updated Kotlin to 1.4 in 6.8 release
d
Okay, but just to be clear, that only removes the warning, right? It won't fix the hard crash with Kotlin 1.5 on Gradle < 6.8?
t
it may fix it as well 🤔
d
well, I'll stay tuned for 1.5.10 then. At least my build runs again with updated Gradle and I'll trust that those warnings are nothing bad 😄
Thank you!
u
(no idea why Kotlin 1.4 accepts the 1.5 jars, but 1.3 doesn’t?)
Thanks for bringing this up, it is indeed unclear and we should document this. Since Kotlin 1.3, Kotlin/JVM metadata has “up to 1 version” forward-compatibility policy. So 1.3 can read metadata of classes compiled with Kotlin from 1.0 to 1.4.255; 1.4 can read metadata from 1.0 to 1.5.255, and so on.
d
Ahhh, that would explain it and that is indeed what I am seeing. With Gradle < 6.8 (so shipping with 1.3) I could use Kotlin 1.4 fine. Now with Gradle >= 6.8 (shipping with 1.4) I can also use 1.5
Unfortunately the 1.5.10 update has broken buildSrc in the IDE completely (works fine when building, it's just an IDE bug): 😞 Let me make a YouTrack issue
521 Views