Skip to content

Commit d5b9996

Browse files
committed
💥 Switch to new architecture
Migration Guide: The new minimum supported version of React Native is 0.81, and Expo SDK 54.
1 parent 94ece28 commit d5b9996

File tree

12 files changed

+130
-365
lines changed

12 files changed

+130
-365
lines changed

android/build.gradle

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,67 @@
11
buildscript {
2-
// The Android Gradle plugin is only required when opening the android folder stand-alone.
3-
// This avoids unnecessary downloads and potential conflicts when the library is included as a
4-
// module dependency in an application project.
5-
if (project == rootProject) {
6-
repositories {
7-
mavenCentral()
8-
google()
9-
}
10-
def buildGradleVersion = ext.has('buildGradlePluginVersion') ? ext.get('buildGradlePluginVersion') : '4.2.2'
11-
12-
dependencies {
13-
classpath "com.android.tools.build:gradle:$buildGradleVersion"
14-
}
15-
}
2+
repositories {
3+
google()
4+
mavenCentral()
5+
}
6+
7+
dependencies {
8+
classpath "com.android.tools.build:gradle:8.7.2"
9+
}
1610
}
1711

18-
apply plugin: 'com.android.library'
12+
13+
apply plugin: "com.android.library"
14+
15+
apply plugin: "com.facebook.react"
1916

2017
def safeExtGet(prop, fallback) {
21-
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
18+
return rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
2219
}
2320

2421
android {
25-
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger()
26-
if (agpVersion >= 7) {
27-
namespace 'org.linusu'
28-
}
29-
compileSdkVersion safeExtGet('compileSdkVersion', 30)
30-
buildToolsVersion safeExtGet('buildToolsVersion', '30.0.3')
31-
32-
defaultConfig {
33-
minSdkVersion safeExtGet('minSdkVersion', 16)
34-
targetSdkVersion safeExtGet('targetSdkVersion', 30)
35-
versionCode 1
36-
versionName "1.0"
22+
namespace "org.linusu"
23+
24+
compileSdkVersion safeExtGet("compileSdkVersion", 35)
25+
26+
defaultConfig {
27+
minSdkVersion safeExtGet("minSdkVersion", 24)
28+
targetSdkVersion safeExtGet("targetSdkVersion", 34)
29+
}
30+
31+
buildFeatures {
32+
buildConfig true
33+
}
34+
35+
buildTypes {
36+
release {
37+
minifyEnabled false
3738
}
38-
lintOptions {
39-
abortOnError false
39+
}
40+
41+
lintOptions {
42+
disable "GradleCompatible"
43+
}
44+
45+
compileOptions {
46+
sourceCompatibility JavaVersion.VERSION_1_8
47+
targetCompatibility JavaVersion.VERSION_1_8
48+
}
49+
50+
sourceSets {
51+
main {
52+
java.srcDirs += [
53+
"generated/java",
54+
"generated/jni"
55+
]
4056
}
57+
}
4158
}
4259

4360
repositories {
44-
mavenCentral()
45-
google()
46-
maven {
47-
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
48-
url "$rootDir/../node_modules/react-native/android"
49-
}
61+
mavenCentral()
62+
google()
5063
}
5164

5265
dependencies {
53-
implementation 'com.facebook.react:react-native:+'
66+
implementation "com.facebook.react:react-android"
5467
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="org.linusu">
3-
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
42
</manifest>

android/src/main/java/org/linusu/RNGetRandomValuesModule.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
package org.linusu;
22

3-
import java.security.NoSuchAlgorithmException;
43
import java.security.SecureRandom;
54

65
import android.util.Base64;
76

87
import com.facebook.react.bridge.ReactApplicationContext;
9-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
10-
import com.facebook.react.bridge.ReactMethod;
11-
import com.facebook.react.bridge.Callback;
8+
import com.facebook.react.module.annotations.ReactModule;
129

13-
public class RNGetRandomValuesModule extends ReactContextBaseJavaModule {
14-
15-
private final ReactApplicationContext reactContext;
10+
@ReactModule(name = RNGetRandomValuesModule.NAME)
11+
public class RNGetRandomValuesModule extends NativeRNGetRandomValuesSpec {
12+
protected static final String NAME = "RNGetRandomValues";
1613

1714
public RNGetRandomValuesModule(ReactApplicationContext reactContext) {
1815
super(reactContext);
19-
this.reactContext = reactContext;
2016
}
2117

2218
@Override
2319
public String getName() {
24-
return "RNGetRandomValues";
20+
return NAME;
2521
}
2622

27-
@ReactMethod(isBlockingSynchronousMethod = true)
28-
public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException {
29-
byte[] data = new byte[byteLength];
23+
@Override
24+
public String getRandomBase64(double byteLength) {
25+
byte[] data = new byte[(int) byteLength];
3026
SecureRandom random = new SecureRandom();
3127

3228
random.nextBytes(data);
Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
package org.linusu;
22

3-
import java.util.Arrays;
4-
import java.util.Collections;
5-
import java.util.List;
3+
import java.util.HashMap;
4+
import java.util.Map;
65

7-
import com.facebook.react.ReactPackage;
6+
import com.facebook.react.BaseReactPackage;
87
import com.facebook.react.bridge.NativeModule;
98
import com.facebook.react.bridge.ReactApplicationContext;
10-
import com.facebook.react.uimanager.ViewManager;
11-
import com.facebook.react.bridge.JavaScriptModule;
9+
import com.facebook.react.module.model.ReactModuleInfo;
10+
import com.facebook.react.module.model.ReactModuleInfoProvider;
1211

13-
public class RNGetRandomValuesPackage implements ReactPackage {
12+
public class RNGetRandomValuesPackage extends BaseReactPackage {
1413
@Override
15-
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16-
return Arrays.<NativeModule>asList(new RNGetRandomValuesModule(reactContext));
17-
}
18-
19-
// Deprecated from RN 0.47
20-
public List<Class<? extends JavaScriptModule>> createJSModules() {
21-
return Collections.emptyList();
14+
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
15+
if (name.equals(RNGetRandomValuesModule.NAME)) {
16+
return new RNGetRandomValuesModule(reactContext);
17+
} else {
18+
return null;
19+
}
2220
}
2321

2422
@Override
25-
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
26-
return Collections.emptyList();
23+
public ReactModuleInfoProvider getReactModuleInfoProvider() {
24+
return new ReactModuleInfoProvider() {
25+
@Override
26+
public Map<String, ReactModuleInfo> getReactModuleInfos() {
27+
Map<String, ReactModuleInfo> map = new HashMap<>();
28+
map.put(RNGetRandomValuesModule.NAME, new ReactModuleInfo(
29+
RNGetRandomValuesModule.NAME, // name
30+
RNGetRandomValuesModule.NAME, // className
31+
false, // canOverrideExistingModule
32+
false, // needsEagerInit
33+
false, // isCXXModule
34+
true // isTurboModule
35+
));
36+
return map;
37+
}
38+
};
2739
}
2840
}

index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const base64Decode = require('fast-base64-decode')
2-
const { NativeModules } = require('react-native')
2+
const { TurboModuleRegistry } = require('react-native')
33

44
class TypeMismatchError extends Error {}
55
class QuotaExceededError extends Error {}
@@ -19,22 +19,17 @@ function insecureRandomValues (array) {
1919
return array
2020
}
2121

22+
let module = null
2223
/**
2324
* @param {number} byteLength
2425
* @returns {string}
2526
*/
2627
function getRandomBase64 (byteLength) {
27-
if (NativeModules.RNGetRandomValues) {
28-
return NativeModules.RNGetRandomValues.getRandomBase64(byteLength)
29-
} else if (NativeModules.ExpoRandom) {
30-
// Expo SDK 41-44
31-
return NativeModules.ExpoRandom.getRandomBase64String(byteLength)
32-
} else if (global.ExpoModules) {
33-
// Expo SDK 45+
34-
return global.ExpoModules.ExpoRandom.getRandomBase64String(byteLength);
35-
} else {
36-
throw new Error('Native module not found')
28+
if (module == null) {
29+
module = TurboModuleRegistry.getEnforcing('RNGetRandomValues')
3730
}
31+
32+
return module.getRandomBase64(byteLength)
3833
}
3934

4035
/**
@@ -50,7 +45,7 @@ function getRandomValues (array) {
5045
}
5146

5247
// Expo SDK 48+
53-
if (global.expo && global.expo.modules && global.expo.modules.ExpoCrypto && global.expo.modules.ExpoCrypto.getRandomValues) {
48+
if (global.expo?.modules?.ExpoCrypto?.getRandomValues) {
5449
// ExpoCrypto.getRandomValues doesn't return the array
5550
global.expo.modules.ExpoCrypto.getRandomValues(array)
5651
return array

ios/RNGetRandomValues.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#import <React/RCTBridgeModule.h>
1+
#import <RNGetRandomValuesSpec/RNGetRandomValuesSpec.h>
2+
3+
@interface RNGetRandomValues : NSObject <NativeRNGetRandomValuesSpec>
24

3-
@interface RNGetRandomValues : NSObject <RCTBridgeModule>
4-
-(NSString*)getRandomBase64:(NSUInteger)byteLength;
55
@end
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#import "RNGetRandomValues.h"
22

33
@implementation RNGetRandomValues
4-
5-
- (dispatch_queue_t)methodQueue
6-
{
7-
return dispatch_get_main_queue();
8-
}
9-
RCT_EXPORT_MODULE()
10-
11-
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString*, getRandomBase64:(NSUInteger)byteLength) {
4+
- (NSString *)getRandomBase64:(double)byteLength {
125
NSMutableData *data = [NSMutableData dataWithLength:byteLength];
136
int result = SecRandomCopyBytes(kSecRandomDefault, byteLength, data.mutableBytes);
147
if (result != errSecSuccess) {
@@ -17,4 +10,15 @@ - (dispatch_queue_t)methodQueue
1710
return [data base64EncodedStringWithOptions:0];
1811
}
1912

13+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
14+
(const facebook::react::ObjCTurboModule::InitParams &)params
15+
{
16+
return std::make_shared<facebook::react::NativeRNGetRandomValuesSpecJSI>(params);
17+
}
18+
19+
+ (NSString *)moduleName
20+
{
21+
return @"RNGetRandomValues";
22+
}
23+
2024
@end

0 commit comments

Comments
 (0)