From 40af65101f60acef01d5aa280c0b3105ef0ee196 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 25 Nov 2026 14:01:33 +0100 Subject: [PATCH] build: Pin the published Kotlin version on the kotlin extension The jdk-target convention set the language/API version on each main compile task. KGP's model doesn't see options set that way. Under the com.android.kotlin.multiplatform.library plugin, sentry-compose then fails to configure: Kotlin checks that androidMain's version isn't lower than commonMain's before the commonMain metadata task is configured. The convention now sets the version on the kotlin extension of published modules, and test compilations go back to the compiler's default. Every compile task resolves to the same version as before. Co-Authored-By: Claude Opus 5.5 --- .../io/sentry/gradle/JdkTargetConvention.kt | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt b/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt index 3dd36346b6..355b93fa07 100644 --- a/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt +++ b/build-logic/src/main/kotlin/io/sentry/gradle/JdkTargetConvention.kt @@ -13,9 +13,11 @@ import org.gradle.api.tasks.compile.JavaCompile import org.gradle.jvm.toolchain.JavaLanguageVersion import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.KotlinVersion +import org.jetbrains.kotlin.gradle.plugin.KotlinTarget import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask @@ -128,15 +130,7 @@ class JdkTargetConvention : Plugin { project.keepKotlinModuleNameWithoutGroup() - // Test code isn't published, so it keeps the compiler's default and may use newer features. - project.plugins.withId("com.vanniktech.maven.publish") { - project.tasks.withType(KotlinCompilationTask::class.java).configureEach { - if (!name.contains("Test")) { - compilerOptions.languageVersion.set(PUBLISHED_KOTLIN_VERSION) - compilerOptions.apiVersion.set(PUBLISHED_KOTLIN_VERSION) - } - } - } + project.plugins.withId("com.vanniktech.maven.publish") { project.pinPublishedKotlinVersion() } // The SDK targets the oldest language/API version its consumers' Kotlin can read, which newer // compilers deprecate. That's deliberate, so the warning on every compilation is noise. @@ -145,6 +139,50 @@ class JdkTargetConvention : Plugin { } } + /** + * Sets the published language and API version on the `kotlin` extension rather than on the + * compile tasks, so KGP's model sees it too: multiplatform modules check that a source set's + * version isn't lower than the one of the source sets it depends on, and that check doesn't see + * the options of compile tasks that aren't configured yet. Test code isn't published, so it keeps + * the compiler's default and may use newer features. + */ + private fun Project.pinPublishedKotlinVersion() { + plugins.withId("org.jetbrains.kotlin.jvm") { + extensions.getByType(KotlinJvmProjectExtension::class.java).run { + compilerOptions.pinPublishedKotlinVersion() + target.useDefaultKotlinVersionForTests() + } + } + plugins.withId("org.jetbrains.kotlin.android") { + extensions.getByType(KotlinAndroidProjectExtension::class.java).run { + compilerOptions.pinPublishedKotlinVersion() + target.useDefaultKotlinVersionForTests() + } + } + plugins.withId("org.jetbrains.kotlin.multiplatform") { + extensions.getByType(KotlinMultiplatformExtension::class.java).run { + compilerOptions.pinPublishedKotlinVersion() + targets.configureEach { useDefaultKotlinVersionForTests() } + } + } + } + + private fun KotlinCommonCompilerOptions.pinPublishedKotlinVersion() { + languageVersion.set(PUBLISHED_KOTLIN_VERSION) + apiVersion.set(PUBLISHED_KOTLIN_VERSION) + } + + private fun KotlinTarget.useDefaultKotlinVersionForTests() { + compilations + .matching { it.name.contains("test", ignoreCase = true) } + .configureEach { + compileTaskProvider.configure { + compilerOptions.languageVersion.set(KotlinVersion.DEFAULT) + compilerOptions.apiVersion.set(KotlinVersion.DEFAULT) + } + } + } + /** * KGP 2.4 prefixes the Kotlin module name with the group, e.g. `io.sentry:sentry-okhttp`. R8 copies * that name into the names of the `.kotlin_module` entries it writes under `META-INF`, and app