Jakob K
05/26/2022, 4:48 PM1.7.0-RC
in a buildSrc project, the kotlin-dsl
plugin cannot be applied. Is there any way I can already use 1.7.0-RC
- or is it just not possible yet?
The exception is:
Caused by: java.lang.NoSuchFieldError: Companion
at org.jetbrains.kotlin.gradle.dsl.ToolchainSupport$Companion.createToolchain$kotlin_gradle_plugin(ToolchainDsl.kt:33)
at org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension.<init>(KotlinProjectExtension.kt:66)
at org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension.<init>(KotlinProjectExtension.kt:106)
ephemient
05/26/2022, 5:01 PMChris Lee
05/26/2022, 5:05 PMkotlin-dsl
and the Gradle-compatible/provided Kotlin versions), or a Kotlin library/application using the Kotlin Gradle plugin. It’s nonsensical to mix the two in a given project.Jakob K
05/26/2022, 5:07 PM1.7.0-RC
?ephemient
05/26/2022, 5:08 PMChris Lee
05/26/2022, 5:10 PMafterEvaluate {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
// arg necessary to address <https://github.com/gradle/gradle/issues/17052>
freeCompilerArgs += "-Xsam-conversions=class"
apiVersion = "1.5"
languageVersion = "1.5"
}
}
}
…note the dreaded afterEvaluate
which is required in the case (always try to avoid this!!!), as the Kotlin DSL plugin sets the language level to 1.4 in its own afterEvaluate
.ephemient
05/26/2022, 5:12 PMephemient
05/26/2022, 5:14 PMChris Lee
05/26/2022, 5:14 PMconfigure<KotlinDslPluginOptions> {
jvmTarget.set(kotlinPluginJvmVersion.toString())
}
Jakob K
05/26/2022, 9:58 PMwhich maxes out at 1.5.31 for currently released versions of GradleSo it is basically impossible to write precompiled scripts (with the Gradle Kotlin DSL extensions) in buildSrc with up to date ksp, dokka versions and so on? Seems like the only solution is to just not use the kotlin-dsl plugin here, but then the scripts would become much more verbose.
ephemient
05/26/2022, 9:59 PMkotlin-dsl
, it will run in the Gradle environment which will force the usage of Gradle's embedded Kotlin. so no, you cannot use the latest Kotlin.Jakob K
05/26/2022, 10:01 PMephemient
05/26/2022, 10:05 PMephemient
05/26/2022, 10:07 PMJakob K
05/26/2022, 10:10 PMephemient
05/26/2022, 10:10 PMephemient
05/26/2022, 10:11 PMJakob K
05/26/2022, 10:13 PMephemient
05/26/2022, 10:13 PMJakob K
05/26/2022, 10:14 PMephemient
05/26/2022, 10:15 PMJakob K
05/26/2022, 10:15 PMephemient
05/26/2022, 10:15 PMplugins { id("...ksp") }
inside buildSrc/build.gradle.kts
, not inside buildSrc/src/main/kotlin/my-plugin.gradle.kts
? because the latter is fineJakob K
05/26/2022, 10:17 PMephemient
05/26/2022, 10:17 PM// buildSrc/build.gradle.kts
repositories { ... }
dependencies {
implementation("...ksp-gradle-plugin...")
}
? as long as you're not applying the plugin the the buildSrc
project itself, that is totally fineJakob K
05/26/2022, 10:19 PMJakob K
05/26/2022, 10:19 PMephemient
05/26/2022, 10:22 PMJakob K
05/26/2022, 10:25 PMephemient
05/26/2022, 10:26 PMJakob K
05/26/2022, 10:27 PMephemient
05/26/2022, 10:27 PM// buildSrc/build.gradle.kts
repositories {
google()
mavenCentral()
}
dependencies {
implementation("com.android.tools.build:gradle:7.2.0")
implementation(kotlin("gradle-plugin", "1.6.10"))
implementation("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.6.10-1.0.4")
}
and that works fine, for using Kotlin 1.6.10 and KSP 1.6.10-1.0.4 plugins in the main buildephemient
05/26/2022, 10:28 PMJakob K
05/26/2022, 10:29 PMephemient
05/26/2022, 10:30 PMJakob K
05/26/2022, 10:31 PMkotlin-dsl
plugin, right?ephemient
05/26/2022, 10:33 PMephemient
05/26/2022, 10:34 PMephemient
05/26/2022, 10:36 PMephemient
05/26/2022, 10:37 PM// buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation("com.android.tools.build:gradle:7.2.0")
implementation(kotlin("gradle-plugin", "1.7.0-RC"))
compileOnly("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.5.31-1.0.1")
runtimeOnly("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:1.7.0-RC-1.0.5")
}
worksJakob K
05/26/2022, 10:41 PMoh I see, that does result in buildSrc's older embedded Kotlin compiler being given newer libraries in its compile classpathdid you get the same
java.lang.NoSuchFieldError: Companion
exception? or are you just referring to the "w: Runtime JAR files in the classpath should have the same version" warning?Jakob K
05/26/2022, 10:44 PMhonestly that seems like a KSP issue, because Kotlin Gradle plugin is careful with what it usesseems like it is a very common issue, as it is also the case for dokka and other compiler plugins
ephemient
05/26/2022, 10:45 PMMichal Klimczak
06/29/2022, 2:08 PMruntimeOnly
with my library (and thus with a specific kotlin version).
Instead, I'm now requiring the ksp plugin, but not providing it implicitly, so that user can choose his kotlin and ksp version.
I'm not sure what the original use case for ksp plugin dependency you had @Jakob K, but wanted to share in case anyone ends up here like me 🙂