Skip to content

Commit 1c5475a

Browse files
authored
Merge pull request #9 from numandev1/fix/android
fix: android build for less than 71 version
2 parents eb28263 + de0264b commit 1c5475a

File tree

4 files changed

+154
-87
lines changed

4 files changed

+154
-87
lines changed

android/CMakeLists.txt

Lines changed: 89 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,93 @@
1-
project("react-native-keys")
1+
project(react-native-keys)
22
cmake_minimum_required(VERSION 3.9.0)
33

4-
set(PACKAGE_NAME "cpp")
5-
set(BUILD_DIR ./build)
4+
set(PACKAGE_NAME "react-native-keys")
5+
set(CMAKE_VERBOSE_MAKEFILE ON)
66
set(CMAKE_CXX_STANDARD 17)
7+
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
78

8-
# Consume shared libraries and headers from prefabs
9-
find_package(fbjni REQUIRED CONFIG)
10-
find_package(ReactAndroid REQUIRED CONFIG)
11-
12-
include_directories(
13-
${PACKAGE_NAME}
14-
"${NODE_MODULES_DIR}/react-native/React"
15-
"${NODE_MODULES_DIR}/react-native/React/Base"
16-
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
17-
"./cpp"
18-
${FOLLY_INCLUDE_DIR}
19-
"."
20-
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
21-
)
22-
23-
file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
24-
message(STATUS "LIBRN_DIR: ${LIBRN_DIR}")
25-
26-
add_library(
27-
${PACKAGE_NAME}
28-
SHARED
29-
./cpp/mediator.cpp
30-
./cpp/crypto.cpp
31-
./cpp-adapter.cpp
32-
)
33-
34-
find_library(
35-
LOG_LIB
36-
log
37-
)
38-
39-
find_library(
40-
REACT_NATIVE_JNI_LIB
41-
reactnativejni
42-
PATHS ${LIBRN_DIR}
43-
NO_CMAKE_FIND_ROOT_PATH
44-
)
45-
46-
set_target_properties(
47-
${PACKAGE_NAME} PROPERTIES
48-
CXX_STANDARD 17
49-
CXX_EXTENSIONS OFF
50-
POSITION_INDEPENDENT_CODE ON
51-
)
52-
53-
target_link_libraries(
54-
${PACKAGE_NAME}
55-
ReactAndroid::turbomodulejsijni
56-
fbjni::fbjni
57-
${LOG_LIB}
58-
ReactAndroid::jsi
59-
ReactAndroid::reactnativejni
60-
ReactAndroid::react_nativemodule_core
61-
android
62-
)
9+
if(${REACT_NATIVE_MINOR_VERSION} GREATER_EQUAL 71)
10+
# Consume shared libraries and headers from prefabs
11+
find_package(fbjni REQUIRED CONFIG)
12+
find_package(ReactAndroid REQUIRED CONFIG)
13+
14+
include_directories(
15+
${PACKAGE_NAME}
16+
"${NODE_MODULES_DIR}/react-native/React"
17+
"${NODE_MODULES_DIR}/react-native/React/Base"
18+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
19+
"./cpp"
20+
${FOLLY_INCLUDE_DIR}
21+
"."
22+
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
23+
)
24+
25+
file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
26+
message(STATUS "LIBRN_DIR: ${LIBRN_DIR}")
27+
28+
add_library(
29+
${PACKAGE_NAME}
30+
SHARED
31+
./cpp/mediator.cpp
32+
./cpp/crypto.cpp
33+
./cpp-adapter.cpp
34+
)
35+
36+
find_library(
37+
LOG_LIB
38+
log
39+
)
40+
41+
find_library(
42+
REACT_NATIVE_JNI_LIB
43+
reactnativejni
44+
PATHS ${LIBRN_DIR}
45+
NO_CMAKE_FIND_ROOT_PATH
46+
)
47+
48+
set_target_properties(
49+
${PACKAGE_NAME} PROPERTIES
50+
CXX_STANDARD 17
51+
CXX_EXTENSIONS OFF
52+
POSITION_INDEPENDENT_CODE ON
53+
)
54+
55+
target_link_libraries(
56+
${PACKAGE_NAME}
57+
ReactAndroid::turbomodulejsijni
58+
fbjni::fbjni
59+
${LOG_LIB}
60+
ReactAndroid::jsi
61+
ReactAndroid::reactnativejni
62+
ReactAndroid::react_nativemodule_core
63+
android
64+
)
65+
else()
66+
add_library(
67+
${PACKAGE_NAME}
68+
SHARED
69+
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
70+
./cpp/mediator.cpp
71+
./cpp/crypto.cpp
72+
./cpp-adapter.cpp
73+
)
74+
75+
include_directories(
76+
../../react-native/React
77+
../../react-native/React/Base
78+
../../react-native/ReactCommon/jsi
79+
./cpp
80+
)
81+
82+
set_target_properties(
83+
${PACKAGE_NAME} PROPERTIES
84+
CXX_STANDARD 17
85+
CXX_EXTENSIONS OFF
86+
POSITION_INDEPENDENT_CODE ON
87+
)
88+
89+
target_link_libraries(
90+
${PACKAGE_NAME}
91+
android
92+
)
93+
endif()

android/RNKeys.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def getCurrentFlavor() {
2323
return flavor
2424
}
2525

26+
def safeExtGet(prop, fallback) {
27+
project.ext.has(prop) ? project.ext.get(prop) : fallback
28+
}
29+
2630
def loadKeys(){
2731
def flavor = getCurrentFlavor()
2832
def keysFile = "keys.development.json"
@@ -40,7 +44,10 @@ def loadKeys(){
4044
}
4145
}
4246
}
43-
if(project.ext.IS_EXAMPLE)
47+
48+
def IS_EXAMPLE=safeExtGet("IS_EXAMPLE",false);
49+
50+
if(IS_EXAMPLE)
4451
{
4552
exportCommand=exportCommand+"export IS_EXAMPLE='TRUE' && ../../keysAndroid.js"
4653
}

android/build.gradle

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,44 @@ buildscript {
1717

1818
apply plugin: 'com.android.library'
1919

20+
def safeExtGet(prop, fallback) {
21+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
22+
}
23+
24+
def resolveReactNativeDirectory() {
25+
def reactNativeLocation = safeExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
26+
if (reactNativeLocation != null) {
27+
return file(reactNativeLocation)
28+
}
29+
30+
// monorepo workaround
31+
// react-native can be hoisted or in project's own node_modules
32+
def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native")
33+
if (reactNativeFromProjectNodeModules.exists()) {
34+
return reactNativeFromProjectNodeModules
35+
}
36+
37+
def reactNativeFromNodeModulesWithReanimated = file("${projectDir}/../../react-native")
38+
if (reactNativeFromNodeModulesWithReanimated.exists()) {
39+
return reactNativeFromNodeModulesWithReanimated
40+
}
41+
42+
throw new Exception(
43+
"[react-native-keys] Unable to resolve react-native location in " +
44+
"node_modules. You should add project extension property (in app/build.gradle) " +
45+
"`REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
46+
)
47+
}
48+
49+
def REACT_NATIVE_DIR = resolveReactNativeDirectory()
50+
51+
def reactProperties = new Properties()
52+
file("$REACT_NATIVE_DIR/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
53+
54+
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME")
55+
def REACT_NATIVE_MINOR_VERSION = REACT_NATIVE_VERSION.startsWith("0.0.0-") ? 1000 : REACT_NATIVE_VERSION.split("\\.")[1].toInteger()
56+
57+
2058
def getExtOrDefault(name, defaultValue) {
2159
return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
2260
}
@@ -46,9 +84,9 @@ if(!found){
4684
}
4785

4886
if(!found) {
49-
throw new GradleException(
50-
"${project.name}: unable to locate React Native android sources. " +
51-
"Ensure you have you installed React Native as a dependency in your project and try again.")
87+
throw new GradleException(
88+
"${project.name}: unable to locate React Native android sources. " +
89+
"Ensure you have you installed React Native as a dependency in your project and try again.")
5290
}
5391

5492
def nodeModulesPath = nodeModulesDir.toString().replace("\\", "/")
@@ -70,7 +108,9 @@ android {
70108
cmake {
71109
cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
72110
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
73-
arguments '-DANDROID_STL=c++_shared', "-DNODE_MODULES_DIR=${nodeModulesPath}"
111+
arguments '-DANDROID_STL=c++_shared',
112+
"-DNODE_MODULES_DIR=${nodeModulesPath}",
113+
"-DREACT_NATIVE_MINOR_VERSION=${REACT_NATIVE_MINOR_VERSION}"
74114
}
75115
}
76116
}
@@ -86,9 +126,9 @@ android {
86126
}
87127

88128
externalNativeBuild {
89-
cmake {
90-
path "./CMakeLists.txt"
91-
}
129+
cmake {
130+
path "./CMakeLists.txt"
131+
}
92132
}
93133

94134
packagingOptions {
@@ -116,8 +156,12 @@ repositories {
116156

117157
dependencies {
118158
//noinspection GradleDynamicVersion
119-
implementation "com.facebook.react:react-android:"
120-
implementation "com.facebook.react:hermes-android:"
121-
159+
if (REACT_NATIVE_MINOR_VERSION >= 71) {
160+
implementation "com.facebook.react:react-android:"
161+
implementation "com.facebook.react:hermes-android:"
162+
}
163+
else
164+
{
165+
implementation 'com.facebook.react:react-native:+'
166+
}
122167
}
123-

android/src/main/java/com/reactnativekeysjsi/KeysModule.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public String getName() {
3737
@ReactMethod(isBlockingSynchronousMethod = true)
3838
public boolean install() {
3939
try {
40-
System.loadLibrary("cpp");
40+
System.loadLibrary("react-native-keys");
4141

4242
ReactApplicationContext context = getReactApplicationContext();
4343
nativeInstall(
@@ -50,26 +50,11 @@ public boolean install() {
5050
}
5151

5252
static{
53-
System.loadLibrary("cpp");
53+
System.loadLibrary("react-native-keys");
5454
}
5555

5656
public static native String getJniJsonStringyfyData(String key);
5757

58-
// public static String getSecureForFromJni(String key) {
59-
// JSONObject jniData = null;
60-
// try {
61-
// if (jniData == null) {
62-
// String privateKey=PrivateKey.privatekey;
63-
// jniData = new JSONObject(getJniJsonStringyfyData(privateKey));
64-
// }
65-
// if (jniData.has(key)) {
66-
// return jniData.getString(key);
67-
// }
68-
// } catch (Exception ignore) {
69-
// return "";
70-
// }
71-
// return "";
72-
// }
7358

7459
public static String getSecureFor(String key) {
7560
JSONObject jniData = null;

0 commit comments

Comments
 (0)