From fcdd08aff6b2dcd35da2113c635262aab1b86a9a Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 4 May 2023 11:12:00 -0700 Subject: [PATCH] Kotlin to 1.8.22 and JDK Toolchain to 11 (#37220) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/37220 I'm bumping Kotlin to 1.8.0 to align to the version used internally. On top of this, I had to configure the JDK toolchain to 11 as Kotlin 1.8 was changing the default version of the stdlib it ships with by default. This also shields us against problems once we'll bump to AGP 8 which requires JDK 17 but still allows to produce libraries that are JDK 11 compatible. Changelog: [Android] [Changed] - Kotlin to 1.8.0 and JDK Toolchain to 11 Reviewed By: cipolleschi Differential Revision: D45524689 fbshipit-source-id: 2558b5b6727b5d6e0e1e3cc58f0c6a85ddcefc4d --- ReactAndroid/build.gradle | 19 ++++++---- ReactAndroid/hermes-engine/build.gradle | 9 +++++ build.gradle.kts | 2 +- .../build.gradle.kts | 9 ++++- .../kotlin/com/facebook/react/ReactPlugin.kt | 4 +- .../react/utils/JdkConfiguratorUtils.kt | 37 ++++++------------- template/android/build.gradle | 2 + 7 files changed, 45 insertions(+), 37 deletions(-) diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 08f1fe0d5c37ee..a7ce9fc449ee36 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -532,9 +532,14 @@ android { ndkVersion rootProject.ext.ndkVersion } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + defaultConfig { - minSdkVersion(21) - targetSdkVersion(33) + minSdk = 21 + targetSdk = 33 versionCode(1) versionName("1.0") @@ -557,10 +562,7 @@ android { "-DREACT_BUILD_DIR=$buildDir", "-DANDROID_STL=c++_shared", "-DANDROID_TOOLCHAIN=clang", - "-DANDROID_PLATFORM=android-21", - // Due to https://github.com/android/ndk/issues/1693 we're losing Android - // specific compilation flags. This can be removed once we moved to NDK 25/26 - "-DANDROID_USE_LEGACY_TOOLCHAIN_FILE=ON" + "-DANDROID_PLATFORM=android-21" targets "jsijniprofiler", "reactnativeblob", @@ -815,6 +817,10 @@ react { jsRootDir = file("../Libraries") } +kotlin { + jvmToolchain(11) +} + /** * NOTE(flewp): This 'privateReact' config is an undocumented feature of the react-native-gradle-plugin. * It matches the same structure as the 'react' config, but the plugin only considers the root project's @@ -834,7 +840,6 @@ tasks.withType(Test).all { // We add --add-opens flags to make sure we can run PowerMock tests on JDK >= 17 if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_17)) { jvmArgs += [ - "-XX:+AllowRedefinitionToAddDeleteMethods", "--illegal-access=permit", "--add-opens=java.base/java.lang=ALL-UNNAMED", "--add-opens=java.base/java.xml.internal=ALL-UNNAMED", diff --git a/ReactAndroid/hermes-engine/build.gradle b/ReactAndroid/hermes-engine/build.gradle index 39f47a4b5cc329..417362d3e8de88 100644 --- a/ReactAndroid/hermes-engine/build.gradle +++ b/ReactAndroid/hermes-engine/build.gradle @@ -222,6 +222,15 @@ android { } } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + kotlin { + jvmToolchain(11) + } + sourceSets { main { manifest.srcFile "$hermesDir/android/hermes/src/main/AndroidManifest.xml" diff --git a/build.gradle.kts b/build.gradle.kts index 217bf9379b9543..7eaf5ed6e82801 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,5 +14,5 @@ plugins { id("com.android.library") version "7.4.2" apply false id("com.android.application") version "7.4.2" apply false id("de.undercouch.download") version "5.0.1" apply false - kotlin("android") version "1.7.22" apply false + kotlin("android") version "1.8.22" apply false } diff --git a/packages/react-native-gradle-plugin/build.gradle.kts b/packages/react-native-gradle-plugin/build.gradle.kts index aba364903b2857..b7e6edb032d54f 100644 --- a/packages/react-native-gradle-plugin/build.gradle.kts +++ b/packages/react-native-gradle-plugin/build.gradle.kts @@ -11,7 +11,7 @@ import org.gradle.configurationcache.extensions.serviceOf import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.7.22" + kotlin("jvm") version "1.8.0" id("java-gradle-plugin") } @@ -33,7 +33,12 @@ group = "com.facebook.react" dependencies { implementation(gradleApi()) + + // The KGP/AGP version is defined by React Native Gradle plugin. + // Therefore we specify an implementation dep rather than a compileOnly. + implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.22") implementation("com.android.tools.build:gradle:7.4.2") + implementation("com.google.code.gson:gson:2.8.9") implementation("com.google.guava:guava:31.0.1-jre") implementation("com.squareup:javapoet:1.13.0") @@ -54,6 +59,8 @@ java { targetCompatibility = JavaVersion.VERSION_11 } +kotlin { jvmToolchain(11) } + tasks.withType { kotlinOptions { jvmTarget = JavaVersion.VERSION_11.majorVersion diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt index 062dcc738a266d..8663111ce17768 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactPlugin.kt @@ -66,6 +66,7 @@ class ReactPlugin : Plugin { configureBuildConfigFields(project) configureDevPorts(project) configureBackwardCompatibilityReactMap(project) + configureJavaToolChains(project) project.extensions.getByType(AndroidComponentsExtension::class.java).apply { onVariants(selector().all()) { variant -> @@ -79,9 +80,6 @@ class ReactPlugin : Plugin { project.pluginManager.withPlugin("com.android.library") { configureCodegen(project, extension, rootExtension, isLibrary = true) } - - // App and Library Configurations - configureJavaToolChains(project) } private fun checkJvmVersion(project: Project) { diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt index a18f2b67315543..c86484ab5249c5 100644 --- a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt @@ -8,39 +8,26 @@ package com.facebook.react.utils import com.android.build.api.variant.AndroidComponentsExtension -import com.facebook.react.utils.PropertyUtils.INTERNAL_DISABLE_JAVA_VERSION_ALIGNMENT -import org.gradle.api.Action import org.gradle.api.JavaVersion import org.gradle.api.Project -import org.gradle.api.plugins.AppliedPlugin +import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension internal object JdkConfiguratorUtils { /** - * Function that takes care of configuring the JDK toolchain for all the projects projects. As we - * do decide the JDK version based on the AGP version that RNGP brings over, here we can safely + * Function that takes care of configuring the JDK toolchain for Application projects. As we do + * decide the JDK version based on the AGP version that RNGP brings over, here we can safely * configure the toolchain to 11. */ - fun configureJavaToolChains(input: Project) { - if (input.hasProperty(INTERNAL_DISABLE_JAVA_VERSION_ALIGNMENT)) { - return + fun configureJavaToolChains(project: Project) { + project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext -> + ext.compileOptions.sourceCompatibility = JavaVersion.VERSION_11 + ext.compileOptions.targetCompatibility = JavaVersion.VERSION_11 } - input.rootProject.allprojects { project -> - val action = - Action { - project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { - ext -> - ext.compileOptions.sourceCompatibility = JavaVersion.VERSION_11 - ext.compileOptions.targetCompatibility = JavaVersion.VERSION_11 - } - } - project.pluginManager.withPlugin("com.android.application", action) - project.pluginManager.withPlugin("com.android.library", action) + project.pluginManager.withPlugin("org.jetbrains.kotlin.android") { + project.extensions.getByType(KotlinTopLevelExtension::class.java).jvmToolchain(11) + } + project.pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { + project.extensions.getByType(KotlinTopLevelExtension::class.java).jvmToolchain(11) } - // We set kotlin.jvm.target.validation.mode=warning on the root projects, as for projects - // on Gradle 8+ and Kotlin 1.8+ this value is set to `error`. This will cause the build to - // fail if the JDK version between compileKotlin and compileJava and jvmTarget are not - // aligned. This won't be necessary anymore from React Native 0.73. More on this: - // https://kotlinlang.org/docs/whatsnew18.html#obligatory-check-for-jvm-targets-of-related-kotlin-and-java-compile-tasks - input.rootProject.extensions.extraProperties.set("kotlin.jvm.target.validation.mode", "warning") } } diff --git a/template/android/build.gradle b/template/android/build.gradle index 34ea71819406f6..ca995d01c0dcc0 100644 --- a/template/android/build.gradle +++ b/template/android/build.gradle @@ -9,6 +9,7 @@ buildscript { // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. ndkVersion = "23.1.7779620" + kotlinVersion = "1.8.22" } repositories { google() @@ -17,5 +18,6 @@ buildscript { dependencies { classpath("com.android.tools.build:gradle") classpath("com.facebook.react:react-native-gradle-plugin") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin") } }