|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +// This C++ file is part of the default configuration used by apps and is placed |
| 9 | +// inside react-native to encapsulate it from user space (so you won't need to |
| 10 | +// touch C++/Cmake code at all on Android). |
| 11 | +// |
| 12 | +// If you wish to customize it (because you want to manually link a C++ library |
| 13 | +// or pass a custom compilation flag) you can: |
| 14 | +// |
| 15 | +// 1. Copy this CMake file inside the `android/app/src/main/jni` folder of your |
| 16 | +// project |
| 17 | +// 2. Copy the OnLoad.cpp (in this same folder) file inside the same folder as |
| 18 | +// above. |
| 19 | +// 3. Extend your `android/app/build.gradle` as follows |
| 20 | +// |
| 21 | +// android { |
| 22 | +// // Other config here... |
| 23 | +// externalNativeBuild { |
| 24 | +// cmake { |
| 25 | +// path "src/main/jni/CMakeLists.txt" |
| 26 | +// } |
| 27 | +// } |
| 28 | +// } |
| 29 | + |
| 30 | +#include <DefaultComponentsRegistry.h> |
| 31 | +#include <DefaultTurboModuleManagerDelegate.h> |
| 32 | +#include <autolinking.h> |
| 33 | +#include <fbjni/fbjni.h> |
| 34 | +#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h> |
| 35 | +#include <rncore.h> |
| 36 | + |
| 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 { |
| 45 | + |
| 46 | +void registerComponents( |
| 47 | + std::shared_ptr<const ComponentDescriptorProviderRegistry> registry) { |
| 48 | + // Custom Fabric Components go here. You can register custom |
| 49 | + // components coming from your App or from 3rd party libraries here. |
| 50 | + // |
| 51 | + // providerRegistry->add(concreteComponentDescriptorProvider< |
| 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 |
| 58 | + |
| 59 | + // And we fallback to the components autolinked |
| 60 | + autolinking_registerProviders(registry); |
| 61 | +} |
| 62 | + |
| 63 | +std::shared_ptr<TurboModule> cxxModuleProvider( |
| 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); |
| 76 | + |
| 77 | + return nullptr; |
| 78 | +} |
| 79 | + |
| 80 | +std::shared_ptr<TurboModule> javaModuleProvider( |
| 81 | + const std::string& name, |
| 82 | + const JavaTurboModule::InitParams& params) { |
| 83 | + // Here you can provide your own module provider for TurboModules coming from |
| 84 | + // either your application or from external libraries. The approach to follow |
| 85 | + // is similar to the following (for a library called `samplelibrary`): |
| 86 | + // |
| 87 | + // auto module = samplelibrary_ModuleProvider(name, params); |
| 88 | + // if (module != nullptr) { |
| 89 | + // return module; |
| 90 | + // } |
| 91 | + // return rncore_ModuleProvider(name, params); |
| 92 | + |
| 93 | + // We link app local modules if available |
| 94 | +#ifdef REACT_NATIVE_APP_MODULE_PROVIDER |
| 95 | + auto module = REACT_NATIVE_APP_MODULE_PROVIDER(name, params); |
| 96 | + if (module != nullptr) { |
| 97 | + return module; |
| 98 | + } |
| 99 | +#endif |
| 100 | + |
| 101 | + // We first try to look up core modules |
| 102 | + if (auto module = rncore_ModuleProvider(name, params)) { |
| 103 | + return module; |
| 104 | + } |
| 105 | + |
| 106 | + // And we fallback to the module providers autolinked |
| 107 | + if (auto module = autolinking_ModuleProvider(name, params)) { |
| 108 | + return module; |
| 109 | + } |
| 110 | + |
| 111 | + return nullptr; |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace facebook::react |
| 115 | + |
| 116 | +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) { |
| 117 | + return facebook::jni::initialize(vm, [] { |
| 118 | + facebook::react::DefaultTurboModuleManagerDelegate::cxxModuleProvider = |
| 119 | + &facebook::react::cxxModuleProvider; |
| 120 | + facebook::react::DefaultTurboModuleManagerDelegate::javaModuleProvider = |
| 121 | + &facebook::react::javaModuleProvider; |
| 122 | + facebook::react::DefaultComponentsRegistry:: |
| 123 | + registerComponentDescriptorsFromEntryPoint = |
| 124 | + &facebook::react::registerComponents; |
| 125 | + }); |
| 126 | +} |
0 commit comments