Create reactive native module in C or C ++ using Android NDK?

Should I create my own Java interface (JNI) to access C or C ++ code from an adaptive native module for Android?

My goal is to reuse common C and C ++ algorithms (non-user interface) in an adaptive Native Module that supports both Android and iOS. Just call C from the Objective C * .m module or C ++ from the Objective C ++ * .mm module. However, the native-native Native Module for Android implements the Native Module code in Java.

https://facebook.imtqy.com/react-native/docs/native-modules-android.html#content

Android NDK allows you to write Android code in C or C ++. Android NDK works well with C ++ frameworks such as Qt 5.6 . I don't understand how can I cross javascript to respond to my own Native Module, avoiding java JNI?

Thanks in advance for any advice or guidance,

+6
source share
2 answers

I had to do this recently in a React Native project, and I could not find a way around it without using JNI. To do this, you basically need to make the code for the JS to Java bridge, and then more code for the Java-C bridge (via JNI), and then return to JS.

If you only pass primitives, then this is pretty easy, since JNI can deal with type conversion, like Java int, to jint (which is an int typedef) in C, but if you pass complex data types, then you need to write code, to get the C structure from the JJI job by telling the JNI which fields you have and what types these fields are. It was easier for me to write utility functions for this. This is a lot of initial setup and boring code, but as soon as you go and get used to it, it is not so difficult. JNI Spec is great for reference, it just doesn't tell you how you should use it.

I have a github project that uses JNI to traverse some class classes. This blog article explains all the configuration steps for JNI, and if you're comfortable with it, it gets into the dirty details of β€œReal World JNI.” Let me know if you have any questions about this.

+12
source

If you are ready to do a little extra work, you can use Djinni .

This project creates interface bindings for Objective-C and Java. By creating shell code for C ++, you can create Java and Objective-C classes that integrate with React Native. So React will call Obj-C / Java, which will call C ++. Profit.

It will be great to see how this works for you, I have not yet seen anyone do it.

+2
source

All Articles