From 4bafc61a564bbf0ab314c3a8e5bd9b2a444c3690 Mon Sep 17 00:00:00 2001 From: Christian Melchior Date: Thu, 14 Sep 2023 16:59:58 +0200 Subject: [PATCH] Remove Realm Plugin setting the bytecode target to 1.8 (#1514) --- CHANGELOG.md | 2 +- benchmarks/androidApp/build.gradle.kts | 6 ++-- benchmarks/build.gradle.kts | 2 +- buildSrc/build.gradle.kts | 5 ++++ buildSrc/src/main/kotlin/Config.kt | 10 ++++--- .../src/main/kotlin/realm-lint.gradle.kts | 2 ++ .../kmm-sample/androidApp/build.gradle.kts | 4 +-- .../compose-desktop/build.gradle.kts | 2 +- .../realm-java-compatibility/app/build.gradle | 6 ++-- .../single-platform/build.gradle.kts | 6 ++-- packages/build.gradle.kts | 3 +- packages/cinterop/build.gradle.kts | 14 +++------ packages/gradle-plugin/build.gradle.kts | 4 +-- .../io/realm/kotlin/gradle/RealmPlugin.kt | 29 ------------------- packages/jni-swig-stub/build.gradle.kts | 9 ++---- packages/library-base/build.gradle.kts | 7 ++--- packages/library-sync/build.gradle.kts | 7 ++--- .../plugin-compiler-shaded/build.gradle.kts | 4 +-- packages/plugin-compiler/build.gradle.kts | 5 ++-- packages/test-base/build.gradle.kts | 8 ++--- packages/test-sync/build.gradle.kts | 5 ++-- 21 files changed, 49 insertions(+), 91 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5bc4628fb..130171b81f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ * None. ### Enhancements -* None. +* Realm will no longer set the JVM bytecode to 1.8 when applying the Realm plugin. ([#1513](https://github.com/realm/realm-kotlin/issues/1513)) ### Fixed * [Sync] If calling a function on App Services that resulted in a redirect, it would only redirect for GET requests. (Issue [#1517](https://github.com/realm/realm-kotlin/pull/1517)) diff --git a/benchmarks/androidApp/build.gradle.kts b/benchmarks/androidApp/build.gradle.kts index 68c7b0d5e6..9cb9b3ae58 100644 --- a/benchmarks/androidApp/build.gradle.kts +++ b/benchmarks/androidApp/build.gradle.kts @@ -8,12 +8,12 @@ android { compileSdk = Versions.Android.compileSdkVersion compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } kotlinOptions { - jvmTarget = "1.8" + jvmTarget = Versions.kotlinJvmTarget } defaultConfig { diff --git a/benchmarks/build.gradle.kts b/benchmarks/build.gradle.kts index 2bebc22f69..1722d6672d 100644 --- a/benchmarks/build.gradle.kts +++ b/benchmarks/build.gradle.kts @@ -32,7 +32,7 @@ allprojects { } tasks.withType { - kotlinOptions.jvmTarget = Versions.jvmTarget + kotlinOptions.jvmTarget = Versions.kotlinJvmTarget } } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index e5bd2ee582..ee2e79fee3 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -29,6 +29,11 @@ gradlePlugin { } } +java { + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion +} + repositories { google() gradlePluginPortal() diff --git a/buildSrc/src/main/kotlin/Config.kt b/buildSrc/src/main/kotlin/Config.kt index 78048a52e2..3402f0e318 100644 --- a/buildSrc/src/main/kotlin/Config.kt +++ b/buildSrc/src/main/kotlin/Config.kt @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +import org.gradle.api.JavaVersion /** * Enum describing operating systems we can build on. @@ -124,20 +124,22 @@ object Versions { const val jmh = "1.34" // https://github.com/openjdk/jmh const val jmhPlugin = "0.6.6" // https://github.com/melix/jmh-gradle-plugin const val junit = "4.13.2" // https://mvnrepository.com/artifact/junit/junit - const val jvmTarget = "1.8" + const val kbson = "0.3.0" // https://github.com/mongodb/kbson // When updating the Kotlin version, also remember to update /examples/min-android-sample/build.gradle.kts const val kotlin = "1.8.21" // https://github.com/JetBrains/kotlin and https://kotlinlang.org/docs/releases.html#release-details + const val kotlinJvmTarget = "1.8" // Which JVM bytecode version is kotlin compiled to. const val latestKotlin = "1.9.20-Beta" // https://kotlinlang.org/docs/eap.html#build-details const val kotlinCompileTesting = "1.5.0" // https://github.com/tschuchortdev/kotlin-compile-testing const val ktlint = "0.45.2" // https://github.com/pinterest/ktlint const val ktor = "2.1.2" // https://github.com/ktorio/ktor + const val multidex = "2.0.1" // https://developer.android.com/jetpack/androidx/releases/multidex const val nexusPublishPlugin = "1.1.0" // https://github.com/gradle-nexus/publish-plugin const val okio = "3.2.0" // https://square.github.io/okio/#releases const val relinker = "1.4.5" // https://github.com/KeepSafe/ReLinker const val serialization = "1.4.0" // https://kotlinlang.org/docs/releases.html#release-details const val shadowJar = "6.1.0" // https://mvnrepository.com/artifact/com.github.johnrengelman.shadow/com.github.johnrengelman.shadow.gradle.plugin?repo=gradle-plugins - const val multidex = "2.0.1" // https://developer.android.com/jetpack/androidx/releases/multidex - const val kbson = "0.3.0" // https://github.com/mongodb/kbson + val sourceCompatibilityVersion = JavaVersion.VERSION_1_8 // Language level of any Java source code. + val targetCompatibilityVersion = JavaVersion.VERSION_1_8 // Version of generated JVM bytecode from Java files. } // Could be actual Dependency objects diff --git a/buildSrc/src/main/kotlin/realm-lint.gradle.kts b/buildSrc/src/main/kotlin/realm-lint.gradle.kts index 7875142afd..73714d8f5e 100644 --- a/buildSrc/src/main/kotlin/realm-lint.gradle.kts +++ b/buildSrc/src/main/kotlin/realm-lint.gradle.kts @@ -57,6 +57,7 @@ allprojects { description = "Check Kotlin code style." classpath = ktlint + jvmArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED") mainClass.set("com.pinterest.ktlint.Main") args = listOf( "src/**/*.kt", @@ -74,6 +75,7 @@ allprojects { description = "Fix Kotlin code style deviations." classpath = ktlint + jvmArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED") mainClass.set("com.pinterest.ktlint.Main") args = listOf( "-F", diff --git a/examples/kmm-sample/androidApp/build.gradle.kts b/examples/kmm-sample/androidApp/build.gradle.kts index dd5415befb..4bffec36c9 100644 --- a/examples/kmm-sample/androidApp/build.gradle.kts +++ b/examples/kmm-sample/androidApp/build.gradle.kts @@ -64,7 +64,7 @@ android { } compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } } diff --git a/examples/kmm-sample/compose-desktop/build.gradle.kts b/examples/kmm-sample/compose-desktop/build.gradle.kts index 5735815ed3..9cc8cfd1e5 100644 --- a/examples/kmm-sample/compose-desktop/build.gradle.kts +++ b/examples/kmm-sample/compose-desktop/build.gradle.kts @@ -18,7 +18,7 @@ dependencies { } tasks.withType { - kotlinOptions.jvmTarget = Versions.jvmTarget + kotlinOptions.jvmTarget = Versions.kotlinJvmTarget } application { diff --git a/examples/realm-java-compatibility/app/build.gradle b/examples/realm-java-compatibility/app/build.gradle index ba941ea60f..da72b4e451 100644 --- a/examples/realm-java-compatibility/app/build.gradle +++ b/examples/realm-java-compatibility/app/build.gradle @@ -43,11 +43,11 @@ android { } } compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility Versions.sourceCompatibilityVersion + targetCompatibility Versions.targetCompatibilityVersion } kotlinOptions { - jvmTarget = '1.8' + jvmTarget = Versions.kotlinJvmTarget } } diff --git a/integration-tests/gradle-plugin-test/single-platform/build.gradle.kts b/integration-tests/gradle-plugin-test/single-platform/build.gradle.kts index f2336d03cd..a94fea7d14 100644 --- a/integration-tests/gradle-plugin-test/single-platform/build.gradle.kts +++ b/integration-tests/gradle-plugin-test/single-platform/build.gradle.kts @@ -40,11 +40,11 @@ android { } } compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } kotlinOptions { - jvmTarget = "1.8" + jvmTarget = Versions.kotlinJvmTarget } } diff --git a/packages/build.gradle.kts b/packages/build.gradle.kts index c593f82c55..296bf17b24 100644 --- a/packages/build.gradle.kts +++ b/packages/build.gradle.kts @@ -27,8 +27,9 @@ allprojects { version = Realm.version group = Realm.group + // Define JVM bytecode target for all Kotlin targets tasks.withType { - kotlinOptions.jvmTarget = "${Versions.jvmTarget}" + kotlinOptions.jvmTarget = "${Versions.kotlinJvmTarget}" } } diff --git a/packages/cinterop/build.gradle.kts b/packages/cinterop/build.gradle.kts index 4bf66b813f..8cbeb83f8e 100644 --- a/packages/cinterop/build.gradle.kts +++ b/packages/cinterop/build.gradle.kts @@ -113,11 +113,7 @@ val nativeLibraryIncludesIosSimulatorArm64Release = includeBinaries(releaseLibs.map { "$absoluteCorePath/build-simulator-arm64/lib/$it" }) kotlin { - jvm { - compilations.all { - kotlinOptions.jvmTarget = Versions.jvmTarget - } - } + jvm() android("android") { publishLibraryVariants("release") } @@ -344,12 +340,10 @@ android { path = project.file("src/jvm/CMakeLists.txt") } } - // To avoid - // Failed to transform kotlinx-coroutines-core-jvm-1.5.0-native-mt.jar ... - // The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle + compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } } diff --git a/packages/gradle-plugin/build.gradle.kts b/packages/gradle-plugin/build.gradle.kts index da97942a8e..d7c0c87fbc 100644 --- a/packages/gradle-plugin/build.gradle.kts +++ b/packages/gradle-plugin/build.gradle.kts @@ -82,8 +82,8 @@ publishing { java { withSourcesJar() withJavadocJar() - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } // Make version information available at runtime diff --git a/packages/gradle-plugin/src/main/kotlin/io/realm/kotlin/gradle/RealmPlugin.kt b/packages/gradle-plugin/src/main/kotlin/io/realm/kotlin/gradle/RealmPlugin.kt index 00a2225112..500538896c 100644 --- a/packages/gradle-plugin/src/main/kotlin/io/realm/kotlin/gradle/RealmPlugin.kt +++ b/packages/gradle-plugin/src/main/kotlin/io/realm/kotlin/gradle/RealmPlugin.kt @@ -27,10 +27,6 @@ import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging import org.gradle.api.provider.Provider import org.gradle.build.event.BuildEventsListenerRegistry -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions -import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension -import org.jetbrains.kotlin.gradle.plugin.KotlinTarget import javax.inject.Inject @Suppress("unused") @@ -79,24 +75,11 @@ open class RealmPlugin : Plugin { // Stand alone Android projects have not initialized kotlin plugin when applying this, so // postpone dependency injection till after evaluation. project.afterEvaluate { - val kotlin: Any? = project.extensions.findByName("kotlin") // TODO AUTO-SETUP To ease configuration we could/should inject dependencies to our // library, but await better insight into when/what to inject and supply appropriate // opt-out options through our own extension? // Dependencies should probably be added by source set and not by target, as // kotlin.sourceSets.getByName("commonMain").dependencies (or "main" for Android), but - when (kotlin) { - is KotlinSingleTargetExtension<*> -> { - updateKotlinOption(kotlin.target) - } - is KotlinMultiplatformExtension -> { - kotlin.targets.all { target -> updateKotlinOption(target) } - } - else -> { - // TODO AUTO-SETUP Should we report errors? Probably an oversighted case - // TODO("Cannot 'realm-kotlin' library dependency to ${if (kotlin != null) kotlin::class.qualifiedName else "null"}") - } - } // Create the analytics during configuration because it needs access to the project // in order to gather project relevant information in afterEvaluate. Currently @@ -112,16 +95,4 @@ open class RealmPlugin : Plugin { } } } - - private fun updateKotlinOption(target: KotlinTarget) { - target.compilations.all { compilation -> - // Setup correct compiler options - // FIXME AUTO-SETUP Are these to dangerous to apply under the hood? - when (val options = compilation.kotlinOptions) { - is KotlinJvmOptions -> { - options.jvmTarget = "1.8" - } - } - } - } } diff --git a/packages/jni-swig-stub/build.gradle.kts b/packages/jni-swig-stub/build.gradle.kts index 2cd951be67..7662f9c2e7 100644 --- a/packages/jni-swig-stub/build.gradle.kts +++ b/packages/jni-swig-stub/build.gradle.kts @@ -32,11 +32,6 @@ java { } } -configure { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 -} - tasks.create("realmWrapperJvm") { doLast { // If task is actually triggered (not up to date) then we should clean up the old stuff @@ -70,8 +65,8 @@ realmPublish { java { withSourcesJar() withJavadocJar() - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } publishing { diff --git a/packages/library-base/build.gradle.kts b/packages/library-base/build.gradle.kts index 6c9d007386..d5ca3b5ffc 100644 --- a/packages/library-base/build.gradle.kts +++ b/packages/library-base/build.gradle.kts @@ -161,12 +161,9 @@ android { consumerProguardFiles("proguard-rules-consumer-common.pro") } } - // To avoid - // Failed to transform kotlinx-coroutines-core-jvm-1.5.0-native-mt.jar ... - // The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } // Skip BuildConfig generation as it overlaps with io.realm.kotlin.BuildConfig from realm-java buildFeatures { diff --git a/packages/library-sync/build.gradle.kts b/packages/library-sync/build.gradle.kts index 13a714ad9a..d2f3afe90e 100644 --- a/packages/library-sync/build.gradle.kts +++ b/packages/library-sync/build.gradle.kts @@ -157,12 +157,9 @@ android { consumerProguardFiles("proguard-rules-consumer-common.pro") } } - // To avoid - // Failed to transform kotlinx-coroutines-core-jvm-1.5.0-native-mt.jar ... - // The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } // Skip BuildConfig generation as it overlaps with io.realm.kotlin.BuildConfig from realm-java buildFeatures { diff --git a/packages/plugin-compiler-shaded/build.gradle.kts b/packages/plugin-compiler-shaded/build.gradle.kts index 927f017dba..733801d17b 100644 --- a/packages/plugin-compiler-shaded/build.gradle.kts +++ b/packages/plugin-compiler-shaded/build.gradle.kts @@ -55,8 +55,8 @@ realmPublish { java { withSourcesJar() withJavadocJar() - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } publishing { diff --git a/packages/plugin-compiler/build.gradle.kts b/packages/plugin-compiler/build.gradle.kts index 1dd9133e7e..d68062946b 100644 --- a/packages/plugin-compiler/build.gradle.kts +++ b/packages/plugin-compiler/build.gradle.kts @@ -43,7 +43,6 @@ dependencies { tasks.withType { kotlinOptions { - jvmTarget = "${Versions.jvmTarget}" freeCompilerArgs = listOf("-Xjvm-default=all-compatibility") } } @@ -69,6 +68,6 @@ publishing { java { withSourcesJar() withJavadocJar() - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } diff --git a/packages/test-base/build.gradle.kts b/packages/test-base/build.gradle.kts index c1caa81308..8976c4f7c8 100644 --- a/packages/test-base/build.gradle.kts +++ b/packages/test-base/build.gradle.kts @@ -103,10 +103,6 @@ kotlin { } } } - - tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).all { - kotlinOptions.jvmTarget = Versions.jvmTarget - } } // Android configuration @@ -150,8 +146,8 @@ android { } compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } // Remove overlapping resources after adding "org.jetbrains.kotlinx:kotlinx-coroutines-test" to diff --git a/packages/test-sync/build.gradle.kts b/packages/test-sync/build.gradle.kts index c4b9a718a4..72ce21f2c9 100644 --- a/packages/test-sync/build.gradle.kts +++ b/packages/test-sync/build.gradle.kts @@ -122,7 +122,6 @@ kotlin { } // JVM specific KotlinCompilation tasks tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).all { - kotlinOptions.jvmTarget = Versions.jvmTarget kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn" } } @@ -161,8 +160,8 @@ android { } compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = Versions.sourceCompatibilityVersion + targetCompatibility = Versions.targetCompatibilityVersion } // Remove overlapping resources after adding "org.jetbrains.kotlinx:kotlinx-coroutines-test" to