Skip to content

Commit

Permalink
Kotlin to 1.8.0 and JDK Toolchain to 11 (#37220)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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 facebook-github-bot committed May 4, 2023
1 parent 9e893a9 commit 74987b6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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.0" apply false
}

val reactAndroidProperties = java.util.Properties()
Expand Down
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.0")
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 @@ -19,6 +19,7 @@ import com.facebook.react.utils.BackwardCompatUtils.configureBackwardCompatibili
import com.facebook.react.utils.DependencyUtils.configureDependencies
import com.facebook.react.utils.DependencyUtils.configureRepositories
import com.facebook.react.utils.DependencyUtils.readVersionString
import com.facebook.react.utils.JdkConfiguratorUtils.configureJavaToolChains
import com.facebook.react.utils.JsonUtils
import com.facebook.react.utils.NdkConfiguratorUtils.configureReactNativeNdk
import com.facebook.react.utils.ProjectUtils.needsCodegenFromPackageJson
Expand Down Expand Up @@ -63,6 +64,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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.utils

import com.android.build.api.variant.AndroidComponentsExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension

internal object JdkConfiguratorUtils {
/**
* 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(project: Project) {
project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext ->
ext.compileOptions.sourceCompatibility = JavaVersion.VERSION_11
ext.compileOptions.targetCompatibility = JavaVersion.VERSION_11
}
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)
}
}
}
10 changes: 9 additions & 1 deletion packages/react-native/ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ android {
ndkVersion rootProject.ext.ndkVersion
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

defaultConfig {
minSdk = 21
targetSdk = 33
Expand Down Expand Up @@ -718,11 +723,14 @@ react {
jsRootDir = file("../Libraries")
}

kotlin {
jvmToolchain(11)
}

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 packages/react-native/ReactAndroid/hermes-engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,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 packages/react-native/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.0" apply false
}
4 changes: 2 additions & 2 deletions packages/react-native/template/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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.7.22"
kotlinVersion = "1.8.0"
}
repositories {
google()
Expand All @@ -18,6 +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:$kotlinVersion")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
}
}

0 comments on commit 74987b6

Please sign in to comment.