Skip to content

Commit aac14a5

Browse files
committed
🎉 rewrite to C++ with JSI
1 parent 0a48c10 commit aac14a5

16 files changed

+500
-62
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ build/
3838
.gradle
3939
local.properties
4040
*.iml
41+
.cxx
4142

4243
# BUCK
4344
buck-out/

android/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.9.0)
2+
3+
add_library(
4+
reactnativegetrandomvalues
5+
SHARED
6+
../../react-native/ReactCommon/jsi/jsi/jsi.cpp
7+
../cpp/react-native-get-random-values.cpp
8+
../cpp/base64.cpp
9+
./cpp-adapter.cpp
10+
)
11+
12+
include_directories(
13+
../../react-native/React
14+
../../react-native/React/Base
15+
../../react-native/ReactCommon/jsi
16+
../cpp
17+
)
18+
19+
set_target_properties(
20+
reactnativegetrandomvalues PROPERTIES
21+
CXX_STANDARD 17
22+
CXX_EXTENSIONS OFF
23+
POSITION_INDEPENDENT_CODE ON
24+
)
25+
26+
target_link_libraries(
27+
reactnativegetrandomvalues
28+
android
29+
)

android/build.gradle

+29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ buildscript {
66
repositories {
77
mavenCentral()
88
google()
9+
jcenter()
910
}
1011
def buildGradleVersion = ext.has('buildGradlePluginVersion') ? ext.get('buildGradlePluginVersion') : '4.2.2'
1112

@@ -30,19 +31,47 @@ android {
3031
targetSdkVersion safeExtGet('targetSdkVersion', 30)
3132
versionCode 1
3233
versionName "1.0"
34+
35+
externalNativeBuild {
36+
cmake {
37+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
38+
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
39+
}
40+
}
3341
}
42+
43+
externalNativeBuild {
44+
cmake {
45+
path "CMakeLists.txt"
46+
}
47+
}
48+
49+
buildTypes {
50+
release {
51+
minifyEnabled false
52+
}
53+
}
54+
3455
lintOptions {
3556
abortOnError false
57+
disable 'GradleCompatible'
58+
}
59+
60+
compileOptions {
61+
sourceCompatibility JavaVersion.VERSION_1_8
62+
targetCompatibility JavaVersion.VERSION_1_8
3663
}
3764
}
3865

3966
repositories {
67+
mavenLocal()
4068
mavenCentral()
4169
google()
4270
maven {
4371
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
4472
url "$rootDir/../node_modules/react-native/android"
4573
}
74+
jcenter()
4675
}
4776

4877
dependencies {

android/cpp-adapter.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <jni.h>
2+
#include "react-native-get-random-values.h"
3+
4+
extern "C" JNIEXPORT void JNICALL
5+
Java_org_linusu_RNGetRandomValuesModule_nativeInstall(JNIEnv *env, jobject thiz, jlong jsi)
6+
{
7+
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);
8+
9+
if (runtime)
10+
{
11+
reactnativegetrandomvalues::install(*runtime);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
package org.linusu;
22

3-
import java.security.NoSuchAlgorithmException;
4-
import java.security.SecureRandom;
5-
6-
import android.util.Base64;
7-
83
import com.facebook.react.bridge.ReactApplicationContext;
94
import com.facebook.react.bridge.ReactContextBaseJavaModule;
105
import com.facebook.react.bridge.ReactMethod;
11-
import com.facebook.react.bridge.Callback;
126

137
public class RNGetRandomValuesModule extends ReactContextBaseJavaModule {
14-
15-
private final ReactApplicationContext reactContext;
8+
private native void nativeInstall(long jsiPtr, String docDir);
169

1710
public RNGetRandomValuesModule(ReactApplicationContext reactContext) {
1811
super(reactContext);
19-
this.reactContext = reactContext;
2012
}
2113

2214
@Override
@@ -25,12 +17,18 @@ public String getName() {
2517
}
2618

2719
@ReactMethod(isBlockingSynchronousMethod = true)
28-
public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException {
29-
byte[] data = new byte[byteLength];
30-
SecureRandom random = new SecureRandom();
31-
32-
random.nextBytes(data);
33-
34-
return Base64.encodeToString(data, Base64.NO_WRAP);
20+
public boolean install() {
21+
try {
22+
System.loadLibrary("reactnativegetrandomvalues");
23+
24+
ReactApplicationContext context = getReactApplicationContext();
25+
nativeInstall(
26+
context.getJavaScriptContextHolder().get(),
27+
context.getFilesDir().getAbsolutePath()
28+
);
29+
return true;
30+
} catch (Exception exception) {
31+
return false;
32+
}
3533
}
3634
}

0 commit comments

Comments
 (0)