Basically, I want to know how to use a c++ shared library in Android Studio in NDK code (inside jni part). There is quite a lot of questions about that but they all are based on changing Android.mk which is not a way to go because in AS it's generated automatically.
To use pre built librairies with Android Studio, you have to follow these steps:
Compile your C++ librarie with ndk-build (here you build librairies for Android architecture such like arm, x86, etc.).
Create a Java class and JNI wrapper for your C++ methods.
You have then to disable gradle for ndk-build and create your own Android.mk and Application.mk in the jni folder.
Import the .so library and pre build it in the Android.mk.
Invoke ndk-build manually, from the Android Studio console.
Then, include the header of your librarie and call its functions in your JNI part.
For any complement of information, I advise you the intel video that you can find here.
Related
I am working on an Android application with NDK. My c++ code has some dependencies from c++ open source libraries, so I can download the source code of the dependencies and package them in my app. I have created my CMakeList files, but I don't know how to include the external library.
I would like to build the dependency to be able to use it in my own project, so the flow would be as follows:
cd external/library/path
./configure
make
build my app and link the built dependency
Is this possible? If so, how can this be done? Sorry if this is a dumb question, I'm new on C++ and Cmake.
The way I solved this was by cross compiling the libraries and adding them as static libraries. You can cross compile using the clang binaries included in the ndk directory, using the one that targets your app min sdk.
I would like to download some working example of Android Studio NDK project with C or C++ which will be compiled to .so library (or APK from which I can extract .so).
I have tried ndkbuild with Android.mk build and also CMake with CMakeLists.txt, official and unofficial tutorials on Windows...
If I try them on android from same app (java), it is working but I want to use NDK in Unity3D on android and I keep getting DllNotFoundException.
I uploaded my Android and Unity projects to github.
First of all, here is a sample Android Project plugin you are looking for.
It's very important to know how to do this yourself.
This used to be hard to do before but the latest Android Studio made it easier to now use C/C++ or generate .so library easily. Update Android Studio to the latest version then follow steps below to create a C++ plugin.
1.Create a new Project in Android Studio
2.Check the Include C++ suport to enable C++.
3.On the Dropdown Menu, check C++ 11 on the C++ Standard drop-down menu. You will need C++ 11 to actually use most useful C++ features. Also, enable exception or frtti if you need them.
That's really it.
When you build the plugin:
The debug library is should be at:
\app\build\intermediates\cmake\debug\obj\
The release library is should be at:
\app\build\intermediates\cmake\debug\obj\
If you only see the debug but not the release version of the plugin, check here for how to make it appear.
Possible reasons you are getting DllNotFoundException on Android:
1.You did not wrap the C/C++ function around extern "C". You must do this for each function in the .cpp file or you do it in the function in the .h file.
2.You put the plugin in the wrong Unity folder.
The armeabi-v7a and x86 plugins generated at <ProjectDirectory>\app\build\intermediates\cmake\release\obj should be placed at Assets\Plugins\Android\libs\armeabi-v7a\ and Assets\Plugins\Android\libs\x86\ in the Unity project.
Make sure to spell these correctly. See this for more information about this.
3.You are loading it incorrectly from the C# side.
Let's say that the name of the plugin is libScreenshot.a, do not include the lib prefix, also do not add the .a when loading it.
[DllImport("Screenshot", CallingConvention = CallingConvention.Cdecl)]
public static extern void takeScreenshot();
I am compiling C++ library code in Android Studio 2.2. I follow the new guide, where I add all the files to my project and compile it using CMake (and CMakeLists.txt) like this. I want to use C++14 features, and stuff like atomic, stoi etc. but the building is failing with errors.
error: no type named 'condition_variable' in namespace 'std'
error: no member named 'stoi' in namespace 'std'
This is what my CMakeLists looks like (other lines set source files and other stuff):
find_library(GLES GLESv2)
include_directories(${COMMON_PATH} /usr/local/include)
set(ANDROID_STL "c++_shared")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -latomic")
add_library(native-lib SHARED ${COMMON_SRC})
target_link_libraries(native-lib ${GLES})
I found this article on the android page (here), but I don't know how and if I can do this when using CMakeLists and not ndk-build. I see other question that solve it using the c++_static runtime but only with ndk-build.
This documentation would be more useful for cmake case:
arguments "-DANDROID_STL=c++_shared
Gradle also packs libc++_shared.so into APK when using c++_shared; bypassing Gradle might cause trouble for your app on early version of Android OS.
at the time of this question, Android Studio might have trouble; at the time now Android Studio 3.1.3, it should be ok
The cross-compilation process used to generate the native libraries for Android uses the c++ dependencies from the NDK libraries. The NDK provided by Google is good and it has lots of things, but the C++11 and C++14 support is not complete.
If you want to use C++14 features, you can use other NDK like CrystaX NDK for example. With CrystaX you have also C++17 support.
I am trying to learn NDK, and I'd like to use external library (libopus). After reading some articles, I got these steps:
git clone https://android.googlesource.com/platform/external/libopus
mv libopus jni
NDK_PROJECT_PATH=. ndk-build
It crated libs/armeabi/libopus.so file. Yay, awesome! ... And now what? How can I use this library? How can I call its functions and methods?
Also, will my app run on non-ARM architectures (x86, MIPS), because armeabi suggests it'll be ARM only.
You can not just use standard Linux libraries. Java/Android uses the Java Native Interface (JNI) which is special C code that builds the bridge between Java part and native part.
It looks like you already have NDK installed. Look into the project samples, e.g. the "hello-jni" project. In this example you can see what JNI C code you have to write and how to access the self written functions from within your Java code.
Regarding the architectures: Yes, an ARM library is for the ARM platform only. If you want to create a cross-platform App you have to compile all native libraries for each supported platform (usually ARM, ARMv7, x86 and MIPS).
I have some C++ code (interacts with micro controllers) written already by someone else. I learnt android & NDK and comfortable writing small sample programs. Now I need to integrate both.
So, How should I start proceeding on the integration part? How does the NDK actually works? Assuming I have 3 parts now A - C++ code, B - NDK native interface code, C - Android Activity/Class .
1) Should I compile A (g++ linaro) and then place the object file in Android project to be called by C through B?
(or)
2) Should I compile the A & B together using g++ (linaro) and then copy the .so file into the Android Eclipse project? (Not sure how complex it will be to mimic NDK-build command in normal eclipse).
(or)
3) Copy A into Android Eclipse project and generate java.h file, then generate .so file using the both A & B. (In this method I need to find the right place to put the whole CPP project files in the Android/NDK eclipse project).
PS: I tried to find examples that does this, but only seem to find the simple basic examples, which I am pretty comfortable creating already. I need help in the integration part, please post me tutorial if you know (Android/NDK/Eclipse/already_existing_C++_code).
You should compile A using the Android toolchain. Note that Android supports not only ARM (a.k.a. armeabi) but also armv7a, x86, mips, and recently - armeabi-v7a-hard. Soon, x86-64 will be released.
You can compile A with Android standalone toolchain, no need to adopt the NDK build system.
You can compile B as part of A, or separately. In the latter case, simply load A before B in your Java static constructor:
{
loadLibrary("A");
loadLibrary("B");
}
because libB.so will have dependencies on libA.so.
You can pack both libA.so and libB.so in the APK (in folders libs/armeabi, libs/x86, etc.)
First of all, I recommend you to read Android NDK documents. Android.mk is not hard to write in order to compile C++ code into shared library for JNI using NDK toolchain. The most difficult part might be that Android libc (bionic) is not the same as ordinary Linux libc.
So, try to compile A - C++ code using NDK toolchain first. If you failed it, you should port it to Android libc, or you should compile it and link statically it using linaro toolchain. Take a look at the documents to link static elf library using NDK toolchain. But the binary wouldn't work on Linux because Android Linux kernel is not the same as linaro.
Anyway if you got to compile a shared library, easy to integrate it to Android project. just put the shared library to libs/[arch], like libs/armeabi-v7a/libfoo.so.