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

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.

Related

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

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

Use react native to create a iOS framework and Android library (not app) from common code base

Has anyone had any success using react native to create both iOS and Android apps that can be used as libraries/sub-projects in other iOS and Android native projects that were created without react native? My goal is to create new functionality for an existing app that has both a iOS and Android codebase that was developed without react native.
As a simple example, lets image that I need to create a series of screens and logic to allow a user to 'create a new account'. Ideally I would like to create the new functionality (controller, views, api-client, etc.) using react native and then export it as a self contained iOS and Android app. In addition to being able to run these apps on their own, I would also like to be able to include them in other native iOS/Android projects as sub-projects. The native iOS project would then create a new storyboard/ViewController/segue(s) and link the main-view of the react native iOS App to it.
I know on iOS/xcode I can add a sub-project by drag-and-dropping an .xcodepro into another one. If I did this with a .xcodepro that was created with react native, how would I go about linking the top level view to the existing non-react native iOS project? Is there a similar process on the Android side to achieve the same functionality?
I would like to explore React Native as well. We have similar needs as you - to develop a common library/framework for iOS and Android. Our solution was to code the library in C++ and use it in XCode on iOS and through a JNI (Java Native Interface) converter on Android as well. The JNI is a pain as not everything translates 1 to 1. I researched using Ruby/RubyMotion but that also has a JNI conversion step. There are other languages that do not require a JNI conversion - but from my last read these are also eperimental. Trying all of these will require effort and results are not guaranteed. Assuming aside from the shared library you still want Native for the rest of the App (UI etc), this will require developers with a combination of Swift, React, and Java (which could get expensive) The approach that makes most sense would be to use Swift for the iOS framework and Kotlin for the Android library and use the same style (functional/react-swift/react-java style code) That way, while the libraries are still different, they are coded in the same style so development/maintenance effort on one can be quickly copied/applied to the other.

using "reactive native" with our existing code

I wrote my app using native method, currently I want to use reactive native in our further development to avoid writing two copies of code for iOS and Android.
The problem is we can't completely rewrite all the code, we want to replace the project piece by piece.
So I am wondering if I can write some views of my project using reactive native and others remain native code that we have used a long time.
If this is possible, is there any existing tutorial about how to do this?
You can keep your originally written native code via native modules. You can port your current project pieces by pieces to native modules, then let React-Native have access to it.
I guess best option is, start a new React-Native project and make it access your native modules, you won't have to rewrite everything then.
Here is more info for Android and iOS
There is official documentation to this. Checkout:
https://facebook.github.io/react-native/docs/integration-with-existing-apps.html

How to use a dynamic library in a Visual C++ Android Application

I am trying to create a simple Visual C++ Native Activity Application (Android) that uses a dynamic shared library (.so). It seems to be a common thing, but I still can't find any samples for this. Microsoft provides the TwoLibs sample, which is somewhat similar, but there the library is called from Java code. I need to do this in C++.
I tried to make a native activity and adding a reference to the shared library project.
It compiles fine, but when I run it, the program crashes. It doesn't even matter whether I call any functions from the library.
When I use a static library it doesn't crash.
I have also tried to add a compiled .so library to the list of dependencies wiwth the same success:
Could somebody explain me how this is supposed to be done?

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

Categories

Resources