When using native modules, it was possible to use them within existing native apps without the necessity of creating a new module:
There are two ways to write a native module for your React Native application:
Directly within your React Native application’s iOS/Android projects
As a NPM package that can be installed as a dependency by your/other React Native applications (https://reactnative.dev/docs/native-modules-intro)
With Turbo Modules, this option is not mentioned at all within the documentation. I know that Turbo Modules are quite cutting-edge, but is it possible to use Turbo Modules directly within native apps, just like native modules? I'd prefer a solution that will still work with CodeGen, if there is one.
Why do I want this? I'm trying to integrate React Native into my existing iOS and Android apps (Brownfield). By using native modules directly within my native apps, I can easily access old implementations/data without having to port them over into standalone modules.
I've tried to move the implementation of the <ModuleName>Spec.java
into my existing android app, but this way the class extending TurboReactPackage
is not able to return a new instance as soon as getModule
is called.
Edit: I got it working. See my command if you want to know how.
By doing some tinkering I figured out, that I can just follow the documentation but do everything inside my existing react native project. I have to provide my own
OnLoad.cpp
and CMakeLists.txt however. This can be done by following thec++
-Documentation of React Native Turbo Modules.Here is some more detailed information on how I achieved this:
Add this to your apps
build.gradle
file:After that place the following two files under
src/main/jni
:CMakeLists.txt
:OnLoad.cpp
:The important part within the
OnLoad.cpp
is this part in thejavaModuleProvider
-Function:That way your Module will be loaded. Keep in mind to update
<YourModuleNameHere>
to fit your module name (both in the import and withinjavaModuleProvider
).