Skip to content

Commit

Permalink
Kotlin to 1.8.22 and JDK Toolchain to 11 (facebook#37220)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#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
  • Loading branch information
cortinico authored and yayvery committed Jan 14, 2024
1 parent 2d0c1cf commit 5533824
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 37 deletions.
19 changes: 12 additions & 7 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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",
Expand Down Expand Up @@ -810,6 +812,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
Expand All @@ -829,7 +835,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",
Expand Down
9 changes: 9 additions & 0 deletions ReactAndroid/hermes-engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 8 additions & 1 deletion packages/react-native-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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")
Expand All @@ -54,6 +59,8 @@ java {
targetCompatibility = JavaVersion.VERSION_11
}

kotlin { jvmToolchain(11) }

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.majorVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ReactPlugin : Plugin<Project> {
configureBuildConfigFields(project)
configureDevPorts(project)
configureBackwardCompatibilityReactMap(project)
configureJavaToolChains(project)

project.extensions.getByType(AndroidComponentsExtension::class.java).apply {
onVariants(selector().all()) { variant ->
Expand All @@ -79,9 +80,6 @@ class ReactPlugin : Plugin<Project> {
project.pluginManager.withPlugin("com.android.library") {
configureCodegen(project, extension, rootExtension, isLibrary = true)
}

// App and Library Configurations
configureJavaToolChains(project)
}

private fun checkJvmVersion(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppliedPlugin> {
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")
}
}
2 changes: 2 additions & 0 deletions template/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
}
}

0 comments on commit 5533824

Please sign in to comment.