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?
Related
I am trying to create an application that uses Android Native Activity in order to avoid explosing my function code in java, but i meet a problem that i also want to use my another written dynamic library which uses JNI.
I want to know whether i can load such a dynamic library in native activity or not. It seems like that dlopen() can not perform well. Can i use reflection to call System.loadlibrary(), or indeed there exist some dramatic way to solve it?
Thank you for any help you can offer.
NativeActivity gives you all you need to use JNI. You should be able to load your library using dlopen:
dlopen("libname.so", RTLD_LAZY | RTLD_LOCAL)
Make sure to call JNI_OnLoad/JNI_OnUnload if your library has any. This will mimic what System.loadLibrary Java function does.
I have used JNI libraries and Java libraries from NativeActivity-based apps. So if you experience any particular problem with dlopen in this environment, feel free to open a separate question with more detail.
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.
We're trying to use ArcGIS's Android Runtime SDK in NativeScript (it has no nativescript plugin) but we have accessed that rewriting the whole library as a multi-platform plugin would take too much time.
My question is, how can we utilize the native library directly but only the android version of it?
This is the library: https://developers.arcgis.com/android/latest/api-reference/reference/packages.html
Also, is it possible to use it without a custom UI plugin? I don't understand how to add the mapView to the app .xml
For example, in their AndroidStudio tutorial they mention the following steps and I'm not sure how to translate them to NativeScript
Source : https://developers.arcgis.com/android/latest/guide/develop-your-first-map-app.htm
I'm not quite sure what you mean by re-writing the whole library, you never have to do that.
Plugins are being written to wrap the native library with simple user friendly JS api / methods, it necessarily need not to be cross (or multi) platform either.
You may even directly access any third party library within your project as soon you mark them as dependency in your app gradle file.
Here is how you access native apis.
For instance if you want to create an instance of LocatorTask, this should work once you add the library as dependency in your NativeScript project.
const locatorTask = com.esri.arcgisruntime.tasks.geocode.LocatorTask("URI_HERE");
locatorTask.loadAsync();
I am currently working on an android application that evaluate images in different aspects, and I found that there are lots great open source algorithms can be used.
Problem 1: Most of the algorithms are designed on c/c++/matlab languages that cannot be applied directly.
I've search that NDK is a tool that allows us develop android application by other languages, but the setup procedures are quite complicated that I stuck for days. So before I go further on it, I would like to first ask whether I can include other's c/c++ source code directly like calling java library?
Problem 2: For example, I would like to use Point Matching algorithm's source code in my application, but there are lots files inside as it's just source code but not library/plugin. What are the steps to apply the require functions in my android application?
(the most desired way is to blindly input some images to the alogrithm, and it returns the results like calling functions, which I dont have to understand the working principle of it.)
You can't directly use other C++ libraries, you have to build them for Android first using NDK. If there is a version of the library built for Android, then, of course you can use it directly by linking to it using NDK.
You have two options here. First, you create a regular Java application for Android, write a C++ wrapper for handling calls to native side and build the necessary source files using NDK. Your java application will make calls to wrapper using JNI and the wrapper will call your actual C++ functions as in Java->JNI wrapper on C++->Your actual C++ source.
Second option is going fully native, which will leave out Java, JNI calls and the wrapper. You can access your source files directly as if you were writing a desktop C++ application. In the end you will have to build using NDK but this step is required in any case.
As a suggestion, you can also take a look at OpenCV for image processing purposes. It has libraries built for Android, all you will have to do is to link them.
Short version.
Download opencv4android library. Import it in eclipse and see if everything is fine (compile errors, output, etc.).
Secondly, try to import face detection application and try to understand how it works.
It has a java part and a native part.
In order to understand how it works you must understand how java interacts with C++, more or less how NDK works.
One important aspect is to learn how to create your interfaces in C++, based on the native ones defined in java. When you got there then you can try creating your own application.
After that you can come here and ask specific questions.
Can anyone please tell me how to add a C++ file in to an android project? Is there any method to import classes other than java classes?
The answer is that, you can't really add a C++ file directly to a project, but you can compile it and load it into the code that runs in your process and interface to it using the JNI. This is a way to interface native code to Java. However, be aware that you can't really do that much with the JNI. Getting access to standard Android things like UI, Intents, service connections, etc.., these are all somewhat more difficult to use in native code. And you certainly can't take a UNIX app "off the shelf" and stick it on Android by using the JNI. this is a fairly good looking tutorial on the JNI with Android. However, like I said, using the JNI is not an excuse for learning java and the Android SDK. The main reasons people use native code are for utility code (like crypto stuff) and performance (for example, quite a few Android games use the NDK)..
You have to use android NDK. Just download it and refer from android official site.