How to call a C function from a react-native code? - android

I have developed a small android application using react-native. For what it was developed, it works fine. Now to accommodate a small requirement, we need to call a library written in C. I am not sure if there will be a way to call the C code from JavaScript! But I still wanted to ask you all.
Is there a way to do this from react-native? Like I want to call some functions in the C library. If this possible, could you please suggest how? How could I start to test a very basic setup?

You are going to have to write some native code, search for how to call a C lib on Android, then look for react native way to link a native code with a js call.
Native Module link https://facebook.github.io/react-native/docs/native-modules-android.html
StackOverflow question on how to run c/cpp code on android how to use c/cpp library/functions in android

Basically it includes 3 parts.
JavaScript to Java or Kotlin
check out React Native's guide to Android Native Module.
Java or Kotlin to C
Calling C functions from Java is enabled with a mechanism called Java Native Interface (JNI) . check out how to add C and C++ code here.
see more here https://thebhwgroup.com/blog/react-native-jni

Related

How to make C++ Dynamic Library work with React Native

I'm working on native android videochat application. We use Java and C++ dynamic libs(.so). And recently we started thinking about moving some parts of the application to React Native. The problem is - I don't know the right way to do it. I've read some articles about this, they all using Djinni and C++ source code(cpp and hpp files) not dynamic libraris. I have managed to use dynamic libraries inside java code following this guide: https://facebook.github.io/react-native/docs/native-modules-android.html
I'm now able to call lib's functions from Java React Modules and I'm using them in React Native JS code. Also libs must work all the time while the application is running and I need to register some Java Objects in Jni to receive callbacks from it. So the final question is - is using Java -> C++ libs -> JS and backwards is good solution? And is there anything I should know about this interaction.

How To Use Generated Matlab Code in Android?

I have generated MATLAB code than contains a lot of C codes and C/C++ headers.I don't know how to use this code with NDK in Android.
1-Should I change c codes for use in Android?
2-How to use this codes in Android Project?
3-Is there any tutorial or book to use generated code in Android Project?
The android can interact with c/c++ code using JNI. Here are some links which may help you understand the Interaction between Native Code and Android Java Language.
Using C and C++ Code in an Android App with the NDK
Add native code to android
and so on...

Interact with Cocos2d-x Scene at android / ios native window

I'd like to know if Its possible to interact with Cocos2d-x Scene?
Like if I press the button, I can call a method inside Cocos2d-x, for example to start an animation.
If yes, please provide me a sample.
Yes, it's possible.
So on iOS: it's very simple, you just need to import the C++ header file in your objective c(mm file) and call. You might need to save the instance somewhere. This still applies to cocos2d-x - Calling C++ method from Objective C
Android: You need to define JNI function on C++ side and call from java.
Please refer this link : http://stnguyen.com/cocos2d-x/call-cpp-functions-from-java.html
For exchanging information between native layer and cocos2d-x versions 3.x and higher you can use this project:
https://github.com/alfonsocejudo/EasyNDK-for-cocos2dx3
It works well for Android, iOS and OSX.

Create react-native Native Module in C or C++ using Android NDK?

Must I create a Java Native Interface (JNI) in order to access C or C++ code from a react-native Native Module for Android?
My goal is to re-use common C and C++ algorithms (non-UI ) in a react-native Native Module that supports both Android and iOS. It is simple to call C from an Objective C *.m module, or C++ from an Objective C++ *.mm module. However, a react-native Native Module for Android implements the Native Module code in java.
https://facebook.github.io/react-native/docs/native-modules-android.html#content
The Android NDK allows you to write Android code in C or C++. The Android NDK works well with C++ frameworks such as Qt 5.6. I do not understand how I can cross the javascript to react-native Native Module while avoiding a java JNI?
Thanks in advance for any tips or direction,
I recently had to do this on a React Native project, and I couldn't find a way around it without using JNI. To do so you essentially have to make code for the JS to Java bridge, and then more code for the Java to C (via JNI) bridge, and then back out to JS.
If you're only passing primitives then it's pretty easy as JNI can deal with type conversion, for example a Java int to a jint (which is an int typedef) in C, but if you're passing complex data types then you have to write code to get a C struct out a JNI jobject by telling JNI what fields your class has and what types those fields are. I found easier to write up utility functions to do this. It's a lot of initial setup and boring code, but once you get going and are used to it it's not that difficult. The 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 pass around some example classes. This blog article explains all the setup steps for JNI, and if you're already comfortable with that, it gets into dirty detail in the "Real World JNI" section. Let me know if you have any questions about it!
If you are willing to do a little extra work you can use Djinni.
This project generates interface bindings for Objective-C and Java. Once you create the wrapper code for C++, you can create Java and Objective-C classes that integrate with React Native. Thus React will call Obj-C/Java which will call C++. Profit.
It will be cool to see how this works for you, I have not seen anyone do this yet.
As mentioned by https://stackoverflow.com/a/36693929/1925631 you can use Djinni, there's even a fork for react-native https://github.com/sulewicz/djinni-react-native/tree/master/example-react-native/ExampleProject

Running JavaScript methods from Java in Android

I am writing application using PhoneGap on Android. I need to call some PhoneGap methods from native Java code. Anyone knows how to do that??
If you write your native code as an Android plugin (see an example here: https://github.com/phonegap/phonegap-plugins/tree/master/Android/BarcodeScanner) then you have access to all of the PhoneGap Java classes and can call them as you like.

Categories

Resources