Skip to content

Commit 1ca6390

Browse files
committed
Updates OnLoad.cpp and CMakeList.txt to match the RN version samples
1 parent 47d108d commit 1ca6390

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

samples/react-native/android/app/src/main/jni/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,3 @@ project(appmodules)
2929

3030
# This file includes all the necessary to let you build your application with the New Architecture.
3131
include(${REACT_ANDROID_DIR}/cmake-utils/ReactNative-application.cmake)
32-
33-
# App needs to add and link against tm (TurboModules) folder
34-
add_subdirectory(${REACT_ANDROID_DIR}/../../../tm/ tm_build)
35-
target_link_libraries(${CMAKE_PROJECT_NAME} tm)

samples/react-native/android/app/src/main/jni/OnLoad.cpp

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,66 +27,91 @@
2727
// }
2828
// }
2929

30-
#include <AppSpecs.h>
3130
#include <DefaultComponentsRegistry.h>
3231
#include <DefaultTurboModuleManagerDelegate.h>
32+
#include <autolinking.h>
3333
#include <fbjni/fbjni.h>
3434
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
35-
#include <rncli.h>
36-
#include <NativeSampleModule.h>
35+
#include <rncore.h>
3736

38-
namespace facebook {
39-
namespace react {
37+
#ifdef REACT_NATIVE_APP_CODEGEN_HEADER
38+
#include REACT_NATIVE_APP_CODEGEN_HEADER
39+
#endif
40+
#ifdef REACT_NATIVE_APP_COMPONENT_DESCRIPTORS_HEADER
41+
#include REACT_NATIVE_APP_COMPONENT_DESCRIPTORS_HEADER
42+
#endif
43+
44+
namespace facebook::react {
4045

4146
void registerComponents(
42-
std::shared_ptr<ComponentDescriptorProviderRegistry const> registry) {
47+
std::shared_ptr<const ComponentDescriptorProviderRegistry> registry) {
4348
// Custom Fabric Components go here. You can register custom
4449
// components coming from your App or from 3rd party libraries here.
4550
//
4651
// providerRegistry->add(concreteComponentDescriptorProvider<
47-
// AocViewerComponentDescriptor>());
52+
// MyComponentDescriptor>());
53+
54+
// We link app local components if available
55+
#ifdef REACT_NATIVE_APP_COMPONENT_REGISTRATION
56+
REACT_NATIVE_APP_COMPONENT_REGISTRATION(registry);
57+
#endif
4858

49-
// By default we just use the components autolinked by RN CLI
50-
rncli_registerProviders(registry);
59+
// And we fallback to the components autolinked
60+
autolinking_registerProviders(registry);
5161
}
5262

5363
std::shared_ptr<TurboModule> cxxModuleProvider(
54-
const std::string &name,
55-
const std::shared_ptr<CallInvoker> &jsInvoker) {
56-
// Not implemented yet: provide pure-C++ NativeModules here.
57-
if (name == "NativeSampleModule")
58-
{
59-
return std::make_shared<facebook::react::NativeSampleModule>(jsInvoker);
60-
}
61-
return nullptr;
64+
const std::string& name,
65+
const std::shared_ptr<CallInvoker>& jsInvoker) {
66+
// Here you can provide your CXX Turbo Modules coming from
67+
// either your application or from external libraries. The approach to follow
68+
// is similar to the following (for a module called `NativeCxxModuleExample`):
69+
//
70+
// if (name == NativeCxxModuleExample::kModuleName) {
71+
// return std::make_shared<NativeCxxModuleExample>(jsInvoker);
72+
// }
73+
74+
// And we fallback to the CXX module providers autolinked
75+
return autolinking_cxxModuleProvider(name, jsInvoker);
6276
}
6377

6478
std::shared_ptr<TurboModule> javaModuleProvider(
65-
const std::string &name,
66-
const JavaTurboModule::InitParams &params) {
79+
const std::string& name,
80+
const JavaTurboModule::InitParams& params) {
6781
// Here you can provide your own module provider for TurboModules coming from
6882
// either your application or from external libraries. The approach to follow
6983
// is similar to the following (for a library called `samplelibrary`):
7084
//
71-
// auto module = samplelibrary_ModuleProvider(moduleName, params);
85+
// auto module = samplelibrary_ModuleProvider(name, params);
7286
// if (module != nullptr) {
7387
// return module;
7488
// }
75-
// return rncore_ModuleProvider(moduleName, params);
89+
// return rncore_ModuleProvider(name, params);
7690

77-
auto module = AppSpecs_ModuleProvider(name, params);
91+
// We link app local modules if available
92+
#ifdef REACT_NATIVE_APP_MODULE_PROVIDER
93+
auto module = REACT_NATIVE_APP_MODULE_PROVIDER(name, params);
7894
if (module != nullptr) {
7995
return module;
8096
}
97+
#endif
8198

82-
// By default we just use the module providers autolinked by RN CLI
83-
return rncli_ModuleProvider(name, params);
99+
// We first try to look up core modules
100+
if (auto module = rncore_ModuleProvider(name, params)) {
101+
return module;
102+
}
103+
104+
// And we fallback to the module providers autolinked
105+
if (auto module = autolinking_ModuleProvider(name, params)) {
106+
return module;
107+
}
108+
109+
return nullptr;
84110
}
85111

86-
} // namespace react
87-
} // namespace facebook
112+
} // namespace facebook::react
88113

89-
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
114+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
90115
return facebook::jni::initialize(vm, [] {
91116
facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider =
92117
&facebook::react::cxxModuleProvider;
@@ -97,10 +122,3 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
97122
&facebook::react::registerComponents;
98123
});
99124
}
100-
101-
extern "C"
102-
JNIEXPORT void JNICALL
103-
Java_io_sentry_reactnative_sample_SamplePackage_crash(JNIEnv *env, jobject thiz) {
104-
char *ptr = 0;
105-
*ptr += 1;
106-
}

0 commit comments

Comments
 (0)