Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,15 +130,7 @@ class JdkTargetConvention : Plugin<Project> {

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.
Expand All @@ -145,6 +139,50 @@ class JdkTargetConvention : Plugin<Project> {
}
}

/**
* 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
Expand Down
Loading