From b15949840d8f2dedb1d68852d58fe44f4de8f256 Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:43:51 -0800 Subject: [PATCH] Skip evaluated projects when aligning Java versions `configureJavaToolChains` runs for each application that applies the React plugin and registers a `finalizeDsl` callback on every Android project in the build. AGP finalizes a project's DSL when the project is evaluated, and AGP 9 fails the build when `finalizeDsl` is called after that: It is too late to call `finalizeDsl` as the DSL finalization blocks have already been executed. So the build fails during configuration when Gradle evaluates a library before the app, and when a second app applies the plugin after the first has been evaluated. `ReactRootProjectPlugin` orders `:app` before every other project, so the first case needs a build that does not apply that plugin, or whose React app is not `:app`. Skip projects that are already evaluated, as the namespace fallback in `ReactPlugin` already does. A library evaluated before the app keeps the Java version it declares. Co-Authored-By: Claude Opus 5.5 --- .../react/utils/JdkConfiguratorUtils.kt | 6 ++ .../react/utils/JdkConfiguratorUtilsTest.kt | 76 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/JdkConfiguratorUtilsTest.kt diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt index 2a652dbcde74..de27c967b082 100644 --- a/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/utils/JdkConfiguratorUtils.kt @@ -32,6 +32,12 @@ internal object JdkConfiguratorUtils { if (project.hasProperty(INTERNAL_DISABLE_JAVA_VERSION_ALIGNMENT)) { return@allprojects } + // AGP finalizes a project's DSL when the project is evaluated, and fails the build if + // `finalizeDsl` is called after that. A project is already evaluated here if Gradle + // configured it before this app, or if another app in the build has already run this. + if (project.state.executed) { + return@allprojects + } val applicationAction = Action { diff --git a/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/JdkConfiguratorUtilsTest.kt b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/JdkConfiguratorUtilsTest.kt new file mode 100644 index 000000000000..9ce3afbe0162 --- /dev/null +++ b/packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/utils/JdkConfiguratorUtilsTest.kt @@ -0,0 +1,76 @@ +/* + * 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.dsl.CommonExtension +import org.assertj.core.api.Assertions.assertThat +import org.gradle.api.JavaVersion +import org.gradle.api.Project +import org.gradle.api.internal.project.ProjectInternal +import org.gradle.testfixtures.ProjectBuilder +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class JdkConfiguratorUtilsTest { + + @get:Rule val tempFolder = TemporaryFolder() + + @Test + fun configureJavaToolChains_withALibraryEvaluatedBeforeTheApp_alignsTheApp() { + val root = ProjectBuilder.builder().withProjectDir(tempFolder.newFolder("root")).build() + val library = androidProject(root, "library", "com.android.library") + val app = androidProject(root, "app", "com.android.application") + evaluate(library) + + JdkConfiguratorUtils.configureJavaToolChains(app) + evaluate(app) + + assertThat(javaVersions(app)).containsOnly(JavaVersion.VERSION_17) + } + + @Test + fun configureJavaToolChains_withTwoApps_alignsBoth() { + val root = ProjectBuilder.builder().withProjectDir(tempFolder.newFolder("root")).build() + val firstApp = androidProject(root, "first", "com.android.application") + val secondApp = androidProject(root, "second", "com.android.application") + + JdkConfiguratorUtils.configureJavaToolChains(firstApp) + evaluate(firstApp) + JdkConfiguratorUtils.configureJavaToolChains(secondApp) + evaluate(secondApp) + + assertThat(javaVersions(firstApp)).containsOnly(JavaVersion.VERSION_17) + assertThat(javaVersions(secondApp)).containsOnly(JavaVersion.VERSION_17) + } + + private fun androidProject(root: Project, name: String, plugin: String): Project { + val project = + ProjectBuilder.builder() + .withName(name) + .withParent(root) + .withProjectDir(tempFolder.newFolder(name)) + .build() + project.plugins.apply(plugin) + project.extensions.getByType(CommonExtension::class.java).apply { + compileSdk = 35 + namespace = "com.facebook.react.test.$name" + } + return project + } + + // AGP finalizes a project's DSL when the project is evaluated. + private fun evaluate(project: Project) { + (project as ProjectInternal).evaluate() + } + + private fun javaVersions(project: Project): List = + project.extensions.getByType(CommonExtension::class.java).compileOptions.let { + listOf(it.sourceCompatibility, it.targetCompatibility) + } +}