Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register C++ React Native module in Android

I have a C++ React Native module derived from facebook::xplat::module::CxxModule. It is working ok with iOS project but now I'm trying to figure out how to use it from Java. The only documentation I found is the comment in React Native codebase stating that:

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

My question is how to register C++ module in Java using CxxModuleWrapper

like image 572
bjornd Avatar asked Jun 10 '18 07:06

bjornd


1 Answers

Please check this blog for detail: https://medium.com/@kudochien/how-to-write-a-react-native-cxxmodule-59073259f15d. Snippet from the blog:

Export it from native

extern "C" HelloCxxModule* createHelloCxxModule() {   return new HelloCxxModule(); } 

Register it in java

public final class HelloCxxPackage implements ReactPackage {   @Override   public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {     return Arrays.<NativeModule>asList(         // I have librnpackage-hellocxx.so the exported createHelloCxxModule() above.         CxxModuleWrapper.makeDso("rnpackage-hellocxx", "createHelloCxxModule")     );   }   @Override   public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {     return Collections.emptyList();   } } 
like image 52
Geng Jiawen Avatar answered Sep 18 '22 19:09

Geng Jiawen