Skip to content

Commit

Permalink
fix(rn): android works with JSC and Hermes (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
berendsliedrecht authored Jun 1, 2023
1 parent 86395e3 commit 47719c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 95 deletions.
61 changes: 0 additions & 61 deletions wrappers/javascript/indy-vdr-react-native/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ file (GLOB LIBINDY_VDR_DIR "${CMAKE_SOURCE_DIR}/../native/mobile/android/${ANDRO
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 14)

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 (BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
find_package(fbjni REQUIRED CONFIG)
Expand Down Expand Up @@ -69,67 +62,13 @@ else()
"${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
)
endif()

file (GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")

if(${FOR_HERMES})
string(APPEND CMAKE_CXX_FLAGS " -DFOR_HERMES=1")

if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
find_package(hermes-engine REQUIRED CONFIG)
elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 69)
# Bundled Hermes from module `com.facebook.react:hermes-engine` or project `:ReactAndroid:hermes-engine`
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${JS_RUNTIME_DIR}/API"
"${JS_RUNTIME_DIR}/public"
)
else()
# From `hermes-engine` npm package
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"${JS_RUNTIME_DIR}/android/include"
)
endif()

if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
target_link_libraries(
${PACKAGE_NAME}
"hermes-engine::libhermes"
)
else()
target_link_libraries(
${PACKAGE_NAME}
"${BUILD_DIR}/third-party-ndk/hermes/jni/${ANDROID_ABI}/libhermes.so"
)
endif()
else()
file (GLOB LIBJSC_DIR "${BUILD_DIR}/android-jsc*.aar/jni/${ANDROID_ABI}")

if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71)
set(JS_ENGINE_LIB ReactAndroid::jscexecutor)
else()
# Use JSC
find_library(
JS_ENGINE_LIB
jscexecutor
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
endif()
target_link_libraries(
${PACKAGE_NAME}
${JS_ENGINE_LIB}
)
endif()

if(${REACT_NATIVE_VERSION} LESS 71)
find_library(
FBJNI_LIB
Expand Down
52 changes: 18 additions & 34 deletions wrappers/javascript/indy-vdr-react-native/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import org.apache.tools.ant.filters.ReplaceTokens
import java.nio.file.Paths

apply plugin: 'com.android.library'
apply plugin: 'de.undercouch.download'

buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
google()
jcenter()
mavenCentral()
Expand Down Expand Up @@ -58,24 +52,8 @@ file("$nodeModules/react-native/ReactAndroid/gradle.properties").withInputStream
def REACT_NATIVE_FULL_VERSION = reactProperties.getProperty("VERSION_NAME")
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")) {
FOR_HERMES = REACT_NATIVE_VERSION >= 71 && project.hermesEnabled || project.ext.react.enableHermes
}
})

def jsRuntimeDir = {
if (FOR_HERMES) {
if (REACT_NATIVE_VERSION >= 69) {
return Paths.get(CMAKE_NODE_MODULES_DIR, "react-native", "sdks", "hermes")
} else {
return Paths.get(CMAKE_NODE_MODULES_DIR, "hermes-engine")
}
} else {
return Paths.get(CMAKE_NODE_MODULES_DIR, "react-native", "ReactCommon", "jsi")
}
}.call()
apply plugin: 'com.android.library'
apply plugin: 'de.undercouch.download'

def getExt(name) {
return rootProject.ext.get(name)
Expand Down Expand Up @@ -112,9 +90,7 @@ android {
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
arguments "-DANDROID_STL=c++_shared",
"-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}",
"-DNODE_MODULES_DIR=${nodeModules}",
"-DFOR_HERMES=${FOR_HERMES}",
"-DJS_RUNTIME_DIR=${jsRuntimeDir}"
"-DNODE_MODULES_DIR=${nodeModules}"
}
}

Expand All @@ -132,7 +108,6 @@ android {
"**/libjsi.so",
"**/libreactnativejni.so",
"**/libjscexecutor.so",
"**/libhermes.so"
]
}

Expand Down Expand Up @@ -237,10 +212,10 @@ repositories {
dependencies {
if (REACT_NATIVE_VERSION >= 71) {
implementation "com.facebook.react:react-android:"
implementation "com.facebook.react:hermes-android:"
} else {
// noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:' + REACT_NATIVE_FULL_VERSION
}
}

if (REACT_NATIVE_VERSION < 71) {
//noinspection GradleDynamicVersion
Expand All @@ -259,6 +234,15 @@ dependencies {
}
}

def downloadsDir = new File("$buildDir/downloads")

task createNativeDepsDirectories {
doLast {
downloadsDir.mkdirs()
}
}


task extractAARHeaders {
doLast {
configurations.extractHeaders.files.each {
Expand All @@ -272,6 +256,8 @@ task extractAARHeaders {
}
}

extractAARHeaders.mustRunAfter createNativeDepsDirectories

task extractJNIFiles {
doLast {
configurations.extractJNI.files.each {
Expand All @@ -290,9 +276,7 @@ extractJNIFiles.mustRunAfter extractAARHeaders

tasks.whenTaskAdded { task ->
if (!task.name.contains('Clean') && (task.name.contains('externalNative') || task.name.contains('CMake'))) {
if (REACT_NATIVE_VERSION < 71) {
task.dependsOn(extractAARHeaders)
task.dependsOn(extractJNIFiles)
}
task.dependsOn(extractAARHeaders)
task.dependsOn(extractJNIFiles)
}
}

0 comments on commit 47719c2

Please sign in to comment.