react / react/react-native

[Android] ReactRootProjectPlugin drops :app:preBuild -> library preBuild dependency under --configure-on-demand, breaking codegen before CMake

未关闭
#58,149 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Needs: Author Feedback Needs: Repro
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

Description

ReactRootProjectPlugin makes :app:preBuild depend on every library's preBuild so that library codegen runs before the app's CMake configure:

project.gradle.projectsEvaluated {
  val appProject = project.rootProject.subprojects.find { it.name == "app" }
  val appPreBuild = appProject?.tasks?.findByName("preBuild")
  if (appPreBuild != null) {
    val otherPreBuildTasks =
        project.rootProject.subprojects
            .filter { it != appProject }
            .mapNotNull { it.tasks.findByName("preBuild") }
    appPreBuild.dependsOn(otherPreBuildTasks)
  }
}

When Gradle runs with --configure-on-demand (Expo CLI passes it unconditionally in expo run:android), a library project that has not been evaluated by the time projectsEvaluated fires has no preBuild task yet, so findByName returns null and the dependency is silently dropped. The app's configureCMake<Variant> then runs before that library's generateCodegenArtifactsFromSchema, and the autolinking CMake file fails with:

CMake Error at android/app/build/generated/autolinking/src/main/jni/Android-autolinking.cmake:19 (add_subdirectory):
  add_subdirectory given source
  ".../node_modules/react-native-sherpa-onnx/android/build/generated/source/codegen/jni/"
  which is not an existing directory.
CMake Error at .../ReactAndroid/cmake-utils/ReactNative-application.cmake:97 (target_link_libraries):
  Cannot specify link libraries for target "react_codegen_SherpaOnnxSpec"
  which is not built by this project.

Which libraries are affected depends on evaluation order, so it looks flaky per project. In my project @stripe/stripe-react-native (also a codegen TurboModule library) happened to be evaluated in time and worked, while react-native-sherpa-onnx was not. The same failure was reported independently against that library (XDcobra/react-native-sherpa-onnx#63) and closed as not reproducible, which fits an ordering-dependent bug.

Running the identical build without --configure-on-demand (./gradlew :app:assembleDebug -PreactNativeArchitectures=arm64-v8a) succeeds; the task log shows :react-native-sherpa-onnx:generateCodegenArtifactsFromSchema:app:preBuild:app:configureCMakeDebug[arm64-v8a].

Workaround (verified): in the app's build.gradle, add an explicit task-path dependency, which forces on-demand configuration of that project:

preBuild.dependsOn(':react-native-sherpa-onnx:preBuild')

A robust fix would be to wire the dependency per subproject as each one is evaluated (e.g. subproject.afterEvaluate { appPreBuild.dependsOn(subproject.tasks.named("preBuild")) }, or dependsOn("${subproject.path}:preBuild") by task path) instead of snapshotting subprojects at projectsEvaluated.

Steps to reproduce
  1. npx create-expo-app repro && cd repro
  2. npx expo install react-native-sherpa-onnx @dr.pogodin/react-native-fs (any third-party library with codegenConfig and a JNI target that Gradle happens to evaluate late)
  3. npx expo run:android (Expo CLI runs Gradle with --configure-on-demand)
  4. Build fails in :app:configureCMakeDebug[<abi>] as above.
  5. cd android && ./gradlew :app:assembleDebug succeeds.
React Native Version

0.86.0

Affected Platforms

Build - Android

Output of npx @react-native-community/cli info
System:
  OS: macOS 26.6.2
  CPU: (14) arm64 Apple M4 Pro
Binaries:
  Node: 24.12.0
  npm: 11.6.2
  Watchman: Not Found
Managers:
  CocoaPods: 1.17.0
IDEs:
  Android Studio: 2025.3
  Xcode: 26.2/17C52
Languages:
  Java: 17.0.12
Expo SDK 57 (expo 57.0.8), Gradle 9.3.1, AGP as shipped with RN 0.86.0, NDK 27.1.12297006, CMake 3.22.1
Stacktrace or Logs
> Task :stripe_stripe-react-native:generateCodegenArtifactsFromSchema
> Task :app:preBuild
...
> Task :react-native-sherpa-onnx:downloadNativeLibsIfNeeded
> Task :app:configureCMakeDebug[arm64-v8a] FAILED
(no :react-native-sherpa-onnx:generateCodegenArtifactsFromSchema before the failure)

Execution failed for task ':app:configureCMakeDebug[arm64-v8a]'.
> [CXX1429] error when building with cmake using .../ReactAndroid/cmake-utils/default-app-setup/CMakeLists.txt
  CMake Error at .../Android-autolinking.cmake:19 (add_subdirectory):
    add_subdirectory given source ".../react-native-sherpa-onnx/android/build/generated/source/codegen/jni/" which is not an existing directory.
MANDATORY Reproducer

Steps above (create-expo-app + react-native-sherpa-onnx + expo run:android). The failure depends on Gradle's on-demand evaluation order, so the library that gets skipped may differ by project.

Screenshots and Videos

N/A

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先定位 ReactRootProjectPlugin,并跟踪 projectsEvaluated 如何发现库的 preBuild 任务。使用 Expo 和 react-native-sherpa-onnx 的必需步骤重现问题,然后比较使用 --configure-on-demand 与普通模式时 Gradle 任务的顺序。当库的 codegen 在 app configureCMake 之前运行,并且 autolinking 的 CMake 生成成功时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
android, cmake, kotlin, react-native
领域
build-system, mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
描述清楚
新手友好度
55/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。