Skip to content

Commit 218c51b

Browse files
committed
feat(android): add support new arch for android
1 parent 1d123d2 commit 218c51b

File tree

85 files changed

+6541
-8707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+6541
-8707
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jsconfig.json
1111

1212
# Xcode
1313
#
14+
.cxx/
1415
build/
1516
*.pbxuser
1617
!default.pbxuser

README.md

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
### Why should we use react-native-keys over react-native-config?
66

77
We should use **react-native-keys** instead of **react-native-config** because **react-native-keys** gives guarantee of undecryptable **envs** whereas **react-native-config** **envs** can be decompile and hack
8-
#### See the [ Migration from react-native-config](docs/react-native-config-migration-guide.md)
8+
9+
#### See the [Migration from react-native-config](docs/react-native-config-migration-guide.md)
910

1011
<div align="center">
1112
<img align="right" width="250" style="margin-right: 55px;" src="./media/key.png">
@@ -67,7 +68,7 @@ Create a new file `keys.development.json` in the root of your React Native app a
6768
}
6869
```
6970

70-
**Note: Dont forgot to follow other steps for **Android** and **IOS** to use these keys**
71+
**Note: Dont forgot to follow other steps for**Android**and**IOS**to use these keys**
7172

7273
later on you can define other enviroment files like `keys.staging.json` and `keys.production.json`
7374

@@ -99,7 +100,7 @@ Keep in mind It's [basically impossible to prevent users from reverse engineerin
99100
Install the package:
100101

101102
```
102-
$ yarn add react-native-keys
103+
yarn add react-native-keys
103104
```
104105

105106
Link the library:
@@ -109,7 +110,7 @@ Link the library:
109110
or later. For earlier versions you need to manually link the module.)
110111

111112
```
112-
$ react-native link react-native-keys
113+
react-native link react-native-keys
113114
```
114115

115116
if cocoapods are used in the project then pod has to be installed as well:
@@ -124,12 +125,12 @@ if cocoapods are used in the project then pod has to be installed as well:
124125
125126
- Manual Link (iOS)
126127

127-
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
128-
2. Go to `node_modules``react-native-keys` and add `Keys.xcodeproj`
129-
3. Expand the `Keys.xcodeproj``Products` folder
130-
4. In the project navigator, select your project. Add `Keys.a` to your project's `Build Phases``Link Binary With Libraries`
131-
5. And go the Build Settings tab. Make sure All is toggled on (instead of Basic)
132-
6. Look for Header Search Paths and add `$(SRCROOT)/../node_modules/react-native-keys/ios/**` as `non-recursive`
128+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
129+
2. Go to `node_modules``react-native-keys` and add `Keys.xcodeproj`
130+
3. Expand the `Keys.xcodeproj``Products` folder
131+
4. In the project navigator, select your project. Add `Keys.a` to your project's `Build Phases``Link Binary With Libraries`
132+
5. And go the Build Settings tab. Make sure All is toggled on (instead of Basic)
133+
6. Look for Header Search Paths and add `$(SRCROOT)/../node_modules/react-native-keys/ios/**` as `non-recursive`
133134

134135
- Manual Link (Android)
135136

@@ -144,8 +145,8 @@ if cocoapods are used in the project then pod has to be installed as well:
144145

145146
```diff
146147
dependencies {
147-
implementation "com.facebook.react:react-native:+" // From node_modules
148-
+ implementation project(':react-native-keys')
148+
implementation "com.facebook.react:react-native:+" // From node_modules
149+
+ implementation project(':react-native-keys')
149150
}
150151
```
151152

@@ -156,9 +157,9 @@ if cocoapods are used in the project then pod has to be installed as well:
156157

157158
@Override
158159
protected List<ReactPackage> getPackages() {
159-
return Arrays.asList(
160-
new MainReactPackage()
161-
+ new KeysPackage()
160+
return Arrays.asList(
161+
new MainReactPackage()
162+
+ new KeysPackage()
162163
);
163164
}
164165
```
@@ -175,7 +176,7 @@ you can only read jni key into java file.like this
175176
URL url = new URL(BuildConfig.API_URL); // https://example.com
176177
```
177178

178-
You can also read them from your Gradle configuration:
179+
You can also read them from your Gradle configuration(only public keys):
179180

180181
```groovy
181182
defaultConfig {
@@ -202,7 +203,7 @@ versionCode project.keys.get("VERSION_CODE").toInteger()
202203
```java
203204
import static com.reactnativekeysjsi.KeysModule.getSecureFor;
204205

205-
String secureValue=getSecureFor("BRANCH_KEY"); // key_test_omQ7YYKiq57vOqEJsdcsdfeEsiWkwxE
206+
String secureValue = getSecureFor("BRANCH_KEY"); // key_test_omQ7YYKiq57vOqEJsdcsdfeEsiWkwxE
206207
```
207208

208209
### iOS
@@ -265,10 +266,19 @@ ios/tmp.xcconfig
265266

266267
- Go to _Edit scheme..._ -> _Build_ -> _Pre-actions_, click _+_ and select _New Run Script Action_. Paste below code which will generate KEYS keys on native ios side (into node*modules) Make sure to select your target under \_Provide build settings from*, so `$SRCROOT` environment variables is available to the script.
267268

268-
```
269+
```sh
269270
"${SRCROOT}/../node_modules/react-native-keys/keysIOS.js"
270271
```
271272

273+
Alternatively, you can define a map in `Pre-actions` associating builds with env files:
274+
275+
```sh
276+
export KEYSFILE = "path_to_env"
277+
"${SRCROOT}/../node_modules/react-native-keys/keysIOS.js"
278+
```
279+
280+
call, and use build cases in lowercase, like:
281+
272282
### Different environments
273283

274284
Save config for different environments in different files: `keys.staging.json`, `keys.production.json`, etc.
@@ -278,9 +288,9 @@ By default react-native-keys will read from `keys.development.json`, but you can
278288
The simplest approach is to tell it what file to read with an environment variable, like:
279289

280290
```
281-
$ KEYSFILE=keys.staging.json react-native run-ios # bash
282-
$ SET KEYSFILE=keys.staging.json && react-native run-ios # windows
283-
$ env:KEYSFILE="keys.staging.json"; react-native run-ios # powershell
291+
KEYSFILE=keys.staging.json react-native run-ios # bash
292+
SET KEYSFILE=keys.staging.json && react-native run-ios # windows
293+
env:KEYSFILE="keys.staging.json"; react-native run-ios # powershell
284294
```
285295

286296
This also works for `run-android`. Alternatively, there are platform-specific options below.
@@ -289,13 +299,13 @@ This also works for `run-android`. Alternatively, there are platform-specific op
289299

290300
The same environment variable can be used to assemble releases with a different config:
291301

292-
```
293-
$ cd android && KEYSFILE=keys.staging.json ./gradlew assembleRelease
302+
```sh
303+
cd android && KEYSFILE=keys.staging.json ./gradlew assembleRelease
294304
```
295305

296306
Alternatively, you can define a map in `build.gradle` associating builds with env files. Do it before the `apply from` call, and use build cases in lowercase, like:
297307

298-
```
308+
```groovy
299309
project.ext.keyFiles = [
300310
debug: "keys.development.json",
301311
release: "keys.staging.json",
@@ -309,7 +319,7 @@ apply from: project(':react-native-keys').projectDir.getPath() + "/RNKeys.gradle
309319
In `android/app/build.gradle`, if you use `applicationIdSuffix` or `applicationId` that is different from the package name indicated in `AndroidManifest.xml` in `<manifest package="...">` tag, for example, to support different build variants:
310320
Add this in `android/app/build.gradle`
311321

312-
```
322+
```groovy
313323
defaultConfig {
314324
...
315325
resValue "string", "build_config_package", "YOUR_PACKAGE_NAME_IN_ANDROIDMANIFEST_XML"

android/CMakeLists.txt

Lines changed: 71 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,97 +7,88 @@ set(CMAKE_CXX_STANDARD 17)
77
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
88

99
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)
10+
# Consume shared libraries and headers from prefabs
11+
find_package(ReactAndroid REQUIRED CONFIG)
1312

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-
"./androidcpp"
20-
"../cpp"
21-
${FOLLY_INCLUDE_DIR}
22-
"."
23-
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
24-
)
13+
include_directories(
14+
${PACKAGE_NAME}
15+
"./androidcpp"
16+
"../cpp"
17+
)
2518

26-
file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
27-
message(STATUS "LIBRN_DIR: ${LIBRN_DIR}")
19+
file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
20+
message(STATUS "LIBRN_DIR: ${LIBRN_DIR}")
2821

29-
add_library(
30-
${PACKAGE_NAME}
31-
SHARED
32-
./androidcpp/mediator.cpp
33-
./cpp-adapter.cpp
34-
../cpp/crypto.cpp
35-
../cpp/decryptor.cpp
36-
)
22+
add_library(
23+
${PACKAGE_NAME}
24+
SHARED
25+
./androidcpp/mediator.cpp
26+
./cpp-adapter.cpp
27+
../cpp/crypto.cpp
28+
../cpp/decryptor.cpp
29+
)
3730

38-
find_library(
39-
LOG_LIB
40-
log
41-
)
31+
find_library(
32+
LOG_LIB
33+
log
34+
)
4235

43-
find_library(
44-
REACT_NATIVE_JNI_LIB
45-
reactnativejni
46-
PATHS ${LIBRN_DIR}
47-
NO_CMAKE_FIND_ROOT_PATH
48-
)
36+
find_library(
37+
REACT_NATIVE_JNI_LIB
38+
reactnativejni
39+
PATHS ${LIBRN_DIR}
40+
NO_CMAKE_FIND_ROOT_PATH
41+
)
4942

50-
set_target_properties(
51-
${PACKAGE_NAME} PROPERTIES
52-
CXX_STANDARD 17
53-
CXX_EXTENSIONS OFF
54-
POSITION_INDEPENDENT_CODE ON
55-
)
43+
set_target_properties(
44+
${PACKAGE_NAME} PROPERTIES
45+
CXX_STANDARD 17
46+
CXX_EXTENSIONS OFF
47+
POSITION_INDEPENDENT_CODE ON
48+
)
5649

57-
find_package(openssl REQUIRED CONFIG)
50+
find_package(openssl REQUIRED CONFIG)
5851

59-
target_link_libraries(
60-
${PACKAGE_NAME}
61-
ReactAndroid::turbomodulejsijni
62-
fbjni::fbjni
63-
${LOG_LIB}
64-
ReactAndroid::jsi
65-
ReactAndroid::reactnativejni
66-
ReactAndroid::react_nativemodule_core
67-
android
68-
openssl::crypto
69-
)
52+
target_link_libraries(
53+
${PACKAGE_NAME}
54+
${LOG_LIB}
55+
ReactAndroid::jsi
56+
ReactAndroid::reactnativejni
57+
ReactAndroid::react_nativemodule_core
58+
android
59+
openssl::crypto
60+
)
7061
else()
71-
add_library(
72-
${PACKAGE_NAME}
73-
SHARED
74-
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
75-
./androidcpp/mediator.cpp
76-
./cpp-adapter.cpp
77-
../cpp/crypto.cpp
78-
../cpp/decryptor.cpp
79-
)
62+
add_library(
63+
${PACKAGE_NAME}
64+
SHARED
65+
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
66+
./androidcpp/mediator.cpp
67+
./cpp-adapter.cpp
68+
../cpp/crypto.cpp
69+
../cpp/decryptor.cpp
70+
)
8071

81-
include_directories(
82-
../../react-native/React
83-
../../react-native/React/Base
84-
../../react-native/ReactCommon/jsi
85-
./cpp
86-
../cpp
87-
)
72+
include_directories(
73+
../../react-native/React
74+
../../react-native/React/Base
75+
../../react-native/ReactCommon/jsi
76+
./cpp
77+
../cpp
78+
)
8879

89-
set_target_properties(
90-
${PACKAGE_NAME} PROPERTIES
91-
CXX_STANDARD 17
92-
CXX_EXTENSIONS OFF
93-
POSITION_INDEPENDENT_CODE ON
94-
)
80+
set_target_properties(
81+
${PACKAGE_NAME} PROPERTIES
82+
CXX_STANDARD 17
83+
CXX_EXTENSIONS OFF
84+
POSITION_INDEPENDENT_CODE ON
85+
)
9586

96-
find_package(openssl REQUIRED CONFIG)
87+
find_package(openssl REQUIRED CONFIG)
9788

98-
target_link_libraries(
99-
${PACKAGE_NAME}
100-
android
101-
openssl::crypto
102-
)
89+
target_link_libraries(
90+
${PACKAGE_NAME}
91+
android
92+
openssl::crypto
93+
)
10394
endif()

android/androidcpp/mediator.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ std::string jstring2string(JNIEnv *env, jstring jStr)
1010

1111
const jclass stringClass = env->GetObjectClass(jStr);
1212
const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
13-
const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes,
14-
env->NewStringUTF("UTF-8"));
13+
const jbyteArray stringJbytes = (jbyteArray)env->CallObjectMethod(jStr, getBytes,
14+
env->NewStringUTF("UTF-8"));
1515

16-
size_t length = (size_t) env->GetArrayLength(stringJbytes);
16+
size_t length = (size_t)env->GetArrayLength(stringJbytes);
1717
jbyte *pBytes = env->GetByteArrayElements(stringJbytes, NULL);
1818

19-
std::string ret = std::string((char *) pBytes, length);
19+
std::string ret = std::string((char *)pBytes, length);
2020
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
2121

2222
env->DeleteLocalRef(stringJbytes);
@@ -25,8 +25,9 @@ std::string jstring2string(JNIEnv *env, jstring jStr)
2525
}
2626

2727
extern "C" JNIEXPORT jstring JNICALL
28-
Java_com_reactnativekeysjsi_KeysModule_getJniJsonStringifyData(JNIEnv * env, jobject thiz,jstring key) {
28+
Java_com_reactnativekeysjsi_KeysModule_getJniJsonStringifyData(JNIEnv *env, jobject thiz, jstring key)
29+
{
2930
auto *crypto = new Crypto();
30-
std::string _key{jstring2string(env,key)};
31+
std::string _key{jstring2string(env, key)};
3132
return env->NewStringUTF(crypto->getJniJsonStringifyData(_key).c_str());
3233
}

0 commit comments

Comments
 (0)