So, I have a little C library. I want to make this library available to other devs that develop native C code in android (other libraries, native apps etc).
What's the best way to go? I don't really have much clue yet, trying to figure all this out.
I've read that there are two paths to go with the NDK: using Android.mk and using create-standalone-toolchain -- is this correct? How does each one of these works? Is there a third way without the NDK, and should this be used now that the NDK is available?
**
This may have been asked before, but I understand things have changed since the release of the NDK, as a lot of posts I find seem obsolete.
this question can have a Huge answer, I will try to be as brief as possible.
Using the Android NDK, make a new android project, create a JNI folder, and create an Android.mk file, in it, make two libs, one being your lib, exported as a shared lib and the other, a jniwrapper that will test it. Here is an example of how it was done by a guy for his own lib.
You can make a shared lib by compiling with the standalone toolchain, here is an article on the subject and also take a look at the curl Readme for android, it explains how to compile curl for Android using the standalone toolchain, I believe it's the better way to do it, and also to easier for you since you will use the cross-compiler in a regular fasion...
Related
I have looked at Use prebuilt JNI library in Android Studio 3.1 and How to use .so in a second project in Android?. The first is trying to get a library file without headers working and the other seems to be focusing on a specific issue with his build (although there's some useful information there). I'm relatively new to app development and especially to native development on android. I've gotten a build with the JNI library and some c++ code working, but that seems to be just for building from source.
It's probably a simple answer, but I haven't been able to find documentation on this specifically in the android developers documentation. I'm interested in understanding the correct (or most conventional) place to put and way to use a precompiled library (module/lib/*.so and module/include/*.h) in an android project. Would I even need to use JNI or the NDK if the library is built with another build tool? Another project I have has a native library source object (*.so) in ./obj/local, ./libs, and in many other folders related to JNI. I'm guessing it would be somewhere in there, but I'd like to know what is conventional.
For some context, I'm trying to work with the essentia library. I have followed the guide on compiling for Android and have a build with the general hierarchy mentioned above (essentia/lib and essentia/include) that seems to be working.
I am trying to use SQLCipher within Android. They made it very easy to integrate by adding the dependency:
implementation 'net.zetetic:android-database-sqlcipher:4.2.0#aar'
They have nice and simple examples of then using this in Java, but my application is c++ and I am using the NDK. SQLCipher is primarily C code so I know that this is linking against some compiled C code. Are the headers available for use? Where are these dependencies being installed. I am an iOS developer new to Android so I feel like this should simple but I am just missing something.
There's currently no support for consuming C/C++ dependencies from an AAR. We're working on this here: https://github.com/android-ndk/ndk/issues/916
But I should note that even when that is complete, sqlcipher does need to choose to expose that library. The AAR would not currently contain includes, and it may not be a stable API so they may choose not to expose it.
For a library to be usable directly by ndk, you'd need a .so version of it to link against. If you're including the library like this, you'd use JNI to access it via Java.
I'd recommend against hacking something up to access their .so files directly. Its quite possible their Java code has business logic that prevents errors or initialized things that are not set up properly if you go right against their .so file.
I have been using OpenCV for Android for a long time. It works fine, so far. But now I want to improve the performance and I want to go native. However, even native development itself is a little complicated for me in Android. Now I got the point of it and I can build simple applications by myself.
The point that I am stuck is how to add those libraries and include files in "native" folder of OpenCV Android package.
NOTE:I do not intent to create native application, but use only OpenCV as native. Other parts will still be java
Long story short, I just don't want to call native OpenCV methods with Java wrapper, but instead do everything related to OpenCV in native and then get the result to Java part.
Thanks for any efford in advance.
If you are not using OpenCV from Java at all, the best option is to include the libraries by linking statically with them, and then removing dead code, symbols, etc, to keep the native library size under control. Having to download an additional package is something many users don't like.
Just include
OPENCV_LIB_TYPE:=STATIC
OPENCV_CAMERA_MODULES:=off
include path/to/opencv/sdk/native/jni/OpenCV.mk
in your Android.mk.
Have a look at the OpenCV Android documentation here.
We have developed an iPad application where the core logic is written in CPP code, so that we can use the same code files/libraries to other platforms.
Now I want to use those files and develop similar Android application, but unable to create .so files and integrate paths in Android.mk files and all. I am basically an iOS developer, this is first time I am looking into Android NDK.
Can anyone help and guide if there is any straight forward steps to it.
I have already gone through android developers site and few other tutorial sites. But none of those worked for me.
Require easy-clear steps to call cpp method in java, if I do have few cpp files and .a libraries with me already.
You aren't very specific at the step you are stuck at.
Here's a very quick explanation on how to call native code from java (android) :
first create a method to be exported by the native and called by java (this uses JNI, so google JNI , JNIEXPORT)
once you have this method defined in your native code, it's time to create a shared library (.so) file , using the compiler that comes in the NDK (because you are compiling for android ). You will need to compile for the correct architecture of the device (armeabiv7s is the most common now days).
you need to add the library file in your app.apk inside the armeabi folder (more details in NDK tutorials).
inside your java code you will need to load the shared library via the System.loadLibrary(LIBRARY_NAME);
inside your java code you will need to have defined static native methods that are in pair with the methods you exported from your CPP code
Quick tips :
use C functions,not CPP , since CPP will be mangled in the resulting shared library. In other words, you will need to create a C wrapper that will call your cpp code.
look over a hello world tutorial for NDK , and work yourself from there . Here's a link to such tutorial http://trivedihardik.wordpress.com/2011/06/16/hello-world-example-using-ndk-in-android/
You will bump later on into compilation issues with the makefiles, but by then you will probably be able to be more specific with your question.
Easiest way is to use the hello-jni Android studio sample project.
There are a lot of settings and configurations, you get them from the sample that is a working unit, always easiest when starting from something working.
First run (and modify) the hello-jni and learn how the interactivity between the Java and C parts works. About everything works except environmental ANSI C/C++ stuff. You have to get things like language, country etc from Java and transfer it to the C-code. You are in US in English with "inches and gallons" in JNI.
Then to an own project you create with android studio, copy and modify from it bit by bit from hello-jni. When you have our own branded hello-JNI you can add bit by bit your own code. Often using C-dummies for testing the interactivity with the Java part is easier, then change it to the real C/C++ code of yours.
Read the Android/Android studio documentation and learn and understand. Use the Android emulators, much easier and they are good.
The project configuration stuff is by far the hardest to handle at the start. If I would make a new project today, I would start from the Hello-JNI once again.
I've been working for a month with android ndk, so I am quite a newbie. Currently I am trying to integrate some code into a big project and have multiple problems both with NDK and gstreamer. Questions are stupid but I would be very grateful if someone helped me:
1) After I build and compile a project, do I still need all of those Android.mk files? Or can I just add .so libraries using System.loadlibrary("library")?
2) After I build my .so files, do I still need to declare all .a files as build-shared libraries and link them to the .so which needs them?
3) When I add native android support, do I need to create separate libsomename.so or choose the existing?
Maybe someone could also link me to the good article about building?
1)No, if its already compiled all you need is the so and loadLibrary call. However, if you want to update the library with your app its probably a good idea to keep them
2)No, once you have a .so file that's all you need
3)I'm not sure what you're asking there.