fix: Add support for react-native 0.71 (#1438)

Co-authored-by: Christoph Gritschenberger <christoph.gritschenberger@cca.io>
This commit is contained in:
Christoph Gritschenberger 2023-01-30 19:38:52 +01:00 committed by GitHub
parent ae2c28a2c2
commit b82d0e362d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 187 additions and 95 deletions

View File

@ -1,13 +1,26 @@
project(VisionCamera)
cmake_minimum_required(VERSION 3.4.1) cmake_minimum_required(VERSION 3.4.1)
set (CMAKE_VERBOSE_MAKEFILE ON) set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 14) set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID -DFOR_HERMES=${FOR_HERMES}")
if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
include("${NODE_MODULES_DIR}/react-native/ReactAndroid/cmake-utils/folly-flags.cmake")
add_compile_options(${folly_FLAGS})
else()
set (CMAKE_CXX_FLAGS "-DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID -DFOR_HERMES=${FOR_HERMES}")
endif()
set (PACKAGE_NAME "VisionCamera") set (PACKAGE_NAME "VisionCamera")
set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build) set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set (RN_SO_DIR ${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/react/jni) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)
else()
set (RN_SO_DIR ${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/react/jni)
endif()
# VisionCamera shared # VisionCamera shared
if(${REACT_NATIVE_VERSION} LESS 66) if(${REACT_NATIVE_VERSION} LESS 66)
@ -36,64 +49,84 @@ add_library(
) )
# includes # includes
if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"${NODE_MODULES_DIR}/react-native/ReactCommon/react/renderer/graphics/platform/cxx"
"${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
"${NODE_MODULES_DIR}/react-native/ReactCommon/yoga"
# --- Reanimated ---
# New
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/AnimatedSensor"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/Tools"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/SpecTools"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/SharedItems"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/Registries"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/LayoutAnimations"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/hidden_headers"
"src/main/cpp"
)
else()
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"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${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 ---
# Old
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/AnimatedSensor"
"${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"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/Registries"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/LayoutAnimations"
# New
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/AnimatedSensor"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/Tools"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/SpecTools"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/SharedItems"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/Registries"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/LayoutAnimations"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/hidden_headers"
"src/main/cpp"
)
endif()
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"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
"${NODE_MODULES_DIR}/react-native/ReactCommon"
"${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 ---
# Old
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/AnimatedSensor"
"${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"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/Registries"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/headers/LayoutAnimations"
# New
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/AnimatedSensor"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/Tools"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/SpecTools"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/SharedItems"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/Registries"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/LayoutAnimations"
"${NODE_MODULES_DIR}/react-native-reanimated/Common/cpp/hidden_headers"
"src/main/cpp"
)
# find libraries # find libraries
file (GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}") file (GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
if(${FOR_HERMES}) if(${FOR_HERMES})
string(APPEND CMAKE_CXX_FLAGS " -DJS_RUNTIME_HERMES=1") string(APPEND CMAKE_CXX_FLAGS " -DFOR_HERMES=1")
if(${REACT_NATIVE_VERSION} LESS 69) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
# From `hermes-engine` npm package find_package(hermes-engine REQUIRED CONFIG)
target_include_directories( elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 69)
${PACKAGE_NAME}
PRIVATE
"${JS_RUNTIME_DIR}/android/include"
)
else()
# Bundled Hermes from module `com.facebook.react:hermes-engine` or project `:ReactAndroid:hermes-engine` # Bundled Hermes from module `com.facebook.react:hermes-engine` or project `:ReactAndroid:hermes-engine`
target_include_directories( target_include_directories(
${PACKAGE_NAME} ${PACKAGE_NAME}
@ -101,23 +134,41 @@ if(${FOR_HERMES})
"${JS_RUNTIME_DIR}/API" "${JS_RUNTIME_DIR}/API"
"${JS_RUNTIME_DIR}/public" "${JS_RUNTIME_DIR}/public"
) )
else()
# From `hermes-engine` npm package
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${JS_RUNTIME_DIR}/android/include"
)
endif() endif()
target_link_libraries( if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
${PACKAGE_NAME} target_link_libraries(
"${BUILD_DIR}/third-party-ndk/hermes/jni/${ANDROID_ABI}/libhermes.so" ${PACKAGE_NAME}
) "hermes-engine::libhermes"
)
else()
target_link_libraries(
${PACKAGE_NAME}
"${BUILD_DIR}/third-party-ndk/hermes/jni/${ANDROID_ABI}/libhermes.so"
)
endif()
file (GLOB LIBREANIMATED_DIR "${BUILD_DIR}/react-native-reanimated-*-hermes.aar/jni/${ANDROID_ABI}") file (GLOB LIBREANIMATED_DIR "${BUILD_DIR}/react-native-reanimated-*-hermes.aar/jni/${ANDROID_ABI}")
else() else()
file (GLOB LIBJSC_DIR "${BUILD_DIR}/android-jsc*.aar/jni/${ANDROID_ABI}") file (GLOB LIBJSC_DIR "${BUILD_DIR}/android-jsc*.aar/jni/${ANDROID_ABI}")
# Use JSC if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
find_library( set(JS_ENGINE_LIB ReactAndroid::jscexecutor)
JS_ENGINE_LIB else()
jscexecutor # Use JSC
PATHS ${LIBRN_DIR} find_library(
NO_CMAKE_FIND_ROOT_PATH JS_ENGINE_LIB
) jscexecutor
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
endif()
target_link_libraries( target_link_libraries(
${PACKAGE_NAME} ${PACKAGE_NAME}
${JS_ENGINE_LIB} ${JS_ENGINE_LIB}
@ -127,12 +178,14 @@ else()
file (GLOB LIBREANIMATED_DIR "${BUILD_DIR}/react-native-reanimated-*-jsc.aar/jni/${ANDROID_ABI}") file (GLOB LIBREANIMATED_DIR "${BUILD_DIR}/react-native-reanimated-*-jsc.aar/jni/${ANDROID_ABI}")
endif() endif()
find_library( if(${REACT_NATIVE_VERSION} LESS 71)
FBJNI_LIB find_library(
fbjni FBJNI_LIB
PATHS ${LIBRN_DIR} fbjni
NO_CMAKE_FIND_ROOT_PATH PATHS ${LIBRN_DIR}
) NO_CMAKE_FIND_ROOT_PATH
)
endif()
if(${REACT_NATIVE_VERSION} LESS 69) if(${REACT_NATIVE_VERSION} LESS 69)
find_library( find_library(
@ -141,7 +194,7 @@ if(${REACT_NATIVE_VERSION} LESS 69)
PATHS ${LIBRN_DIR} PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH NO_CMAKE_FIND_ROOT_PATH
) )
else() elseif(${REACT_NATIVE_VERSION} LESS 71)
find_library( find_library(
FOLLY_LIB FOLLY_LIB
folly_runtime folly_runtime
@ -150,14 +203,25 @@ else()
) )
endif() endif()
find_library( if(${REACT_NATIVE_VERSION} LESS 71)
REACT_NATIVE_JNI_LIB find_library(
reactnativejni REACT_NATIVE_JNI_LIB
PATHS ${LIBRN_DIR} reactnativejni
NO_CMAKE_FIND_ROOT_PATH PATHS ${LIBRN_DIR}
) NO_CMAKE_FIND_ROOT_PATH
)
endif()
if(${REACT_NATIVE_VERSION} LESS 66) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::folly_runtime
ReactAndroid::glog
ReactAndroid::jsi
ReactAndroid::reactnativejni
fbjni::fbjni
)
elseif(${REACT_NATIVE_VERSION} LESS 66)
# JSI lib didn't exist on RN 0.65 and before. Simply omit it. # JSI lib didn't exist on RN 0.65 and before. Simply omit it.
set (JSI_LIB "") set (JSI_LIB "")
else() else()

View File

@ -50,7 +50,7 @@ def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME").split("\\
def FOR_HERMES = System.getenv("FOR_HERMES") == "True" def FOR_HERMES = System.getenv("FOR_HERMES") == "True"
rootProject.getSubprojects().forEach({project -> rootProject.getSubprojects().forEach({project ->
if (project.plugins.hasPlugin("com.android.application")) { if (project.plugins.hasPlugin("com.android.application")) {
FOR_HERMES = project.ext.react.enableHermes FOR_HERMES = REACT_NATIVE_VERSION >= 71 && project.hermesEnabled || project.ext.react.enableHermes
} }
}) })
def jsRuntimeDir = { def jsRuntimeDir = {
@ -130,6 +130,12 @@ android {
buildToolsVersion getExtOrDefault('buildToolsVersion') buildToolsVersion getExtOrDefault('buildToolsVersion')
ndkVersion getExtOrDefault('ndkVersion') ndkVersion getExtOrDefault('ndkVersion')
if (REACT_NATIVE_VERSION >= 71) {
buildFeatures {
prefab true
}
}
defaultConfig { defaultConfig {
minSdkVersion 21 minSdkVersion 21
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion') targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
@ -163,7 +169,17 @@ android {
packagingOptions { packagingOptions {
// Exclude all Libraries that are already present in the user's app (through React Native or by him installing REA) // 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", "**/libhermes.so", "**/libfolly_runtime.so"] excludes = ["**/libc++_shared.so",
"**/libfbjni.so",
"**/libjsi.so",
"**/libreactnativejni.so",
"**/libfolly_json.so",
"**/libreanimated.so",
"**/libjscexecutor.so",
"**/libhermes.so",
"**/libfolly_runtime.so",
"**/libglog.so",
]
// META-INF is duplicate by CameraX. // META-INF is duplicate by CameraX.
exclude "META-INF/**" exclude "META-INF/**"
} }
@ -261,26 +277,36 @@ repositories {
def kotlin_version = getExtOrDefault('kotlinVersion') def kotlin_version = getExtOrDefault('kotlinVersion')
dependencies { dependencies {
// noinspection GradleDynamicVersion if (REACT_NATIVE_VERSION >= 71) {
implementation 'com.facebook.react:react-native:+' implementation "com.facebook.react:react-android:"
implementation "com.facebook.react:hermes-android:"
} else {
// noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'
}
if (ENABLE_FRAME_PROCESSORS) { if (ENABLE_FRAME_PROCESSORS) {
implementation project(':react-native-reanimated') implementation project(':react-native-reanimated')
//noinspection GradleDynamicVersion if (REACT_NATIVE_VERSION < 71) {
extractHeaders("com.facebook.fbjni:fbjni:+:headers") //noinspection GradleDynamicVersion
//noinspection GradleDynamicVersion extractHeaders("com.facebook.fbjni:fbjni:headers:+")
extractJNI("com.facebook.fbjni:fbjni:+") //noinspection GradleDynamicVersion
extractJNI("com.facebook.fbjni:fbjni:+")
def rnAarMatcher = "**/react-native/**/*${resolveBuildType()}.aar" def rnAarMatcher = "**/react-native/**/*${resolveBuildType()}.aar"
if (REACT_NATIVE_VERSION < 69) { if (REACT_NATIVE_VERSION < 69) {
rnAarMatcher = "**/**/*.aar" rnAarMatcher = "**/**/*.aar"
}
def rnAAR = fileTree("$reactNative/android").matching({ it.include rnAarMatcher }).singleFile
def jscAAR = fileTree("${nodeModules}/jsc-android/dist/org/webkit/android-jsc").matching({ it.include "**/**/*.aar" }).singleFile
extractJNI(files(rnAAR, jscAAR))
} }
def rnAAR = fileTree("$reactNative/android").matching({ it.include rnAarMatcher }).singleFile
def jscAAR = fileTree("${nodeModules}/jsc-android/dist/org/webkit/android-jsc").matching({ it.include "**/**/*.aar" }).singleFile
def jsEngine = FOR_HERMES ? "hermes" : "jsc" def jsEngine = FOR_HERMES ? "hermes" : "jsc"
def reaAAR = "${nodeModules}/react-native-reanimated/android/react-native-reanimated-${REACT_NATIVE_VERSION}-${jsEngine}.aar" def reaAAR = "${nodeModules}/react-native-reanimated/android/react-native-reanimated-${REACT_NATIVE_VERSION}-${jsEngine}.aar"
extractJNI(files(rnAAR, jscAAR, reaAAR)) extractJNI(files(reaAAR))
} }
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
@ -600,11 +626,13 @@ if (ENABLE_FRAME_PROCESSORS) {
tasks.whenTaskAdded { task -> tasks.whenTaskAdded { task ->
if (!task.name.contains('Clean') && (task.name.contains('externalNative') || task.name.contains('CMake'))) { if (!task.name.contains('Clean') && (task.name.contains('externalNative') || task.name.contains('CMake'))) {
task.dependsOn(extractAARHeaders)
task.dependsOn(extractJNIFiles) task.dependsOn(extractJNIFiles)
task.dependsOn(prepareJSC) if (REACT_NATIVE_VERSION < 71) {
task.dependsOn(prepareHermes) task.dependsOn(extractAARHeaders)
task.dependsOn(prepareThirdPartyNdkHeaders) task.dependsOn(prepareThirdPartyNdkHeaders)
task.dependsOn(prepareJSC)
task.dependsOn(prepareHermes)
}
} }
} }
} }