From ea856b8671645cb168026743fb3944746ae233d0 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Thu, 23 Nov 2023 06:59:18 -0800 Subject: [PATCH] Fix projects being broken on dependencies starting with `a..` (#41621) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41621 Currently, if you have a dependency that is alphabetically smaller than `app`, it's evaluation will happen before `app`. This means that the namespace auto-discovery and the JVM toolchain configuration won't be working and the project will fail to buid. This fixes it by introducing a root-project Gradle Plugin that takes care of enforcing the evaluation order on the `app` project. Fixes #41620 Changelog: [Android] [Fixed] - Fix projects being broken on dependencies starting with `a..` Reviewed By: huntie Differential Revision: D51547294 fbshipit-source-id: 65df7149548b7087dd8928e556fb803b3baf7b79 --- .../build.gradle.kts | 4 +++ .../facebook/react/ReactRootProjectPlugin.kt | 30 +++++++++++++++++++ .../template/android/build.gradle | 4 +-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactRootProjectPlugin.kt diff --git a/packages/react-native-gradle-plugin/build.gradle.kts b/packages/react-native-gradle-plugin/build.gradle.kts index c4a31beb010e19..9346508f497062 100644 --- a/packages/react-native-gradle-plugin/build.gradle.kts +++ b/packages/react-native-gradle-plugin/build.gradle.kts @@ -26,6 +26,10 @@ gradlePlugin { id = "com.facebook.react" implementationClass = "com.facebook.react.ReactPlugin" } + create("reactrootproject") { + id = "com.facebook.react.rootproject" + implementationClass = "com.facebook.react.ReactRootProjectPlugin" + } } } diff --git a/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactRootProjectPlugin.kt b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactRootProjectPlugin.kt new file mode 100644 index 00000000000000..5f232cd0e581a5 --- /dev/null +++ b/packages/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactRootProjectPlugin.kt @@ -0,0 +1,30 @@ +/* + * 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 + +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** + * Gradle plugin applied to the `android/build.gradle` file. + * + * This plugin allows to specify project wide configurations that can be applied to both apps and + * libraries before they're evaluated. + */ +class ReactRootProjectPlugin : Plugin { + override fun apply(project: Project) { + project.subprojects { + // As the :app project (i.e. ReactPlugin) configures both namespaces and JVM toolchains + // for libraries, its evaluation must happen before the libraries' evaluation. + // Eventually the configuration of namespace/JVM toolchain can be moved inside this plugin. + if (it.path != ":app") { + it.evaluationDependsOn(":app") + } + } + } +} diff --git a/packages/react-native/template/android/build.gradle b/packages/react-native/template/android/build.gradle index 8012362ce17b77..a61601bb05dbf6 100644 --- a/packages/react-native/template/android/build.gradle +++ b/packages/react-native/template/android/build.gradle @@ -1,5 +1,3 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. - buildscript { ext { buildToolsVersion = "34.0.0" @@ -19,3 +17,5 @@ buildscript { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin") } } + +apply plugin: "com.facebook.react.rootproject"