8

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.

vkudelas
  • 81
  • 2
  • "native language (Java or Objective-C)" at least Java is not native language – Humam Helfawi Dec 30 '15 at 08:35
  • something like this ? https://facebook.github.io/react-native/docs/native-modules-ios.html – Marged Dec 30 '15 at 08:49
  • No the explanation in link shows you how to write Objective-C code to expose it in JS. I want pure C++ code exposed to JS. – vkudelas Dec 31 '15 at 09:40
  • Humam terminology native language is not correct do you have some better idea? Even platform specific language is not correct because Java is using JRE to run. So if you port JRE to other platfom you will be able to run it there. But it is not possible on Objective-C now and it is quite slow... – vkudelas Dec 31 '15 at 09:44
  • I don't think this is possible. You will have to write OS language wrappers. Objective-C++ makes this fairly painless on iOS but JNI is a pain to use..! – Adamski Jul 07 '17 at 13:00

2 Answers2

5

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.

Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30
Tyler Whitman
  • 524
  • 5
  • 11
1

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?

Community
  • 1
  • 1
  • 1
    Timothy I am looking for a way how to expose C++ to JS in react-native without need to write platform specific code (Objective-C/Java). – vkudelas Dec 31 '15 at 09:42