Skip to content

Commit

Permalink
Merge pull request #9 from numandev1/fix/android
Browse files Browse the repository at this point in the history
fix: android build for less than 71 version
  • Loading branch information
numandev1 authored May 11, 2023
2 parents eb28263 + de0264b commit 1c5475a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 87 deletions.
147 changes: 89 additions & 58 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,62 +1,93 @@
project("react-native-keys")
project(react-native-keys)
cmake_minimum_required(VERSION 3.9.0)

set(PACKAGE_NAME "cpp")
set(BUILD_DIR ./build)
set(PACKAGE_NAME "react-native-keys")
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)

# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)

include_directories(
${PACKAGE_NAME}
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"./cpp"
${FOLLY_INCLUDE_DIR}
"."
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
)

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

add_library(
${PACKAGE_NAME}
SHARED
./cpp/mediator.cpp
./cpp/crypto.cpp
./cpp-adapter.cpp
)

find_library(
LOG_LIB
log
)

find_library(
REACT_NATIVE_JNI_LIB
reactnativejni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)

set_target_properties(
${PACKAGE_NAME} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::turbomodulejsijni
fbjni::fbjni
${LOG_LIB}
ReactAndroid::jsi
ReactAndroid::reactnativejni
ReactAndroid::react_nativemodule_core
android
)
if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)

include_directories(
${PACKAGE_NAME}
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"./cpp"
${FOLLY_INCLUDE_DIR}
"."
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
)

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

add_library(
${PACKAGE_NAME}
SHARED
./cpp/mediator.cpp
./cpp/crypto.cpp
./cpp-adapter.cpp
)

find_library(
LOG_LIB
log
)

find_library(
REACT_NATIVE_JNI_LIB
reactnativejni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)

set_target_properties(
${PACKAGE_NAME} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::turbomodulejsijni
fbjni::fbjni
${LOG_LIB}
ReactAndroid::jsi
ReactAndroid::reactnativejni
ReactAndroid::react_nativemodule_core
android
)
else()
add_library(
${PACKAGE_NAME}
SHARED
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
./cpp/mediator.cpp
./cpp/crypto.cpp
./cpp-adapter.cpp
)

include_directories(
../../react-native/React
../../react-native/React/Base
../../react-native/ReactCommon/jsi
./cpp
)

set_target_properties(
${PACKAGE_NAME} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(
${PACKAGE_NAME}
android
)
endif()
9 changes: 8 additions & 1 deletion android/RNKeys.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def getCurrentFlavor() {
return flavor
}

def safeExtGet(prop, fallback) {
project.ext.has(prop) ? project.ext.get(prop) : fallback
}

def loadKeys(){
def flavor = getCurrentFlavor()
def keysFile = "keys.development.json"
Expand All @@ -40,7 +44,10 @@ def loadKeys(){
}
}
}
if(project.ext.IS_EXAMPLE)

def IS_EXAMPLE=safeExtGet("IS_EXAMPLE",false);

if(IS_EXAMPLE)
{
exportCommand=exportCommand+"export IS_EXAMPLE='TRUE' && ../../keysAndroid.js"
}
Expand Down
66 changes: 55 additions & 11 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@ buildscript {

apply plugin: 'com.android.library'

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def resolveReactNativeDirectory() {
def reactNativeLocation = safeExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
if (reactNativeLocation != null) {
return file(reactNativeLocation)
}

// monorepo workaround
// react-native can be hoisted or in project's own node_modules
def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native")
if (reactNativeFromProjectNodeModules.exists()) {
return reactNativeFromProjectNodeModules
}

def reactNativeFromNodeModulesWithReanimated = file("${projectDir}/../../react-native")
if (reactNativeFromNodeModulesWithReanimated.exists()) {
return reactNativeFromNodeModulesWithReanimated
}

throw new Exception(
"[react-native-keys] Unable to resolve react-native location in " +
"node_modules. You should add project extension property (in app/build.gradle) " +
"`REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
)
}

def REACT_NATIVE_DIR = resolveReactNativeDirectory()

def reactProperties = new Properties()
file("$REACT_NATIVE_DIR/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }

def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()


def getExtOrDefault(name, defaultValue) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
}
Expand Down Expand Up @@ -46,9 +84,9 @@ if(!found){
}

if(!found) {
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again.")
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again.")
}

def nodeModulesPath = nodeModulesDir.toString().replace("\\", "/")
Expand All @@ -70,7 +108,9 @@ android {
cmake {
cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
arguments '-DANDROID_STL=c++_shared', "-DNODE_MODULES_DIR=${nodeModulesPath}"
arguments '-DANDROID_STL=c++_shared',
"-DNODE_MODULES_DIR=${nodeModulesPath}",
"-DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION}"
}
}
}
Expand All @@ -86,9 +126,9 @@ android {
}

externalNativeBuild {
cmake {
path "./CMakeLists.txt"
}
cmake {
path "./CMakeLists.txt"
}
}

packagingOptions {
Expand Down Expand Up @@ -116,8 +156,12 @@ repositories {

dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-android:"
implementation "com.facebook.react:hermes-android:"

if (REACT_NATIVE_MINOR_VERSION >= 71) {
implementation "com.facebook.react:react-android:"
implementation "com.facebook.react:hermes-android:"
}
else
{
implementation 'com.facebook.react:react-native:+'
}
}

19 changes: 2 additions & 17 deletions android/src/main/java/com/reactnativekeysjsi/KeysModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public String getName() {
@ReactMethod(isBlockingSynchronousMethod = true)
public boolean install() {
try {
System.loadLibrary("cpp");
System.loadLibrary("react-native-keys");

ReactApplicationContext context = getReactApplicationContext();
nativeInstall(
Expand All @@ -50,26 +50,11 @@ public boolean install() {
}

static{
System.loadLibrary("cpp");
System.loadLibrary("react-native-keys");
}

public static native String getJniJsonStringyfyData(String key);

// public static String getSecureForFromJni(String key) {
// JSONObject jniData = null;
// try {
// if (jniData == null) {
// String privateKey=PrivateKey.privatekey;
// jniData = new JSONObject(getJniJsonStringyfyData(privateKey));
// }
// if (jniData.has(key)) {
// return jniData.getString(key);
// }
// } catch (Exception ignore) {
// return "";
// }
// return "";
// }

public static String getSecureFor(String key) {
JSONObject jniData = null;
Expand Down

0 comments on commit 1c5475a

Please sign in to comment.