fix(gradle-plugin): guard missing autolinked JNI dirs (#56886)

Summary:
Related to https://github.com/facebook/react-native/issues/56334.

`GenerateAutolinkingNewArchitecturesFileTask` currently emits unconditional `add_subdirectory(...)` calls for autolinked New Architecture JNI/codegen directories.

During `clean`, Android Studio native model configuration, or other native cleanup/configuration flows, some dependency codegen directories may not exist yet. CMake then fails before clean/configuration can complete:

```text
add_subdirectory given source ".../android/build/generated/source/codegen/jni/"
which is not an existing directory
```

This changes the generated `Android-autolinking.cmake` to:

- initialize `AUTOLINKED_LIBRARIES` as an empty list;
- only call `add_subdirectory(...)` when the native directory exists;
- append each autolinked target only after its directory has been added.

That keeps existing generated JNI directories linked as before while allowing native clean/model configuration to skip directories that have not been generated yet.

## Changelog:

[ANDROID] [FIXED] - Guard missing autolinked JNI directories in generated CMake during native clean/model configuration.

Pull Request resolved: https://github.com/facebook/react-native/pull/56886

Test Plan:
```sh
./gradlew -p packages/gradle-plugin :react-native-gradle-plugin:test --tests com.facebook.react.tasks.GenerateAutolinkingNewArchitecturesFileTaskTest
```

Result:

```text
BUILD SUCCESSFUL
```

Reviewed By: christophpurrer

Differential Revision: D105826478

Pulled By: cortinico

fbshipit-source-id: e3a698c2334cc02189eefca21b34a0a0181eb5f3
This commit is contained in:
Phecda authored and meta-codesync[bot] committed 2026-05-20 11:20:46 -07:00
1 parent b3c0f3ce30
commit 948835d944
2 files changed
+45 -30

No files matched your search

@@ -61,31 +61,39 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
// If user provided a custom cmakeListsPath, let's honor it.
val nativeFolderPath = sanitizeCmakeListsPath(cmakeListsPath)
addDirectoryString +=
"add_subdirectory(\"$nativeFolderPath\" ${libraryName}_autolinked_build)"
"""
if(EXISTS "$nativeFolderPath")
add_subdirectory("$nativeFolderPath" ${libraryName}_autolinked_build)
list(APPEND AUTOLINKED_LIBRARIES $CODEGEN_LIB_PREFIX${libraryName})
else()
message(WARNING "React Native: Skipping autolinked library '$CODEGEN_LIB_PREFIX${libraryName}' because the source directory does not exist: $nativeFolderPath")
endif()
"""
.trimIndent()
}
if (cxxModuleCMakeListsPath != null) {
// If user provided a custom cxxModuleCMakeListsPath, let's honor it.
val nativeFolderPath = sanitizeCmakeListsPath(cxxModuleCMakeListsPath)
addDirectoryString +=
"\nadd_subdirectory(\"$nativeFolderPath\" ${libraryName}_cxxmodule_autolinked_build)"
"""
if(EXISTS "$nativeFolderPath")
add_subdirectory("$nativeFolderPath" ${libraryName}_cxxmodule_autolinked_build)
${
dep.cxxModuleCMakeListsModuleName?.let {
" list(APPEND AUTOLINKED_LIBRARIES $it)"
} ?: ""
}
else()
message(WARNING "React Native: Skipping autolinked C++ module '${dep.cxxModuleCMakeListsModuleName ?: libraryName}' because the source directory does not exist: $nativeFolderPath")
endif()
"""
.trimIndent()
}
addDirectoryString
}
val libraryModules =
packages.joinToString("\n ") { dep ->
var autolinkedLibraries = ""
if (dep.libraryName != null) {
autolinkedLibraries += "$CODEGEN_LIB_PREFIX${dep.libraryName}"
}
if (dep.cxxModuleCMakeListsModuleName != null) {
autolinkedLibraries += "\n${dep.cxxModuleCMakeListsModuleName}"
}
autolinkedLibraries
}
return CMAKE_TEMPLATE.replace("{{ libraryIncludes }}", libraryIncludes)
.replace("{{ libraryModules }}", libraryModules)
}
internal fun generateCppFileContent(
@@ -173,11 +181,9 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
# or link against a old prefab target (this is needed for React Native 0.76 on).
set(REACTNATIVE_MERGED_SO true)
{{ libraryIncludes }}
set(AUTOLINKED_LIBRARIES)
set(AUTOLINKED_LIBRARIES
{{ libraryModules }}
)
{{ libraryIncludes }}
"""
.trimIndent()
@@ -128,11 +128,9 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
# or link against a old prefab target (this is needed for React Native 0.76 on).
set(REACTNATIVE_MERGED_SO true)
set(AUTOLINKED_LIBRARIES)
set(AUTOLINKED_LIBRARIES
)
"""
.trimIndent()
)
@@ -155,15 +153,26 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
# or link against a old prefab target (this is needed for React Native 0.76 on).
set(REACTNATIVE_MERGED_SO true)
add_subdirectory("./a/directory/" aPackage_autolinked_build)
add_subdirectory("./another/directory/with\ spaces/" anotherPackage_autolinked_build)
add_subdirectory("./another/directory/cxx/" anotherPackage_cxxmodule_autolinked_build)
set(AUTOLINKED_LIBRARIES)
set(AUTOLINKED_LIBRARIES
react_codegen_aPackage
react_codegen_anotherPackage
another_cxxModule
)
if(EXISTS "./a/directory/")
add_subdirectory("./a/directory/" aPackage_autolinked_build)
list(APPEND AUTOLINKED_LIBRARIES react_codegen_aPackage)
else()
message(WARNING "React Native: Skipping autolinked library 'react_codegen_aPackage' because the source directory does not exist: ./a/directory/")
endif()
if(EXISTS "./another/directory/with\ spaces/")
add_subdirectory("./another/directory/with\ spaces/" anotherPackage_autolinked_build)
list(APPEND AUTOLINKED_LIBRARIES react_codegen_anotherPackage)
else()
message(WARNING "React Native: Skipping autolinked library 'react_codegen_anotherPackage' because the source directory does not exist: ./another/directory/with\ spaces/")
endif()
if(EXISTS "./another/directory/cxx/")
add_subdirectory("./another/directory/cxx/" anotherPackage_cxxmodule_autolinked_build)
list(APPEND AUTOLINKED_LIBRARIES another_cxxModule)
else()
message(WARNING "React Native: Skipping autolinked C++ module 'another_cxxModule' because the source directory does not exist: ./another/directory/cxx/")
endif()
"""
.trimIndent()
)