Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactNative expose C++ native module

We have a lot of business logic written in cross-platform C++. We want to write cross-platform UI for our application and use this business logic to create whole cross-platfrom application.

Is it possible to expose native module purely written in C++ to react-native? We don't want to create wrappers around C++ code in native language (Java or Objective-C). Such wrappers will add more complexity it will make debugging and investigation much harder.

like image 527
vkudelas Avatar asked Dec 30 '15 08:12

vkudelas


2 Answers

I am also looking for a way to do this directly in C++ without writing JNI for Android and Obj-C for iOS. I have found the CxxNativeModule class in the react native source. See the sample implementation SampleCxxModule.

The last thing to figure out is how to register that module in C++. In the JNI for React Native Android, implementation of NativeModule says

NativeModules whose implementation is written in C++ must not provide any Java code (so they can be reused on other platforms), and instead should register themselves using CxxModuleWrapper.

Then implementation of CxxModuleWrapper says

This does nothing interesting, except avoid breaking existing code.

So, this may help you get started. The last thing to figure out is how to register a CxxNativeModule so that it can be used in JS.

like image 156
Tyler Whitman Avatar answered Oct 16 '22 16:10

Tyler Whitman


It seems as though you would need to find the code which is os dependent and write different versions of this code for different operating systems.

Here is a link which suggests how this might be done: How do I check OS with a preprocessor directive?

like image 1
Timothy Dee Avatar answered Oct 16 '22 15:10

Timothy Dee