Skip to content

Commit 390401a

Browse files
authored
Merge pull request #8 from sourcetoad/example-rn-upgrade
Upgrade example to latest RN version (0.68.x)
2 parents cb48cc2 + 573940b commit 390401a

39 files changed

+2354
-1603
lines changed

example/android/app/build.gradle

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,99 @@ def jscFlavor = 'org.webkit:android-jsc:+'
113113
/**
114114
* Whether to enable the Hermes VM.
115115
*
116-
* This should be set on project.ext.react and mirrored here. If it is not set
116+
* This should be set on project.ext.react and that value will be read here. If it is not set
117117
* on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
118118
* and the benefits of using Hermes will therefore be sharply reduced.
119119
*/
120120
def enableHermes = project.ext.react.get("enableHermes", false);
121121

122+
/**
123+
* Architectures to build native code for.
124+
*/
125+
def reactNativeArchitectures() {
126+
def value = project.getProperties().get("reactNativeArchitectures")
127+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
128+
}
129+
122130
android {
131+
ndkVersion rootProject.ext.ndkVersion
123132
compileSdkVersion rootProject.ext.compileSdkVersion
124133

125-
compileOptions {
126-
sourceCompatibility JavaVersion.VERSION_1_8
127-
targetCompatibility JavaVersion.VERSION_1_8
128-
}
129-
130134
defaultConfig {
131135
applicationId "com.example.reactnativeidscansdk"
132136
minSdkVersion rootProject.ext.minSdkVersion
133137
targetSdkVersion rootProject.ext.targetSdkVersion
134138
versionCode 1
135139
versionName "1.0"
140+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
141+
142+
if (isNewArchitectureEnabled()) {
143+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
144+
externalNativeBuild {
145+
ndkBuild {
146+
arguments "APP_PLATFORM=android-21",
147+
"APP_STL=c++_shared",
148+
"NDK_TOOLCHAIN_VERSION=clang",
149+
"GENERATED_SRC_DIR=$buildDir/generated/source",
150+
"PROJECT_BUILD_DIR=$buildDir",
151+
"REACT_ANDROID_DIR=$rootDir/../node_modules/react-native/ReactAndroid",
152+
"REACT_ANDROID_BUILD_DIR=$rootDir/../node_modules/react-native/ReactAndroid/build"
153+
cFlags "-Wall", "-Werror", "-fexceptions", "-frtti", "-DWITH_INSPECTOR=1"
154+
cppFlags "-std=c++17"
155+
// Make sure this target name is the same you specify inside the
156+
// src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
157+
targets "rndiffapp_appmodules"
158+
}
159+
}
160+
}
161+
}
162+
163+
if (isNewArchitectureEnabled()) {
164+
// We configure the NDK build only if you decide to opt-in for the New Architecture.
165+
externalNativeBuild {
166+
ndkBuild {
167+
path "$projectDir/src/main/jni/Android.mk"
168+
}
169+
}
170+
def reactAndroidProjectDir = project(':ReactAndroid').projectDir
171+
def packageReactNdkDebugLibs = tasks.register("packageReactNdkDebugLibs", Copy) {
172+
dependsOn(":ReactAndroid:packageReactNdkDebugLibsForBuck")
173+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
174+
into("$buildDir/react-ndk/exported")
175+
}
176+
def packageReactNdkReleaseLibs = tasks.register("packageReactNdkReleaseLibs", Copy) {
177+
dependsOn(":ReactAndroid:packageReactNdkReleaseLibsForBuck")
178+
from("$reactAndroidProjectDir/src/main/jni/prebuilt/lib")
179+
into("$buildDir/react-ndk/exported")
180+
}
181+
afterEvaluate {
182+
// If you wish to add a custom TurboModule or component locally,
183+
// you should uncomment this line.
184+
// preBuild.dependsOn("generateCodegenArtifactsFromSchema")
185+
preDebugBuild.dependsOn(packageReactNdkDebugLibs)
186+
preReleaseBuild.dependsOn(packageReactNdkReleaseLibs)
187+
// Due to a bug inside AGP, we have to explicitly set a dependency
188+
// between configureNdkBuild* tasks and the preBuild tasks.
189+
// This can be removed once this is solved: https://issuetracker.google.com/issues/207403732
190+
configureNdkBuildRelease.dependsOn(preReleaseBuild)
191+
configureNdkBuildDebug.dependsOn(preDebugBuild)
192+
reactNativeArchitectures().each { architecture ->
193+
tasks.findByName("configureNdkBuildDebug[${architecture}]")?.configure {
194+
dependsOn("preDebugBuild")
195+
}
196+
tasks.findByName("configureNdkBuildRelease[${architecture}]")?.configure {
197+
dependsOn("preReleaseBuild")
198+
}
199+
}
200+
}
136201
}
202+
137203
splits {
138204
abi {
139205
reset()
140206
enable enableSeparateBuildPerCPUArchitecture
141207
universalApk false // If true, also generate a universal APK
142-
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
208+
include (*reactNativeArchitectures())
143209
}
144210
}
145211
signingConfigs {
@@ -167,11 +233,12 @@ android {
167233
variant.outputs.each { output ->
168234
// For each separate APK per architecture, set a unique version code as described here:
169235
// https://developer.android.com/studio/build/configure-apk-splits.html
236+
// Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
170237
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
171238
def abi = output.getFilter(OutputFile.ABI)
172239
if (abi != null) { // null for the universal-debug, universal-release variants
173240
output.versionCodeOverride =
174-
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
241+
defaultConfig.versionCode * 1000 + versionCodes.get(abi)
175242
}
176243

177244
}
@@ -186,7 +253,7 @@ dependencies {
186253

187254
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
188255
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
189-
exclude group:'com.facebook.fbjni'
256+
exclude group:'com.facebook.fbjni'
190257
}
191258
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
192259
exclude group:'com.facebook.flipper'
@@ -207,11 +274,31 @@ dependencies {
207274
implementation project(':reactnativeidscansdk')
208275
}
209276

277+
if (isNewArchitectureEnabled()) {
278+
// If new architecture is enabled, we let you build RN from source
279+
// Otherwise we fallback to a prebuilt .aar bundled in the NPM package.
280+
// This will be applied to all the imported transtitive dependency.
281+
configurations.all {
282+
resolutionStrategy.dependencySubstitution {
283+
substitute(module("com.facebook.react:react-native"))
284+
.using(project(":ReactAndroid")).because("On New Architecture we're building React Native from source")
285+
}
286+
}
287+
}
288+
210289
// Run this once to be able to run the application with BUCK
211290
// puts all compile dependencies into folder libs for BUCK to use
212291
task copyDownloadableDepsToLibs(type: Copy) {
213-
from configurations.compile
292+
from configurations.implementation
214293
into 'libs'
215294
}
216295

217296
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
297+
298+
def isNewArchitectureEnabled() {
299+
// To opt-in for the New Architecture, you can either:
300+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
301+
// - Invoke gradle with `-newArchEnabled=true`
302+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
303+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
304+
}

example/android/app/src/debug/AndroidManifest.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44

55
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
66

7-
<application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />
7+
<application
8+
android:usesCleartextTraffic="true"
9+
tools:targetApi="28"
10+
tools:ignore="GoogleAppIndexingWarning">
11+
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false" />
12+
</application>
813
</manifest>

example/android/app/src/debug/java/com/example/reactnativeidscansdk/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;
@@ -48,7 +49,7 @@ public void apply(OkHttpClient.Builder builder) {
4849
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
4950
if (reactContext == null) {
5051
reactInstanceManager.addReactInstanceEventListener(
51-
new ReactInstanceManager.ReactInstanceEventListener() {
52+
new ReactInstanceEventListener() {
5253
@Override
5354
public void onReactContextInitialized(ReactContext reactContext) {
5455
reactInstanceManager.removeReactInstanceEventListener(this);

example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<activity
1616
android:name=".MainActivity"
1717
android:label="@string/app_name"
18-
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
18+
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
1919
android:launchMode="singleTask"
2020
android:windowSoftInputMode="adjustResize"
2121
android:exported="true">
@@ -24,7 +24,5 @@
2424
<category android:name="android.intent.category.LAUNCHER" />
2525
</intent-filter>
2626
</activity>
27-
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
2827
</application>
29-
3028
</manifest>

example/android/app/src/main/java/com/example/reactnativeidscansdk/MainActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.example.reactnativeidscansdk;
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,25 @@ public class MainActivity extends ReactActivity {
1214
protected String getMainComponentName() {
1315
return "IdscanSdkExample";
1416
}
17+
18+
/**
19+
* Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
20+
* you can specify the rendered you wish to use (Fabric or the older renderer).
21+
*/
22+
@Override
23+
protected ReactActivityDelegate createReactActivityDelegate() {
24+
return new MainActivityDelegate(this, getMainComponentName());
25+
}
26+
public static class MainActivityDelegate extends ReactActivityDelegate {
27+
public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
28+
super(activity, mainComponentName);
29+
}
30+
@Override
31+
protected ReactRootView createRootView() {
32+
ReactRootView reactRootView = new ReactRootView(getContext());
33+
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
34+
reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
35+
return reactRootView;
36+
}
37+
}
1538
}

example/android/app/src/main/java/com/example/reactnativeidscansdk/MainApplication.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.facebook.react.ReactApplication;
77
import com.facebook.react.ReactNativeHost;
88
import com.facebook.react.ReactPackage;
9+
import com.facebook.react.config.ReactFeatureFlags;
10+
import com.example.reactnativeidscansdk.newarchitecture.MainApplicationReactNativeHost;
911
import com.facebook.react.ReactInstanceManager;
1012
import com.facebook.soloader.SoLoader;
1113
import java.lang.reflect.InvocationTargetException;
@@ -37,14 +39,23 @@ protected String getJSMainModuleName() {
3739
}
3840
};
3941

42+
private final ReactNativeHost mNewArchitectureNativeHost =
43+
new MainApplicationReactNativeHost(this);
44+
4045
@Override
4146
public ReactNativeHost getReactNativeHost() {
42-
return mReactNativeHost;
47+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
48+
return mNewArchitectureNativeHost;
49+
} else {
50+
return mReactNativeHost;
51+
}
4352
}
4453

4554
@Override
4655
public void onCreate() {
4756
super.onCreate();
57+
// If you opted-in for the New Architecture, we enable the TurboModule system
58+
ReactFeatureFlags.useTurboModules = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
4859
SoLoader.init(this, /* native exopackage */ false);
4960
initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); // Remove this line if you don't want Flipper enabled
5061
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.example.reactnativeidscansdk.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.EmptyReactNativeConfig;
20+
import com.facebook.react.fabric.FabricJSIModuleProvider;
21+
import com.facebook.react.uimanager.ViewManagerRegistry;
22+
import com.example.reactnativeidscansdk.BuildConfig;
23+
import com.example.reactnativeidscansdk.newarchitecture.components.MainComponentsRegistry;
24+
import com.example.reactnativeidscansdk.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+
new EmptyReactNativeConfig(),
109+
viewManagerRegistry);
110+
}
111+
});
112+
return specs;
113+
}
114+
};
115+
}
116+
}

0 commit comments

Comments
 (0)