Skip to content

Commit 771a1ef

Browse files
committed
feat(example): Upgrade react native to v0.69.3
1 parent 1c09662 commit 771a1ef

21 files changed

+656
-437
lines changed

HelloWorld/.bundle/config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BUNDLE_PATH: "vendor/bundle"
2+
BUNDLE_FORCE_RUBY_PLATFORM: 1

HelloWorld/.prettierrc.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ module.exports = {
22
arrowParens: 'avoid',
33
bracketSameLine: true,
44
bracketSpacing: false,
5-
tabWidth: 2,
65
singleQuote: true,
76
trailingComma: 'all',
8-
arrowParens: 'avoid',
7+
tabWidth: 2,
98
printWidth: 160,
109
};

HelloWorld/.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.7.5

HelloWorld/android/app/build.gradle

+103-12
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ def jscFlavor = 'org.webkit:android-jsc:+'
114114
/**
115115
* Whether to enable the Hermes VM.
116116
*
117-
* This should be set on project.ext.react and mirrored here. If it is not set
117+
* This should be set on project.ext.react and that value will be read here. If it is not set
118118
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
119119
* and the benefits of using Hermes will therefore be sharply reduced.
120120
*/
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 1
138141
versionName "1.0"
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 "helloworldapp_appmodules"
160+
}
161+
}
162+
if (!enableSeparateBuildPerCPUArchitecture) {
163+
ndk {
164+
abiFilters (*reactNativeArchitectures())
165+
}
166+
}
167+
}
168+
}
169+
if (isNewArchitectureEnabled()) {
170+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
171+
externalNativeBuild {
172+
ndkBuild {
173+
path "$projectDir/src/main/jni/Android.mk"
174+
}
175+
}
176+
def reactAndroidProjectDir = project(':ReactAndroid').projectDir
177+
def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
178+
dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
179+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
180+
into("$buildDir/react-ndk/exported")
181+
}
182+
def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
183+
dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
184+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
185+
into("$buildDir/react-ndk/exported")
186+
}
187+
afterEvaluate {
188+
// If you wish to add a custom TurboModule or component locally,
189+
// you should uncomment this line.
190+
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
191+
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
192+
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
193+
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 {
@@ -156,11 +226,6 @@ android {
156226
buildTypes {
157227
debug {
158228
signingConfig signingConfigs.debug
159-
if (nativeArchitectures) {
160-
ndk {
161-
abiFilters nativeArchitectures.split(',')
162-
}
163-
}
164229
}
165230
release {
166231
// Caution! In production, you need to generate your own keystore file.
@@ -190,6 +255,7 @@ android {
190255

191256
dependencies {
192257
implementation fileTree(dir: "libs", include: ["*.jar"])
258+
193259
//noinspection GradleDynamicVersion
194260
implementation "com.facebook.react:react-native:+" // From node_modules
195261

@@ -209,14 +275,31 @@ dependencies {
209275
}
210276

211277
if (enableHermes) {
212-
def hermesPath = "../../node_modules/hermes-engine/android/";
213-
debugImplementation files(hermesPath + "hermes-debug.aar")
214-
releaseImplementation files(hermesPath + "hermes-release.aar")
278+
// noinspection GradleDynamicVersion
279+
implementation("com.facebook.react:hermes-engine:+") { // From node_modules
280+
exclude group:'com.facebook.fbjni'
281+
}
215282
} else {
216283
implementation jscFlavor
217284
}
218285
}
219286

287+
if (isNewArchitectureEnabled()) {
288+
// If new architecture is enabled, we let you build RN from source
289+
// Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
290+
// This will be applied to all the imported transtitive dependency.
291+
configurations.all {
292+
resolutionStrategy.dependencySubstitution {
293+
substitute(module("com.facebook.react:react-native"))
294+
.using(project(":ReactAndroid"))
295+
.because("On New Architecture we're building React Native from source")
296+
substitute(module("com.facebook.react:hermes-engine"))
297+
.using(project(":ReactAndroid:hermes-engine"))
298+
.because("On New Architecture we're building Hermes from source")
299+
}
300+
}
301+
}
302+
220303
// Run this once to be able to run the application with BUCK
221304
// puts all compile dependencies into folder libs for BUCK to use
222305
task copyDownloadableDepsToLibs(type: Copy) {
@@ -225,3 +308,11 @@ task copyDownloadableDepsToLibs(type: Copy) {
225308
}
226309

227310
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
311+
312+
def isNewArchitectureEnabled() {
313+
// To opt-in for the New Architecture, you can either:
314+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
315+
// - Invoke gradle with `-newArchEnabled=true`
316+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
317+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
318+
}

HelloWorld/android/app/src/main/java/com/helloworld/MainActivity.java

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class MainActivity extends ReactActivity {
1414
protected String getMainComponentName() {
1515
return "HelloWorld";
1616
}
17-
1817
/**
1918
* Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
2019
* you can specify the renderer you wish to use - the new renderer (Fabric) or the old renderer
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,36 @@
1-
package com.helloworld.newarchitecture;
1+
package com.helloworld.newarchitecture.components;
22

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;
3+
import com.facebook.jni.HybridData;
4+
import com.facebook.proguard.annotations.DoNotStrip;
175
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.helloworld.BuildConfig;
23-
import com.helloworld.newarchitecture.components.MainComponentsRegistry;
24-
import com.helloworld.newarchitecture.modules.MainApplicationTurboModuleManagerDelegate;
25-
import java.util.ArrayList;
26-
import java.util.List;
6+
import com.facebook.soloader.SoLoader;
277

288
/**
29-
* A {@link ReactNativeHost} that helps you load everything needed for the New Architecture, both
30-
* TurboModule delegates and the Fabric Renderer.
9+
* Class responsible to load the custom Fabric Components. This class has native methods and needs a
10+
* corresponding C++ implementation/header file to work correctly (already placed inside the jni/
11+
* folder for you).
3112
*
3213
* <p>Please note that this class is used ONLY if you opt-in for the New Architecture (see the
3314
* `newArchEnabled` property). Is ignored otherwise.
3415
*/
35-
public class MainApplicationReactNativeHost extends ReactNativeHost {
36-
public MainApplicationReactNativeHost(Application application) {
37-
super(application);
16+
@DoNotStrip
17+
public class MainComponentsRegistry {
18+
static {
19+
SoLoader.loadLibrary("fabricjni");
3820
}
3921

40-
@Override
41-
public boolean getUseDeveloperSupport() {
42-
return BuildConfig.DEBUG;
43-
}
22+
@DoNotStrip private final HybridData mHybridData;
4423

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-
}
24+
@DoNotStrip
25+
private native HybridData initHybrid(ComponentFactory componentFactory);
5626

57-
@Override
58-
protected String getJSMainModuleName() {
59-
return "index";
27+
@DoNotStrip
28+
private MainComponentsRegistry(ComponentFactory componentFactory) {
29+
mHybridData = initHybrid(componentFactory);
6030
}
6131

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-
};
32+
@DoNotStrip
33+
public static MainComponentsRegistry register(ComponentFactory componentFactory) {
34+
return new MainComponentsRegistry(componentFactory);
11535
}
11636
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
THIS_DIR := $(call my-dir)
2+
3+
include $(REACT_ANDROID_DIR)/Android-prebuilt.mk
4+
5+
# If you wish to add a custom TurboModule or Fabric component in your app you
6+
# will have to include the following autogenerated makefile.
7+
# include $(GENERATED_SRC_DIR)/codegen/jni/Android.mk
8+
include $(CLEAR_VARS)
9+
10+
LOCAL_PATH := $(THIS_DIR)
11+
12+
# You can customize the name of your application .so file here.
13+
LOCAL_MODULE := helloworldapp_appmodules
14+
15+
LOCAL_C_INCLUDES := $(LOCAL_PATH)
16+
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
17+
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
18+
19+
# If you wish to add a custom TurboModule or Fabric component in your app you
20+
# will have to uncomment those lines to include the generated source
21+
# files from the codegen (placed in $(GENERATED_SRC_DIR)/codegen/jni)
22+
#
23+
# LOCAL_C_INCLUDES += $(GENERATED_SRC_DIR)/codegen/jni
24+
# LOCAL_SRC_FILES += $(wildcard $(GENERATED_SRC_DIR)/codegen/jni/*.cpp)
25+
# LOCAL_EXPORT_C_INCLUDES += $(GENERATED_SRC_DIR)/codegen/jni
26+
27+
# Here you should add any native library you wish to depend on.
28+
LOCAL_SHARED_LIBRARIES := \
29+
libfabricjni \
30+
libfbjni \
31+
libfolly_runtime \
32+
libglog \
33+
libjsi \
34+
libreact_codegen_rncore \
35+
libreact_debug \
36+
libreact_nativemodule_core \
37+
libreact_render_componentregistry \
38+
libreact_render_core \
39+
libreact_render_debug \
40+
libreact_render_graphics \
41+
librrc_view \
42+
libruntimeexecutor \
43+
libturbomodulejsijni \
44+
libyoga
45+
46+
LOCAL_CFLAGS := -DLOG_TAG=\"ReactNative\" -fexceptions -frtti -std=c++17 -Wall
47+
48+
include $(BUILD_SHARED_LIBRARY)

0 commit comments

Comments
 (0)