From 77e065d961470c695a00063ec1940163c6df4905 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Sun, 2 Jan 2022 16:25:43 +0100 Subject: [PATCH] fix: Fix Android build on React Native 0.65 and older (#694) * fix: Fix Android build on React Native 0.65 and older * fix: Add excludes * fix: Ignore META-INF from package * fix: Wrong var name --- android/CMakeLists.txt | 35 +++++++++++++++++++++++++++++------ android/build.gradle | 10 +++++++++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index ab15dc3..f87ef60 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -10,6 +10,17 @@ set (RN_SO_DIR ${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first- # VisionCamera shared +if(${REACT_NATIVE_VERSION} LESS 66) + set ( + INCLUDE_JSI_CPP + "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp" + ) + set ( + INCLUDE_JSIDYNAMIC_CPP + "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/JSIDynamic.cpp" + ) +endif() + add_library( ${PACKAGE_NAME} SHARED @@ -31,11 +42,14 @@ file (GLOB LIBFBJNI_INCLUDE_DIR "${BUILD_DIR}/fbjni-*-headers.jar/") target_include_directories( ${PACKAGE_NAME} PRIVATE + # --- fbjni --- "${LIBFBJNI_INCLUDE_DIR}" + # --- Third Party (required by RN) --- "${BUILD_DIR}/third-party-ndk/boost" "${BUILD_DIR}/third-party-ndk/double-conversion" "${BUILD_DIR}/third-party-ndk/folly" "${BUILD_DIR}/third-party-ndk/glog" + # --- React Native --- "${NODE_MODULES_DIR}/react-native/React" "${NODE_MODULES_DIR}/react-native/React/Base" "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni" @@ -44,6 +58,9 @@ target_include_directories( "${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker" "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi" "${NODE_MODULES_DIR}/hermes-engine/android/include/" + ${INCLUDE_JSI_CPP} # only on older RN versions + ${INCLUDE_JSIDYNAMIC_CPP} # only on older RN versions + # --- Reanimated --- "${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/Tools" "${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/SpecTools" "${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/SharedItems" @@ -87,12 +104,6 @@ find_library( PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) -find_library( - JSI_LIB - jsi - PATHS ${LIBRN_DIR} - NO_CMAKE_FIND_ROOT_PATH -) find_library( FOLLY_JSON_LIB folly_json @@ -106,6 +117,18 @@ find_library( PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) +if(${REACT_NATIVE_VERSION} LESS 66) + # JSI lib didn't exist on RN 0.65 and before. Simply omit it. + set (JSI_LIB "") +else() + # RN 0.66 distributes libjsi.so, can be used instead of compiling jsi.cpp manually. + find_library( + JSI_LIB + jsi + PATHS ${LIBRN_DIR} + NO_CMAKE_FIND_ROOT_PATH + ) +endif() find_library( REANIMATED_LIB diff --git a/android/build.gradle b/android/build.gradle index f98216e..f82579e 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -4,6 +4,10 @@ import java.nio.file.Paths def reactNative = new File("$projectDir/../node_modules/react-native") +def reactProperties = new Properties() +file("$projectDir/../node_modules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) } +def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME").split("\\.")[1].toInteger() + def FOR_HERMES = System.getenv("FOR_HERMES") == "True" rootProject.getSubprojects().forEach({project -> if (project.plugins.hasPlugin("com.android.application")) { @@ -94,6 +98,7 @@ android { cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID" abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' arguments '-DANDROID_STL=c++_shared', + "-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}", "-DNODE_MODULES_DIR=${rootDir}/../node_modules", "-DFOR_HERMES=${FOR_HERMES}" } @@ -111,7 +116,10 @@ android { } packagingOptions { - excludes = ["**/libc++_shared.so", "**/libfbjni.so", "**/libjsi.so"] + // Exclude all Libraries that are already present in the user's app (through React Native or by him installing REA) + excludes = ["**/libc++_shared.so", "**/libfbjni.so", "**/libjsi.so", "**/libreactnativejni.so", "**/libfolly_json.so", "**/libreanimated.so", "**/libjscexecutor.so"] + // META-INF is duplicate by CameraX. + exclude "META-INF/**" } buildTypes {