Skip to content

Skip evaluated projects when aligning Java versions - #60374

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/gradle-jdk-sweep-evaluated-projects
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/gradle-jdk-sweep-evaluated-projects

Conversation

@ide

@ide ide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

JdkConfiguratorUtils.configureJavaToolChains runs for each application that applies the React plugin, and registers a finalizeDsl callback on every Android project in the build to set Java 17. 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 configuration fails in two cases:

  • Gradle evaluates a library before the app. ReactRootProjectPlugin makes every other project depend on the evaluation of :app, so this happens only in a build that does not apply that plugin, or in one where the app that applies the React plugin is not :app.
  • Two applications apply the plugin. The second one's call reaches the first, which is already evaluated.

ReactPlugin already skips evaluated projects for the same reason when it applies the namespace fallback to libraries (#57290). This change does the same in configureJavaToolChains for Android projects: one that is evaluated before the app is left as it is, and keeps the Java version and Kotlin JVM toolchain it declares. A Kotlin JVM project with no Android plugin, such as the root project in a build without ReactRootProjectPlugin, still gets jvmToolchain(17), which works after evaluation.

Changelog:

[ANDROID] [FIXED] - Fix "It is too late to call finalizeDsl" when a library is evaluated before the app or a build has two apps

Test Plan:

./gradlew -p packages/gradle-plugin :react-native-gradle-plugin:test --tests com.facebook.react.utils.JdkConfiguratorUtilsTest --max-workers=4 --console=plain

The new JdkConfiguratorUtilsTest builds and evaluates projects with ProjectBuilder: configureJavaToolChains_withALibraryEvaluatedBeforeTheApp_alignsTheApp and configureJavaToolChains_withTwoApps_alignsBoth. On main both fail with the finalizeDsl error above. With the fix changed to skip every project, both fail on the assertion ([11, 11] instead of 17), so they check that the versions are set. configureJavaToolChains_withAKotlinJvmProjectEvaluatedBeforeTheApp_setsItsToolchain checks that an evaluated Kotlin JVM project still gets toolchain 17; it fails (null) if every evaluated project is skipped.

ktfmt is clean. Not run: the rest of the plugin's tests, and a real Android build in either failing layout.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 28, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 4f3843338cfe.

ios: no capture

`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 the Android projects that are already evaluated, as the namespace
fallback in `ReactPlugin` already does. A project evaluated before the
app keeps the Java version and Kotlin JVM toolchain it declares. A
Kotlin JVM project without an Android plugin still gets
`jvmToolchain(17)`, which works after evaluation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ide
ide force-pushed the @ide/gradle-jdk-sweep-evaluated-projects branch from b159498 to 4f38433 Compare November 29, 2026 12:02
@ide
ide marked this pull request as ready for review November 29, 2026 13:49
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 29, 2026

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root cause and fix look right to me, and the new tests reproduce both layouts. (Commenting rather than approving since it's still a draft.) Two things:

1. The early return also skips the Kotlin toolchain hooks. The project.state.executed check in JdkConfiguratorUtils.kt comes before the org.jetbrains.kotlin.android and org.jetbrains.kotlin.jvm blocks that call jvmToolchain(17), so an already-evaluated project loses those too.

  • For Android modules that is the right call. Setting the Kotlin toolchain to 17 while Java compile options stay at their default would trip Kotlin's JVM-target consistency check.
  • A plain kotlin.jvm module never had the finalizeDsl problem. If it's evaluated before the app, it now silently stops being aligned to 17.

Could you either say this in the description, or narrow the early return to the AGP finalizeDsl actions, so the kotlin.jvm hook still applies?

2. The test casts to ProjectInternal. (project as ProjectInternal).evaluate() depends on an internal Gradle API that can change in any Gradle release, and no other test in the plugin uses it. Is there a public way to get an evaluated project here? For example, build the project through ProjectBuilder and trigger evaluation via afterEvaluate/getTasksByName, or use a TestKit build. If not, a short comment on why the internal API is needed would help the next person who has to update it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants