Registering a Pure C++ Native Module within a library #286
Unanswered
Zach-Dean-Attractions-io
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I came across this video https://www.youtube.com/watch?v=KURIqiCPbco and in there they used an Objective-C file with a |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I've successfully followed the documentation on pure C++ modules and I am trying to create a library, which exposes a pure C++ module. The reason for needing a pure C++ module is that I want to use the
jsi::Runtime
object so that I can create ajsi::HostObject
type and tie the lifecycle of a JavaScript object to an underlying native object (side note - maybe I should usejsi::NativeObject
instead). Thejsi::Runtime
object isn't exposed in the Java/Objective-C implementations.However, I'm struggling when trying to migrate this to a library. The library project create-react-native-library doesn't support pure C++ modules rather the C++ template is implemented in Kotlin/Objective-C and then calls into shared C++ code and they have recently dropped this template callstack/react-native-builder-bob#818.
iOS
Since
0.79.0
of React Native I can register the pure C++ module within the codegen spec usingios.modulesProvider
.I did somewhat get that working, it called into my pure C++ implementation but then crashed with the error
[runtime not ready]: ReferenceError: Property 'window' doesn't yet exist, js engine: hermes
I have a sample project that should reproduce the error using this commit d9a8cbe.
create-react-native-library
doesn't yet support0.79.+
of React Native so it might be something I did wrong in upgrading.As a side question, does the consuming app also need to be running
0.79.+
of React Native for this change to work? I think it does otherwise when I try to build the example app in Xcode I get an error in the module provider thatRCTModuleProvider
could not be found.I did get iOS working by registering the C++ turbo module in the consuming app's
AppDelegate
file, well using an Objective-C++ bridge file to implement the registration, since theAppDelegate
is now a Swift file. This commit 84cc053 demonstrates it, in particular RCTAppDelegate+TurboModule.h & RCTAppDelegate+TurboModule.mmHowever, ideally I don't want the consumer of my library to need to concern themselves with this code, the library should be able to register the module for them.
Android
The main issue I am facing here is how to register the C++ turbo module. In the documentation it is done using the
OnLoad.cpp
file.However, I've been unable to utilise this approach from a library. I have tried including the
OnLoad.cpp
file in my library project but I can't get the app to build because it complains#include <ReactCommon/TurboModule.h>
can not be found. This commit a6ea11c shows my attempt. I imagine I am missing something in my CMake file. I note that I am not usinginclude(${REACT_ANDROID_DIR}/cmake-utils/ReactNative-application.cmake)
because I think${REACT_ANDROID_DIR}
isn't populated in the library project and so it fails to find the file. I am also getting issues at times with the generatedPackageList.java
file looking for a Java implementation of my module, which doesn't exist.Is it just a case of the consuming app needing to register my module for me? Or how is best to approach this situation?
Beta Was this translation helpful? Give feedback.
All reactions