Skip to content

Commit 84894ed

Browse files
Kotlin: proper compatibility with 1.7 compiler and standard library
1 parent ace207d commit 84894ed

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ buildscript {
5656
// Check
5757
// - https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin
5858
// - https://github.com/Kotlin/kotlinx.coroutines#readme
59-
// Note: when updating to a new minor version also have to increase the minimum compiler version supported
60-
// by consuming projects, see objectbox-kotlin/ build script.
59+
// Note: when updating to a new minor version also have to increase the minimum compiler and standard library
60+
// version supported by consuming projects, see objectbox-kotlin/ build script.
6161
val kotlinVersion by extra("2.0.21")
6262
val coroutinesVersion by extra("1.9.0")
6363
// Dokka includes its own version of the Kotlin compiler, so it must not match the used Kotlin version.

objectbox-kotlin/build.gradle.kts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import org.jetbrains.dokka.gradle.DokkaTask
2-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
3+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
34
import java.net.URL
45

56
plugins {
@@ -14,17 +15,28 @@ tasks.withType<JavaCompile> {
1415
options.release.set(8)
1516
}
1617

17-
tasks.withType<KotlinCompile> {
18-
kotlinOptions {
19-
// Produce Java 8 byte code, would default to Java 6.
20-
jvmTarget = "1.8"
21-
// Allow consumers of this library to use an older version of the Kotlin compiler. By default only the version
22-
// previous to the compiler used for this project typically works.
23-
// Kotlin supports the development with at least three previous versions, so pick the oldest one possible.
24-
// https://kotlinlang.org/docs/kotlin-evolution.html#evolving-the-binary-format
18+
kotlin {
19+
compilerOptions {
20+
// Produce Java 8 byte code, would default to Java 6
21+
jvmTarget.set(JvmTarget.JVM_1_8)
22+
23+
// Allow consumers of this library to use the oldest possible Kotlin compiler and standard libraries.
2524
// https://kotlinlang.org/docs/compatibility-modes.html
26-
apiVersion = "1.7"
27-
languageVersion = "1.7"
25+
// https://kotlinlang.org/docs/kotlin-evolution-principles.html#compatibility-tools
26+
27+
// Prevents using newer language features, sets this as the Kotlin version in produced metadata. So consumers
28+
// can compile this with a Kotlin compiler down to one minor version before this.
29+
// Pick the oldest not deprecated version.
30+
languageVersion.set(KotlinVersion.KOTLIN_1_7)
31+
// Prevents using newer APIs from the Kotlin standard library. So consumers can run this library with a Kotlin
32+
// standard library down to this version.
33+
// Pick the oldest not deprecated version.
34+
apiVersion.set(KotlinVersion.KOTLIN_1_7)
35+
// Depend on the oldest compatible Kotlin standard libraries (by default the Kotlin plugin coerces it to the one
36+
// matching its version). So consumers can safely use this or any later Kotlin standard library.
37+
// Pick the first release matching the versions above.
38+
// Note: when changing, also update coroutines dependency version (as this does not set that).
39+
coreLibrariesVersion = "1.7.0"
2840
}
2941
}
3042

@@ -57,11 +69,11 @@ val sourcesJar by tasks.registering(Jar::class) {
5769
from(sourceSets.main.get().allSource)
5870
}
5971

60-
val coroutinesVersion: String by rootProject.extra
61-
6272
dependencies {
63-
// Note: compileOnly as we do not want to require library users to use coroutines.
64-
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
73+
// Note: compileOnly so consumers do not depend on the coroutines library unless they manually add it.
74+
// Note: pick a version that depends on Kotlin standard library (org.jetbrains.kotlin:kotlin-stdlib) version
75+
// coreLibrariesVersion (set above) or older.
76+
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
6577

6678
api(project(":objectbox-java"))
6779
}

0 commit comments

Comments
 (0)