Distribute NDK Library with gnustl? - android

I am developing a library for Android which has both Java and Native components. The Native library depends on the GNU STL. I was linking against gnustl_static, but have ran into a few strange problems that were resolved by linking gnustl_shared. (for example the question Filesystem and locale). For the time being, I have settled on gnustl_shared.
Here's where the question is -- the consumer of my library is expected to link against the Native .so to add code of their own. I understand from reading that all the C-runtimes must match.
Demanding my clients use gnustl_shared seems like it might not work (as far as diplomacy and ease of integration goes)
If I build with gnustl_shared, the JAR file generated contains libgnustl_shared.so, which causes a problem building an APK which also relies on this library. -- I suppose I can just not include it in the JAR file?
If I build against gnustl_static do I avoid all these problems?
If my API contains refernces to STL objects (mostly std::string and std::vector), will I face problems if my clients do not use GNU STL?
thanks

Related

Bazel Android c++_shared/c++_static issues

We have a project that uses a library that is built on top of Google's Mediapipe, which is built using the Bazel build system.
The project itself is an Android Native Library, built using Gradle with CMake
externalNativeBuild {
cmake {
cppFlags "-std=c++17 -fopenmp -static-openmp -fexceptions -frtti -Wl,-s -Wno-unused-command-line-argument"
arguments "-DANDROID_STL=c++_shared", "-DOpenCV_DIR=${opencvDir}", "-DANDROID_ARM_NEON=TRUE"
}
}
So we end up with 2 (or more later, also dependent on OpenCV for example) shared object libraries - the actual SDK & the Mediapipe project.
We're seeing issues that are similar to this, which lead me to look into the runtime part of our project.
E/libc++abi: terminating with uncaught exception of type std::bad_cast: std::bad_cast
I saw this comment on that issue thread, and adding
System.loadLibrary("c++_shared");
Solved the crash.
However, this is not a practical solution as the project we're building would provide a native SDK in the form of multiple .so files and I wouldn't want to force our clients to have to explicitly load the shared runtime library before using our library.
The gradle library has "-DANDROID_STL=c++_shared" flag, so this is using the shared one, but I couldn't find any way to compile Mediapipe (with Bazel) using c++_shared. I couldn't find any reference to using shared runtime when compiling Bazel projects (except for this, which isn't exactly relevant and the solution didn't help me)
We might be able to work around this by setting -DANDROID_STL=c++_static, but this has other issues, mainly, it violates Android's guidelines for using multiple shared libraries, though it might be possible for for middleware vendors
So the question is,
Is it possible to build Mediapipe (or any other Bazel based) using c++_shared Android STL
If not, are there any other options to solve the runtime conflicts
Is it even a runtime conflict or something else?
I managed to get it working as suggested by using c++_static on all of our shared objects (SDK, Mediapipe, OpenCV and others)

Android NDK: Static or shared C++ runtime for 3rd-party Java library

I'm compiling a 3rd-party Java library for Android that uses JNI. I read the relevant pages on adding C++ support on developer.android but I'm still confused about a couple of issues regarding C++ STL runtime that I was hoping I could clear up here:
1- My library has no control over the app it will be embedded in, so I don't know if there will be other libraries that might use a static/shared STLs. If I use a static C++ runtime with ANDROID_STL=c++_static, is it safe, or should I have to worry about another library that could be using something like gnustl_static which might conflict with mine?
2- If I use a shared C++ runtime with ANDROID_STL=c++_shared, is it a guarantee that a specific element in the STL will use the libc++ runtime or could it be possible to use gnustl if it doesn't exist? For example, If I was using std::string with a shared c++ runtime (c++_shared) in an app that has another library of gnustl_static, will my std::string implementation be taken from libc++ or gnustl?
Ideally, I'd like to have a very stripped down version of a static c++ runtime with (c++_static) that only includes std::vector, std::string and std::map. I was actually planning to use something like -ffunction-sections as described here and #768.
Please advise and thank you.
Environment Details
Pkg.Desc = Android NDK
Pkg.Revision = r15c
Android Studio = 3.1.2
system: cmake Host OS: Arch Linux ($ uname -r % 4.18.5-arch1-1-ARCH)
Compiler: Clang++
STL: c++_static/c++_shared
Your concern is a very real one. But if handled properly, you can find a robust way out.
The warnings about using single C++ runtime across all libraries in the app (and the whole idea to define C++ support in NDK as APP_STL vs. most other flags such as LOCAL_CFLAGS or LOCAL_SHARED_LIBRARIES, are relevant for the native libraries that are connected. JNI libraries that never communicate directly (except through their corresponding Java layers) can use different C++ runtimes. Another point is that normal build will only package one C++ runtime shared lib into the APK. Note that versioning is also a potential hazard: if a developer who adds your library uses a different NDK release, there might be collisions or unexpected side effects when his version of STL runtime works with your code.
Therefore, to achieve maximum flexibility, your library should use a static C++ runtime. This may effect the size of the binary, but if, as you say, you use only a limited subset of STL, this extra will be rather small.
The bottom line, you will have minimum to worry about if build your shared library with libc++_static.

What is the behavior if an Android NDK application loads more than one shared C++ STL implementation?

We have an Android application with several native libraries. Currently, most of them use stlport_shared, but we are interested in upgrading to c++_shared (the LLVM libc++ runtime). If we were to upgrade some of them, but not others, we would have to load both stlport and llvm.
I imagined that loading two implementations of the STL might cause issues, but in practice the app seems to run correctly. Is this undefined behavior, or is it allowable to load more than one STL implementation?
I've read over https://developer.android.com/ndk/guides/cpp-support.html and some of the documentation supplied with the NDK, but can't seem to find a definitive answer. Thank you for your help!
It's best to avoid this if at all possible.
If an STL type (like a std::string, for example) is accessed by both an stlport_shared binary and a c++_static binary, as the two implementations of std::string are not compatible this will not work.
You also are vulnerable to the issues described by https://stackoverflow.com/a/25856207/632035

Using GNU STL and C++11 with V8 on Android NDK

I am building an application on Android using NDK and V8. I would prefer to use C++11 and GNU STL for features such as shared_ptr; however, the V8 build system seems to be hardcoded to use stlport_static as its STL variant.
Even when patching the build system to generate a fully self-contained library, V8's build does not statically link STLport into its .a files (which is to be expected), and thus I get hundreds of linker errors from unresolved symbols in libv8_base.a et al. I also do not see any way to indicate to ndk-build that it should link in stlport_static when I am using a different STL variant.
Is there a reasonable pattern to linking in stlport_static while using gnustl_static, or, better yet, is there a way of building Android V8 against gnustl_static instead?
You have a problem. Well, mixing different STLs in separate .so's is possible, with extra care; but using two STLs inside one .so is simply impossible.
You either need to implement your own shared_ptr (no big deal), but then you will face the same issue for every other feature that exists in gnustl and not in stlport.
Or you need to port V8 to gnustl (and I am afraid that the MIT license does not allow this).

Libraries available on Android NDK?

Are there any libraries available by default in the Android NDK? If not, can I use any c++/c library, as long as I include it myself?
In particular, I need compression, encryption, regex and sqlite. I was thinking of using zlib, crypto++, sqlite(duh!) and boost, but can use anything else if it is already available.
Also, if nothing is available built-in, would you recommend something other then my above choice (I prefer public-domain when possible, or BSD or similar)
SQLite is available on Android via abstracted database access in Java. The ‍.so itself is private. You can of course compile your own SQLite library if you require modifications, but in general you should access it from Java. I have done this for encryption purposes.
zlib is available to the NDK
crypto++ could of course be compiled via the NDK
Boost certainly works on Android/NDK (I'm currently using it heavily)
By default Android NDK delivers some libraries, so called 'stable native APIs' (http://developer.android.com/sdk/ndk/overview.html):
libc (C library) headers
libm (math library) headers
JNI interface headers
libz (Zlib compression) headers
liblog (Android logging) header
OpenGL ES 1.1 and OpenGL ES 2.0 (3D graphics libraries) headers
libjnigraphics (Pixel buffer access) header (for Android 2.2 and above).
A Minimal set of headers for C++ support
OpenSL ES native audio libraries
Android native application APIS
First, I think you need to download the NDK package from here: http://developer.android.com/sdk/ndk/index.html and investigate the documentation from the package. I'm sure you will get the answers to most of your questions.
And quick answer to your questions. Yes there are libraries by default in the Android NDK. For example standard C and C++ libraries.
This is only a partial answer with respect to Crypto++....
Crypto++ has a wiki page with build instructions from the command line at Android (Command Line). The process will create the various libraries and show you how to build cryptest.exe to verify the library on a device. But you will still need to create you wrapper project using Android's build system.
Piotr Morgwai Kotarbiński has a page on building the Crypto++ library with Android's modified build system. See Building Crypto++ with NDK toolchain. I don't believe Piotr's article verifies the library on a device.

Categories

Resources