Skip to content

Commit d50ddac

Browse files
committed
fix: Fix android example error.
1 parent 771a1ef commit d50ddac

12 files changed

Lines changed: 116 additions & 205 deletions

File tree

HelloWorld/android/app/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ android {
140140
versionCode 1
141141
versionName "1.0"
142142
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
143+
143144
if (isNewArchitectureEnabled()) {
144145
// We configure the NDK build only if you decide to opt-in for the New Architecture.
145146
externalNativeBuild {
@@ -156,7 +157,7 @@ android {
156157
cppFlags "-std=c++17"
157158
// Make sure this target name is the same you specify inside the
158159
// src/main/jni/Android.mk file for the `LOCAL_MODULE` variable.
159-
targets "helloworldapp_appmodules"
160+
targets "helloworld_appmodules"
160161
}
161162
}
162163
if (!enableSeparateBuildPerCPUArchitecture) {
@@ -166,6 +167,7 @@ android {
166167
}
167168
}
168169
}
170+
169171
if (isNewArchitectureEnabled()) {
170172
// We configure the NDK build only if you decide to opt-in for the New Architecture.
171173
externalNativeBuild {
@@ -275,7 +277,7 @@ dependencies {
275277
}
276278

277279
if (enableHermes) {
278-
// noinspection GradleDynamicVersion
280+
//noinspection GradleDynamicVersion
279281
implementation("com.facebook.react:hermes-engine:+") { // From node_modules
280282
exclude group:'com.facebook.fbjni'
281283
}
@@ -315,4 +317,4 @@ def isNewArchitectureEnabled() {
315317
// - Invoke gradle with `-newArchEnabled=true`
316318
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
317319
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
318-
}
320+
}

HelloWorld/android/app/src/debug/java/com/helloworld/ReactNativeFlipper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* directory of this source tree.
66
*/
77
package com.helloworld;
8+
89
import android.content.Context;
910
import com.facebook.flipper.android.AndroidFlipperClient;
1011
import com.facebook.flipper.android.utils.FlipperUtils;
@@ -23,15 +24,18 @@
2324
import com.facebook.react.bridge.ReactContext;
2425
import com.facebook.react.modules.network.NetworkingModule;
2526
import okhttp3.OkHttpClient;
27+
2628
public class ReactNativeFlipper {
2729
public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
2830
if (FlipperUtils.shouldEnableFlipper(context)) {
2931
final FlipperClient client = AndroidFlipperClient.getInstance(context);
32+
3033
client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()));
3134
client.addPlugin(new ReactFlipperPlugin());
3235
client.addPlugin(new DatabasesFlipperPlugin(context));
3336
client.addPlugin(new SharedPreferencesFlipperPlugin(context));
3437
client.addPlugin(CrashReporterPlugin.getInstance());
38+
3539
NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
3640
NetworkingModule.setCustomClientBuilder(
3741
new NetworkingModule.CustomClientBuilder() {
@@ -42,6 +46,7 @@ public void apply(OkHttpClient.Builder builder) {
4246
});
4347
client.addPlugin(networkFlipperPlugin);
4448
client.start();
49+
4550
// Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
4651
// Hence we run if after all native modules have been initialized
4752
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
@@ -65,4 +70,4 @@ public void run() {
6570
}
6671
}
6772
}
68-
}
73+
}

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

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

3-
import com.facebook.jni.HybridData;
4-
import com.facebook.proguard.annotations.DoNotStrip;
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;
517
import com.facebook.react.fabric.ComponentFactory;
6-
import com.facebook.soloader.SoLoader;
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;
727

828
/**
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).
29+
* A {@link ReactNativeHost} that helps you load everything needed for the New Architecture, both
30+
* TurboModule delegates and the Fabric Renderer.
1231
*
1332
* <p>Please note that this class is used ONLY if you opt-in for the New Architecture (see the
1433
* `newArchEnabled` property). Is ignored otherwise.
1534
*/
16-
@DoNotStrip
17-
public class MainComponentsRegistry {
18-
static {
19-
SoLoader.loadLibrary("fabricjni");
35+
public class MainApplicationReactNativeHost extends ReactNativeHost {
36+
public MainApplicationReactNativeHost(Application application) {
37+
super(application);
2038
}
2139

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

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

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

32-
@DoNotStrip
33-
public static MainComponentsRegistry register(ComponentFactory componentFactory) {
34-
return new MainComponentsRegistry(componentFactory);
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+
};
35115
}
36116
}

HelloWorld/android/app/src/main/java/com/helloworld/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

HelloWorld/android/app/src/main/jni/Android.mk

Lines changed: 0 additions & 48 deletions
This file was deleted.

HelloWorld/android/app/src/main/jni/MainApplicationTurboModuleManagerDelegate.cpp

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)