Skip to content

Commit

Permalink
feat: new architecture support (#649)
Browse files Browse the repository at this point in the history
* feat: rn 70 ios builds with new arch

* feat: android migration wip

* fix: update test-app, react-native and react version, fix codegen config

* fix: change spec for backward compat

* fix: simplify android code and make it work

* fix: ios code

* fix: remove unnecessary folly version

* fix: srcDirs on paper

* refactor: review

* refactor: review

* refactor: node 18

* docs: new arch in readme

* docs: new arch in readme

* refactor: use Spec

---------

Co-authored-by: Wojciech Lewicki <[email protected]>
  • Loading branch information
vonovak and WoLewicki authored May 15, 2023
1 parent 301c551 commit fc1c0f2
Show file tree
Hide file tree
Showing 21 changed files with 3,404 additions and 2,583 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

strategy:
matrix:
node-version: [15.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ A React Native wrapper for:
</tr>
</table>

Requires RN >= 0.63, Android 5.0+ and iOS 11+
Requires RN >= 0.69, Android 5.0+ and iOS 11+

New architecture is supported.

# Table of Contents

Expand Down
74 changes: 40 additions & 34 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
buildscript {
if (project == rootProject) {
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
}
repositories {
google()
mavenCentral()
}
}

apply plugin: 'com.android.library'
def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['ReactNativeDocumentPicker_' + name]).toInteger()
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
def isNewArchitectureEnabled() {
// To opt-in for the New Architecture, you can either:
// - Set `newArchEnabled` to true inside the `gradle.properties` file
// - Invoke gradle with `-newArchEnabled=true`
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'
if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
}


android {
compileSdkVersion safeExtGet('compileSdkVersion', 29)
buildToolsVersion safeExtGet('buildToolsVersion', '29.0.2')
compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')

// Used to override the NDK path/version on internal CI or by allowing
// users to customize the NDK path/version from their root project (e.g. for M1 support)
if (rootProject.hasProperty("ndkPath")) {
ndkPath rootProject.ext.ndkPath
}
if (rootProject.hasProperty("ndkVersion")) {
ndkVersion rootProject.ext.ndkVersion
}

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 21)
targetSdkVersion safeExtGet('targetSdkVersion', 29)
versionCode 1
versionName "1.0"
minSdkVersion getExtOrIntegerDefault('minSdkVersion')
targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
}

buildTypes {
release {
minifyEnabled false
sourceSets.main {
java {
if (!isNewArchitectureEnabled()) {
srcDirs += 'src/paper/java'
}
}
}
lintOptions {
disable 'GradleCompatible'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
google()
mavenLocal()
mavenCentral()
}

dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
implementation 'com.facebook.react:react-native:+' // from node_modules
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -39,8 +37,7 @@
import java.util.List;
import java.util.UUID;

@ReactModule(name = DocumentPickerModule.NAME)
public class DocumentPickerModule extends ReactContextBaseJavaModule {
public class RNDocumentPickerModule extends NativeDocumentPickerSpec {
public static final String NAME = "RNDocumentPicker";
private static final int READ_REQUEST_CODE = 41;
private static final int PICK_DIR_REQUEST_CODE = 42;
Expand All @@ -64,6 +61,14 @@ public class DocumentPickerModule extends ReactContextBaseJavaModule {
private static final String FIELD_TYPE = "type";
private static final String FIELD_SIZE = "size";

private Promise promise;
private String copyTo;

public RNDocumentPickerModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(activityEventListener);
}

private final ActivityEventListener activityEventListener = new BaseActivityEventListener() {
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
Expand All @@ -89,14 +94,6 @@ private String[] readableArrayToStringArray(ReadableArray readableArray) {
return array;
}

private Promise promise;
private String copyTo;

public DocumentPickerModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addActivityEventListener(activityEventListener);
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
Expand Down Expand Up @@ -167,6 +164,11 @@ public void pickDirectory(Promise promise) {
}
}

@Override
public void releaseSecureAccess(ReadableArray uris, Promise promise) {
promise.reject("RNDocumentPicker:releaseSecureAccess", "releaseSecureAccess is not supported on Android");
}

private void onPickDirectoryResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_CANCELED) {
sendError(E_DOCUMENT_PICKER_CANCELED, "User canceled directory picker");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.reactnativedocumentpicker;

import androidx.annotation.Nullable;

import com.facebook.react.TurboReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;

import java.util.HashMap;
import java.util.Map;

public class RNDocumentPickerPackage extends TurboReactPackage {

@Nullable
@Override
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
if (name.equals(RNDocumentPickerModule.NAME)) {
return new RNDocumentPickerModule(reactContext);
} else {
return null;
}
}

@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return () -> {
boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
moduleInfos.put(
RNDocumentPickerModule.NAME,
new ReactModuleInfo(
RNDocumentPickerModule.NAME,
RNDocumentPickerModule.NAME,
// "DocumentPickerModule",
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
isTurboModule // isTurboModule
));
return moduleInfos;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Then it was commited. It is here to support the old architecture.
* If you use the new architecture, this file won't be included and instead will be generated by the codegen.
*
* @generated by codegen project: GenerateModuleJavaSpec.js
*
* @nolint
*/

package com.reactnativedocumentpicker;

import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactModuleWithSpec;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;

public abstract class NativeDocumentPickerSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
public NativeDocumentPickerSpec(ReactApplicationContext reactContext) {
super(reactContext);
}

@ReactMethod
@DoNotStrip
public abstract void pick(ReadableMap options, Promise promise);

@ReactMethod
@DoNotStrip
public abstract void releaseSecureAccess(ReadableArray uris, Promise promise);

@ReactMethod
@DoNotStrip
public abstract void pickDirectory(Promise promise);
}
1 change: 1 addition & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const styles = StyleSheet.create({
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
box: {
width: 60,
Expand Down
33 changes: 17 additions & 16 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
buildscript {
def androidTestAppDir = "../../node_modules/react-native-test-app/android"
apply(from: "${androidTestAppDir}/dependencies.gradle")
def androidTestAppDir = "../../node_modules/react-native-test-app/android"
apply(from: "${androidTestAppDir}/dependencies.gradle")

repositories {
mavenCentral()
google()
}
repositories {
mavenCentral()
google()
}

dependencies {
classpath "com.android.tools.build:gradle:$androidPluginVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
dependencies {
getReactNativeDependencies().each { dependency ->
classpath(dependency)
}
}
}

allprojects {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("${rootDir}/../../node_modules/react-native/android")
}
mavenCentral()
google()
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("${rootDir}/../../node_modules/react-native/android")
}
mavenCentral()
google()
}
}
8 changes: 8 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ android.enableJetifier=true
# Version of Flipper to use with React Native. Default value is whatever React
# Native defaults to. To disable Flipper, set it to `false`.
#FLIPPER_VERSION=0.125.0
FLIPPER_VERSION=false

# Enable Fabric at runtime.
#USE_FABRIC=1

# Enable new architecture, i.e. Fabric + TurboModule - implies USE_FABRIC=1.
# Note that this is incompatible with web debugging.
newArchEnabled=false

# Uncomment the line below if building react-native from source
#ANDROID_NDK_VERSION=21.4.7075529

# Version of Kotlin to build against.
#KOTLIN_VERSION=1.7.10
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit fc1c0f2

Please sign in to comment.