Skip to content

Commit 00d3971

Browse files
MatiPl01tomekzaw
andauthored
(cherry-pick) Fix linking error after Gradle clean project on Android (#7919)
Cherry-pick of the #7866 PR Co-authored-by: Tomasz Zawadzki <[email protected]>
1 parent 1b44844 commit 00d3971

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

packages/react-native-reanimated/android/CMakeLists.txt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ file(GLOB_RECURSE REANIMATED_ANDROID_CPP_SOURCES CONFIGURE_DEPENDS
4949

5050
find_package(fbjni REQUIRED CONFIG)
5151
find_package(ReactAndroid REQUIRED CONFIG)
52-
find_package(react-native-worklets REQUIRED CONFIG)
5352

5453
add_library(reanimated SHARED ${REANIMATED_COMMON_CPP_SOURCES}
5554
${REANIMATED_ANDROID_CPP_SOURCES})
@@ -65,13 +64,30 @@ target_include_directories(
6564
"${REACT_NATIVE_DIR}/ReactCommon/runtimeexecutor"
6665
"${REACT_NATIVE_DIR}/ReactCommon/jsiexecutor"
6766
"${REACT_NATIVE_DIR}/ReactCommon/react/renderer/graphics/platform/cxx"
68-
)
67+
"${REACT_NATIVE_WORKLETS_DIR}/Common/cpp"
68+
"${REACT_NATIVE_WORKLETS_DIR}/android/src/main/cpp")
6969

7070
set_target_properties(reanimated PROPERTIES LINKER_LANGUAGE CXX)
7171

72-
target_link_libraries(reanimated log ReactAndroid::jsi fbjni::fbjni android)
72+
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
73+
set(BUILD_TYPE "debug")
74+
else()
75+
set(BUILD_TYPE "release")
76+
endif()
77+
78+
add_library(worklets SHARED IMPORTED)
79+
80+
set_target_properties(
81+
worklets
82+
PROPERTIES
83+
IMPORTED_LOCATION
84+
"${REACT_NATIVE_WORKLETS_DIR}/android/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libworklets.so"
85+
)
86+
87+
set_target_properties(reanimated PROPERTIES LINKER_LANGUAGE CXX)
7388

74-
target_link_libraries(reanimated react-native-worklets::worklets)
89+
target_link_libraries(reanimated log ReactAndroid::jsi fbjni::fbjni android
90+
worklets)
7591

7692
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
7793
target_link_libraries(reanimated ReactAndroid::reactnative)

packages/react-native-reanimated/android/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ def resolveReactNativeDirectory() {
4242
)
4343
}
4444

45+
def resolveReactNativeWorkletsDirectory() {
46+
def reactNativeWorkletsLocation = safeAppExtGet("REACT_NATIVE_WORKLETS_NODE_MODULES_DIR", null)
47+
if (reactNativeWorkletsLocation != null) {
48+
return file(reactNativeWorkletsLocation)
49+
}
50+
51+
// Fallback to node resolver for custom directory structures like monorepos.
52+
def reactNativeWorkletsPackage = file(
53+
providers.exec {
54+
workingDir(rootDir)
55+
commandLine("node", "--print", "require.resolve('react-native-worklets/package.json')")
56+
}.standardOutput.asText.get().trim()
57+
)
58+
if (reactNativeWorkletsPackage.exists()) {
59+
return reactNativeWorkletsPackage.parentFile
60+
}
61+
62+
throw new GradleException(
63+
"[Reanimated] Unable to resolve react-native-worklets location in node_modules. You should set project extension property (in `app/build.gradle`) named `REACT_NATIVE_WORKLETS_NODE_MODULES_DIR` with the path to react-native-worklets in node_modules."
64+
)
65+
}
66+
4567
def getReactNativeMinorVersion() {
4668
def reactNativeRootDir = resolveReactNativeDirectory()
4769
def reactProperties = new Properties()
@@ -102,6 +124,7 @@ if (isNewArchitectureEnabled()) {
102124
}
103125

104126
def reactNativeRootDir = resolveReactNativeDirectory()
127+
def reactNativeWorkletsRootDir = resolveReactNativeWorkletsDirectory()
105128
def REACT_NATIVE_MINOR_VERSION = getReactNativeMinorVersion()
106129
def REANIMATED_VERSION = getReanimatedVersion()
107130
def IS_NEW_ARCHITECTURE_ENABLED = isNewArchitectureEnabled()
@@ -181,6 +204,7 @@ android {
181204
"-DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION}",
182205
"-DANDROID_TOOLCHAIN=clang",
183206
"-DREACT_NATIVE_DIR=${toPlatformFileString(reactNativeRootDir.path)}",
207+
"-DREACT_NATIVE_WORKLETS_DIR=${toPlatformFileString(reactNativeWorkletsRootDir.path)}",
184208
"-DIS_REANIMATED_EXAMPLE_APP=${IS_REANIMATED_EXAMPLE_APP}",
185209
"-DREANIMATED_PROFILING=${REANIMATED_PROFILING}",
186210
"-DREANIMATED_VERSION=${REANIMATED_VERSION}",
@@ -324,3 +348,12 @@ dependencies {
324348
}
325349

326350
preBuild.dependsOn(prepareReanimatedHeadersForPrefabs)
351+
352+
if (project != rootProject) {
353+
evaluationDependsOn(":react-native-worklets")
354+
355+
afterEvaluate {
356+
tasks.getByName("externalNativeBuildDebug").dependsOn(findProject(":react-native-worklets").tasks.getByName("externalNativeBuildDebug"))
357+
tasks.getByName("externalNativeBuildRelease").dependsOn(findProject(":react-native-worklets").tasks.getByName("externalNativeBuildRelease"))
358+
}
359+
}

scripts/lint-cmake.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ if ! which cmake-lint >/dev/null; then
55
exit 1
66
fi
77

8-
cmake-lint "$@"
8+
cmake-lint "$@" --line-width 120

0 commit comments

Comments
 (0)