Skip to content

Commit 3231a7b

Browse files
committed
feat: RN69.7 android part
1 parent 49ff3b6 commit 3231a7b

25 files changed

+860
-131
lines changed

android/app/build.gradle

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ def jscFlavor = 'org.webkit:android-jsc:+'
121121
def enableHermes = project.ext.react.get("enableHermes", false);
122122

123123
/**
124-
* Architectures to build native code for in debug.
124+
* Architectures to build native code for.
125125
*/
126-
def nativeArchitectures = project.getProperties().get("reactNativeDebugArchitectures")
126+
def reactNativeArchitectures() {
127+
def value = project.getProperties().get("reactNativeArchitectures")
128+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
129+
}
127130

128131
android {
129132
ndkVersion rootProject.ext.ndkVersion
@@ -136,13 +139,80 @@ android {
136139
targetSdkVersion rootProject.ext.targetSdkVersion
137140
versionCode 30
138141
versionName "0.0.30"
142+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
143+
if (isNewArchitectureEnabled()) {
144+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
145+
externalNativeBuild {
146+
ndkBuild {
147+
arguments "APP_PLATFORM=android-21",
148+
"APP_STL=c++_shared",
149+
"NDK_TOOLCHAIN_VERSION=clang",
150+
"GENERATED_SRC_DIR=$buildDir/generated/source",
151+
"PROJECT_BUILD_DIR=$buildDir",
152+
"REACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
153+
"REACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build",
154+
"NODE_MODULES_DIR=$rootDir/../node_modules"
155+
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
156+
cppFlags "-std=c++17"
157+
// Make sure this target name is the same you specify inside the
158+
// src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
159+
targets "rndiffapp_appmodules"
160+
}
161+
}
162+
if (!enableSeparateBuildPerCPUArchitecture) {
163+
ndk {
164+
abiFilters (*reactNativeArchitectures())
165+
}
166+
}
167+
}
168+
}
169+
170+
if (isNewArchitectureEnabled()) {
171+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
172+
externalNativeBuild {
173+
ndkBuild {
174+
path "$projectDir/src/main/jni/Android.mk"
175+
}
176+
}
177+
def reactAndroidProjectDir = project(':ReactAndroid').projectDir
178+
def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
179+
dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
180+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
181+
into("$buildDir/react-ndk/exported")
182+
}
183+
def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
184+
dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
185+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
186+
into("$buildDir/react-ndk/exported")
187+
}
188+
afterEvaluate {
189+
// If you wish to add a custom TurboModule or component locally,
190+
// you should uncomment this line.
191+
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
192+
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
193+
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
194+
// Due to a bug inside AGP, we have to explicitly set a dependency
195+
// between configureNdkBuild* tasks and the preBuild tasks.
196+
// This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
197+
configureNdkBuildRelease.dependsOn(preReleaseBuild)
198+
configureNdkBuildDebug.dependsOn(preDebugBuild)
199+
reactNativeArchitectures().each { architecture ->
200+
tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure {
201+
dependsOn("preDebugBuild")
202+
}
203+
tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure {
204+
dependsOn("preReleaseBuild")
205+
}
206+
}
207+
}
139208
}
209+
140210
splits {
141211
abi {
142212
reset()
143213
enable enableSeparateBuildPerCPUArchitecture
144214
universalApk false // If true, also generate a universal APK
145-
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
215+
include (*reactNativeArchitectures())
146216
}
147217
}
148218
signingConfigs {
@@ -164,11 +234,6 @@ android {
164234
buildTypes {
165235
debug {
166236
signingConfig signingConfigs.debug
167-
if (nativeArchitectures) {
168-
ndk {
169-
abiFilters nativeArchitectures.split(',')
170-
}
171-
}
172237
}
173238
release {
174239
// Caution! In production, you need to generate your own keystore file.
@@ -217,14 +282,31 @@ dependencies {
217282
}
218283

219284
if (enableHermes) {
220-
def hermesPath = "../../node_modules/hermes-engine/android/";
221-
debugImplementation files(hermesPath + "hermes-debug.aar")
222-
releaseImplementation files(hermesPath + "hermes-release.aar")
285+
//noinspection GradleDynamicVersion
286+
implementation("com.facebook.react:hermes-engine:+") { // From node_modules
287+
exclude group:'com.facebook.fbjni'
288+
}
223289
} else {
224290
implementation jscFlavor
225291
}
226292
}
227293

294+
if (isNewArchitectureEnabled()) {
295+
// If new architecture is enabled, we let you build RN from source
296+
// Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
297+
// This will be applied to all the imported transtitive dependency.
298+
configurations.all {
299+
resolutionStrategy.dependencySubstitution {
300+
substitute(module("com.facebook.react:react-native"))
301+
.using(project(":ReactAndroid"))
302+
.because("On New Architecture we're building React Native from source")
303+
substitute(module("com.facebook.react:hermes-engine"))
304+
.using(project(":ReactAndroid:hermes-engine"))
305+
.because("On New Architecture we're building Hermes from source")
306+
}
307+
}
308+
}
309+
228310
// Run this once to be able to run the application with BUCK
229311
// puts all compile dependencies into folder libs for BUCK to use
230312
task copyDownloadableDepsToLibs(type: Copy) {
@@ -234,4 +316,12 @@ task copyDownloadableDepsToLibs(type: Copy) {
234316

235317
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
236318

319+
def isNewArchitectureEnabled() {
320+
// To opt-in for the New Architecture, you can either:
321+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
322+
// - Invoke gradle with `-newArchEnabled=true`
323+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
324+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
325+
}
326+
237327
apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

android/app/src/debug/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
android:usesCleartextTraffic="true"
99
tools:targetApi="28"
1010
tools:ignore="GoogleAppIndexingWarning">
11-
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
11+
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false" />
1212
</application>
1313
</manifest>

android/app/src/debug/java/com/nfcopenreader/ReactNativeFlipper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.facebook.flipper.plugins.network.NetworkFlipperPlugin;
2020
import com.facebook.flipper.plugins.react.ReactFlipperPlugin;
2121
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin;
22+
import com.facebook.react.ReactInstanceEventListener;
2223
import com.facebook.react.ReactInstanceManager;
2324
import com.facebook.react.bridge.ReactContext;
2425
import com.facebook.react.modules.network.NetworkingModule;
@@ -51,7 +52,7 @@ public void apply(OkHttpClient.Builder builder) {
5152
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
5253
if (reactContext == null) {
5354
reactInstanceManager.addReactInstanceEventListener(
54-
new ReactInstanceManager.ReactInstanceEventListener() {
55+
new ReactInstanceEventListener() {
5556
@Override
5657
public void onReactContextInitialized(ReactContext reactContext) {
5758
reactInstanceManager.removeReactInstanceEventListener(this);

android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
android:label="@string/app_name"
1616
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
1717
android:launchMode="singleTask"
18-
android:windowSoftInputMode="adjustResize">
18+
android:windowSoftInputMode="adjustResize"
19+
android:exported="true">
1920
<intent-filter>
2021
<action android:name="android.intent.action.MAIN" />
2122
<category android:name="android.intent.category.LAUNCHER" />

android/app/src/main/java/com/washow/nfcopenrewriter/MainActivity.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.washow.nfcopenrewriter;
22

33
import com.facebook.react.ReactActivity;
4+
import com.facebook.react.ReactActivityDelegate;
5+
import com.facebook.react.ReactRootView;
46

57
public class MainActivity extends ReactActivity {
68

@@ -12,4 +14,35 @@ public class MainActivity extends ReactActivity {
1214
protected String getMainComponentName() {
1315
return "NfcOpenReader";
1416
}
17+
18+
/**
19+
* Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
20+
* you can specify the renderer you wish to use - the new renderer (Fabric) or the old renderer
21+
* (Paper).
22+
*/
23+
@Override
24+
protected ReactActivityDelegate createReactActivityDelegate() {
25+
return new MainActivityDelegate(this, getMainComponentName());
26+
}
27+
28+
public static class MainActivityDelegate extends ReactActivityDelegate {
29+
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
30+
super(activity, mainComponentName);
31+
}
32+
33+
@Override
34+
protected ReactRootView createRootView() {
35+
ReactRootView reactRootView = new ReactRootView(getContext());
36+
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
37+
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
38+
return reactRootView;
39+
}
40+
41+
@Override
42+
protected boolean isConcurrentRootEnabled() {
43+
// If you opted-in for the New Architecture, we enable Concurrent Root (i.e. React 18).
44+
// More on this on https://reactjs.org/blog/2022/03/29/react-v18.html
45+
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
46+
}
47+
}
1548
}

android/app/src/main/java/com/washow/nfcopenrewriter/MainApplication.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.facebook.react.ReactInstanceManager;
88
import com.facebook.react.ReactNativeHost;
99
import com.facebook.react.ReactPackage;
10+
import com.facebook.react.config.ReactFeatureFlags;
1011
import com.facebook.soloader.SoLoader;
12+
import com.washow.nfcopenrewriter.newarchitecture.MainApplicationReactNativeHost;
1113
import java.lang.reflect.InvocationTargetException;
1214
import java.util.List;
1315

@@ -35,14 +37,23 @@ protected String getJSMainModuleName() {
3537
}
3638
};
3739

40+
private final ReactNativeHost mNewArchitectureNativeHost =
41+
new MainApplicationReactNativeHost(this);
42+
3843
@Override
3944
public ReactNativeHost getReactNativeHost() {
40-
return mReactNativeHost;
45+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
46+
return mNewArchitectureNativeHost;
47+
} else {
48+
return mReactNativeHost;
49+
}
4150
}
4251

4352
@Override
4453
public void onCreate() {
4554
super.onCreate();
55+
// If you opted-in for the New Architecture, we enable the TurboModule system
56+
ReactFeatureFlags.useTurboModules = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
4657
SoLoader.init(this, /* native exopackage */ false);
4758
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
4859
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.washow.nfcopenrewriter.newarchitecture;
2+
3+
import android.app.Application;
4+
import androidx.annotation.NonNull;
5+
import com.facebook.react.PackageList;
6+
import com.facebook.react.ReactInstanceManager;
7+
import com.facebook.react.ReactNativeHost;
8+
import com.facebook.react.ReactPackage;
9+
import com.facebook.react.ReactPackageTurboModuleManagerDelegate;
10+
import com.facebook.react.bridge.JSIModulePackage;
11+
import com.facebook.react.bridge.JSIModuleProvider;
12+
import com.facebook.react.bridge.JSIModuleSpec;
13+
import com.facebook.react.bridge.JSIModuleType;
14+
import com.facebook.react.bridge.JavaScriptContextHolder;
15+
import com.facebook.react.bridge.ReactApplicationContext;
16+
import com.facebook.react.bridge.UIManager;
17+
import com.facebook.react.fabric.ComponentFactory;
18+
import com.facebook.react.fabric.CoreComponentsRegistry;
19+
import com.facebook.react.fabric.FabricJSIModuleProvider;
20+
import com.facebook.react.fabric.ReactNativeConfig;
21+
import com.facebook.react.uimanager.ViewManagerRegistry;
22+
import com.washow.nfcopenrewriter.BuildConfig;
23+
import com.washow.nfcopenrewriter.newarchitecture.components.MainComponentsRegistry;
24+
import com.washow.nfcopenrewriter.newarchitecture.modules.MainApplicationTurboModuleManagerDelegate;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
/**
29+
* A {@link ReactNativeHost} that helps you load everything needed for the New Architecture, both
30+
* TurboModule delegates and the Fabric Renderer.
31+
*
32+
* <p>Please note that this class is used ONLY if you opt-in for the New Architecture (see the
33+
* `newArchEnabled` property). Is ignored otherwise.
34+
*/
35+
public class MainApplicationReactNativeHost extends ReactNativeHost {
36+
public MainApplicationReactNativeHost(Application application) {
37+
super(application);
38+
}
39+
40+
@Override
41+
public boolean getUseDeveloperSupport() {
42+
return BuildConfig.DEBUG;
43+
}
44+
45+
@Override
46+
protected List<ReactPackage> getPackages() {
47+
List<ReactPackage> packages = new PackageList(this).getPackages();
48+
// Packages that cannot be autolinked yet can be added manually here, for example:
49+
// packages.add(new MyReactNativePackage());
50+
// TurboModules must also be loaded here providing a valid TurboReactPackage implementation:
51+
// packages.add(new TurboReactPackage() { ... });
52+
// If you have custom Fabric Components, their ViewManagers should also be loaded here
53+
// inside a ReactPackage.
54+
return packages;
55+
}
56+
57+
@Override
58+
protected String getJSMainModuleName() {
59+
return "index";
60+
}
61+
62+
@NonNull
63+
@Override
64+
protected ReactPackageTurboModuleManagerDelegate.Builder
65+
getReactPackageTurboModuleManagerDelegateBuilder() {
66+
// Here we provide the ReactPackageTurboModuleManagerDelegate Builder. This is necessary
67+
// for the new architecture and to use TurboModules correctly.
68+
return new MainApplicationTurboModuleManagerDelegate.Builder();
69+
}
70+
71+
@Override
72+
protected JSIModulePackage getJSIModulePackage() {
73+
return new JSIModulePackage() {
74+
@Override
75+
public List<JSIModuleSpec> getJSIModules(
76+
final ReactApplicationContext reactApplicationContext,
77+
final JavaScriptContextHolder jsContext) {
78+
final List<JSIModuleSpec> specs = new ArrayList<>();
79+
80+
// Here we provide a new JSIModuleSpec that will be responsible of providing the
81+
// custom Fabric Components.
82+
specs.add(
83+
new JSIModuleSpec() {
84+
@Override
85+
public JSIModuleType getJSIModuleType() {
86+
return JSIModuleType.UIManager;
87+
}
88+
89+
@Override
90+
public JSIModuleProvider<UIManager> getJSIModuleProvider() {
91+
final ComponentFactory componentFactory = new ComponentFactory();
92+
CoreComponentsRegistry.register(componentFactory);
93+
94+
// Here we register a Components Registry.
95+
// The one that is generated with the template contains no components
96+
// and just provides you the one from React Native core.
97+
MainComponentsRegistry.register(componentFactory);
98+
99+
final ReactInstanceManager reactInstanceManager = getReactInstanceManager();
100+
101+
ViewManagerRegistry viewManagerRegistry =
102+
new ViewManagerRegistry(
103+
reactInstanceManager.getOrCreateViewManagers(reactApplicationContext));
104+
105+
return new FabricJSIModuleProvider(
106+
reactApplicationContext,
107+
componentFactory,
108+
ReactNativeConfig.DEFAULT_CONFIG,
109+
viewManagerRegistry);
110+
}
111+
});
112+
return specs;
113+
}
114+
};
115+
}
116+
}

0 commit comments

Comments
 (0)