Skip to content

Commit 948835d

Browse files
Phecdameta-codesync[bot]
authored andcommitted
fix(gradle-plugin): guard missing autolinked JNI dirs (#56886)
Summary: Related to #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: #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
1 parent b3c0f3c commit 948835d

2 files changed

Lines changed: 46 additions & 31 deletions

File tree

packages/gradle-plugin/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTask.kt

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,39 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
6161
// If user provided a custom cmakeListsPath, let's honor it.
6262
val nativeFolderPath = sanitizeCmakeListsPath(cmakeListsPath)
6363
addDirectoryString +=
64-
"add_subdirectory(\"$nativeFolderPath\" ${libraryName}_autolinked_build)"
64+
"""
65+
if(EXISTS "$nativeFolderPath")
66+
add_subdirectory("$nativeFolderPath" ${libraryName}_autolinked_build)
67+
list(APPEND AUTOLINKED_LIBRARIES $CODEGEN_LIB_PREFIX${libraryName})
68+
else()
69+
message(WARNING "React Native: Skipping autolinked library '$CODEGEN_LIB_PREFIX${libraryName}' because the source directory does not exist: $nativeFolderPath")
70+
endif()
71+
"""
72+
.trimIndent()
6573
}
6674
if (cxxModuleCMakeListsPath != null) {
6775
// If user provided a custom cxxModuleCMakeListsPath, let's honor it.
6876
val nativeFolderPath = sanitizeCmakeListsPath(cxxModuleCMakeListsPath)
6977
addDirectoryString +=
70-
"\nadd_subdirectory(\"$nativeFolderPath\" ${libraryName}_cxxmodule_autolinked_build)"
78+
"""
79+
80+
if(EXISTS "$nativeFolderPath")
81+
add_subdirectory("$nativeFolderPath" ${libraryName}_cxxmodule_autolinked_build)
82+
${
83+
dep.cxxModuleCMakeListsModuleName?.let {
84+
" list(APPEND AUTOLINKED_LIBRARIES $it)"
85+
} ?: ""
86+
}
87+
else()
88+
message(WARNING "React Native: Skipping autolinked C++ module '${dep.cxxModuleCMakeListsModuleName ?: libraryName}' because the source directory does not exist: $nativeFolderPath")
89+
endif()
90+
"""
91+
.trimIndent()
7192
}
7293
addDirectoryString
7394
}
7495

75-
val libraryModules =
76-
packages.joinToString("\n ") { dep ->
77-
var autolinkedLibraries = ""
78-
if (dep.libraryName != null) {
79-
autolinkedLibraries += "$CODEGEN_LIB_PREFIX${dep.libraryName}"
80-
}
81-
if (dep.cxxModuleCMakeListsModuleName != null) {
82-
autolinkedLibraries += "\n${dep.cxxModuleCMakeListsModuleName}"
83-
}
84-
autolinkedLibraries
85-
}
86-
8796
return CMAKE_TEMPLATE.replace("{{ libraryIncludes }}", libraryIncludes)
88-
.replace("{{ libraryModules }}", libraryModules)
8997
}
9098

9199
internal fun generateCppFileContent(
@@ -173,11 +181,9 @@ abstract class GenerateAutolinkingNewArchitecturesFileTask : DefaultTask() {
173181
# or link against a old prefab target (this is needed for React Native 0.76 on).
174182
set(REACTNATIVE_MERGED_SO true)
175183
176-
{{ libraryIncludes }}
184+
set(AUTOLINKED_LIBRARIES)
177185
178-
set(AUTOLINKED_LIBRARIES
179-
{{ libraryModules }}
180-
)
186+
{{ libraryIncludes }}
181187
"""
182188
.trimIndent()
183189

packages/gradle-plugin/react-native-gradle-plugin/src/test/kotlin/com/facebook/react/tasks/GenerateAutolinkingNewArchitecturesFileTaskTest.kt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,9 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
128128
# or link against a old prefab target (this is needed for React Native 0.76 on).
129129
set(REACTNATIVE_MERGED_SO true)
130130
131+
set(AUTOLINKED_LIBRARIES)
131132
132133
133-
set(AUTOLINKED_LIBRARIES
134-
135-
)
136134
"""
137135
.trimIndent()
138136
)
@@ -155,15 +153,26 @@ class GenerateAutolinkingNewArchitecturesFileTaskTest {
155153
# or link against a old prefab target (this is needed for React Native 0.76 on).
156154
set(REACTNATIVE_MERGED_SO true)
157155
158-
add_subdirectory("./a/directory/" aPackage_autolinked_build)
159-
add_subdirectory("./another/directory/with\ spaces/" anotherPackage_autolinked_build)
160-
add_subdirectory("./another/directory/cxx/" anotherPackage_cxxmodule_autolinked_build)
161-
162-
set(AUTOLINKED_LIBRARIES
163-
react_codegen_aPackage
164-
react_codegen_anotherPackage
165-
another_cxxModule
166-
)
156+
set(AUTOLINKED_LIBRARIES)
157+
158+
if(EXISTS "./a/directory/")
159+
add_subdirectory("./a/directory/" aPackage_autolinked_build)
160+
list(APPEND AUTOLINKED_LIBRARIES react_codegen_aPackage)
161+
else()
162+
message(WARNING "React Native: Skipping autolinked library 'react_codegen_aPackage' because the source directory does not exist: ./a/directory/")
163+
endif()
164+
if(EXISTS "./another/directory/with\ spaces/")
165+
add_subdirectory("./another/directory/with\ spaces/" anotherPackage_autolinked_build)
166+
list(APPEND AUTOLINKED_LIBRARIES react_codegen_anotherPackage)
167+
else()
168+
message(WARNING "React Native: Skipping autolinked library 'react_codegen_anotherPackage' because the source directory does not exist: ./another/directory/with\ spaces/")
169+
endif()
170+
if(EXISTS "./another/directory/cxx/")
171+
add_subdirectory("./another/directory/cxx/" anotherPackage_cxxmodule_autolinked_build)
172+
list(APPEND AUTOLINKED_LIBRARIES another_cxxModule)
173+
else()
174+
message(WARNING "React Native: Skipping autolinked C++ module 'another_cxxModule' because the source directory does not exist: ./another/directory/cxx/")
175+
endif()
167176
"""
168177
.trimIndent()
169178
)

0 commit comments

Comments
 (0)