Find the NDK version used for building OpenCV Android native libraries - android

Suppose I have prebuilt OpenCV Android native libraries, such as this. How to find which version of NDK is used for building this?
I am curios about this because OpenCV 3.4.3 prebuilt Android libraries seems to incompatible (?) with NDK r18.

OpenCV library contained this information. Open the prebuilt library in Notepad++ and search for "General configuration for OpenCV".

Assuming it was built with a new enough version of the NDK (I don't remember exactly when we added this, but it's been a year or two), there's an ELF note baked into the binaries. Can use https://android.googlesource.com/platform/ndk/+/master/parse_elfnote.py to parse it. It'll show you something like the following:
$ python parse_elfnote.py $NDK/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so
----------ABI INFO----------
ABI_ANDROID_API: 21
ABI_NDK_VERSION: r18
ABI_NDK_BUILD_NUMBER: 5002713

Related

How can I integrate OpenCV 4.0 into a pure C++ Android NDK project?

What are the steps necessary for me to integrate,
the latest version of OpenCV
into a pure C++(No Java Code) Android NDK project, such as Android NDK Google tutorial:
Endless-Tunnel
?
official documentation for android integration refers to much older version :OpenCV-2.4 and folder structures are no longer the same.
I am using Android Studio on Linux.
All help is highly Appreciated.
Download opencv Android package (e.g. opencv-4.0.1-android-sdk) and unpack to, say, ~/android.
To the bottom of CMakeLists.txt, add
set( OpenCV_DIR "~/android/OpenCV-android-sdk/sdk/native/jni" )
find_package( OpenCV REQUIRED )
target_link_libraries(game opencv_java)
The package will define the following variables:
OpenCV_LIBS : The list of all imported targets for OpenCV modules.
OpenCV_INCLUDE_DIRS : The list of OpenCV include directories. With CMake >= 2.8.11 you don't even need to write
include_directories(${OpenCV_INCLUDE_DIRS})
This version of prebuilt OpenCV SDK defines also
OpenCV_VERSION : The version of this OpenCV build: "4.0.1"
OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API: "16".
This means that your app manifest needs minSdkVersion 16 or higher (the original sample needs a fix here).
Instead of the shared library that contains all OpenCV functionality, you can use static libraries (opencv_imgcodecs, opencv_stitching, et al). These static libraries assume the default ANDROID_STL=c++_static.
For best results, use NDK r.18 or r.19.
UPDATE: NDK r.21 works well for opencv 4.3.0.
If you would like to create shared objects(.so) and compile without using Android studio, here is my blog. This is often useful when you would like to create a native third-party library.

Include GStreamer in android ndk project that uses CMake (CMakeLists.txt) and not ndk-build (Android.mk)

I have an android project that currently uses Cmake for including all .cpp /.c code. Now I want to add the GStreamer native libraries such that I can use them in my native code.
But the gstreamer docs https://gstreamer.freedesktop.org/documentation/installing/for-android-development.html
only document using ndk-build to use gstreamer on android.
Now I don't want to totally refactor my project to use ndk-build and Android.mk because
CMake does its job and i never had problems with it
as stated here cmake is the default for android ndk https://developer.android.com/studio/projects/add-native-code
I also need to include the googlevr ndk library that uses cmake.
So I need to figure out a workaround and therefore need your help. Here are some ideas I came up with
Each module can have ether cmake or ndk-build support. Therefore,
I probably could add a new module using ndk-build and include gstreamer there (but then gstreamer is only available in this module)
Compile gstreamer for android using cmake inside android studio - but I don't see evidence that has been done before or is possible for someone without strong cmake knowledge.
Any other ideas/ improvements ? thanks
Even though you have already moved away from GStreamer but in case someone else is facing this issue please have a look at following github repository
https://github.com/henkeldi/gstreamer-android
For myself, I had to make two modifications
I modified the definition of GSTREAMER_ROOT to include ANDROID_ABI so it looks like following
#GStreamer
set(GSTREAMER_ROOT $ENV{GSTREAMER_ROOT_ANDROID}/${ANDROID_ABI})
I had to rename the folders in GStreamer pre-built binaries to be according to ANDROID_ABI. So, my Gstreamer pre-built binaries folder look like following
After the above changes, I am able to successfully build with APK with GStreamer using CMake
At some point I gave up on gstreamer and am now using ffmpeg instead.
There is a nice tutorial for compiling ffmpeg that makes including native ffmpeg inside an android project really easy:
Tutorial on Medium
Github project

Set ANDROID_STL in CMakeLists

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.

Determine what version of NDK was used to build library?

Is there any way to determine what version of the NDK was used to compile an "aar" library? Either decompiling, or via code would be fine, just need to know.
I am trying to determine what version of the ndk one of my third party libraries was built using.
An "aar" file is not compiled with NDK, you probably mean one of the c++ libraries inside that file. There is no simple way, but you can rule out new ndk versions according to the library date.
Also, you might be able to find out by getting the compiler version - see if something like this can help: How to retrieve the GCC version used to compile a given ELF executable

Is there a way one can find out which ndk version was used to compile android .so share library?

Is there a way one can find out which ndk version was used to compile android .so share library ?
There are some hints, like if the file was compiled in 2010, it could not be NDK r10. Or if it was compiled with gcc 4.9, it must have been above r8 (IIRC, I did not check). You can set the lower limit also if it is 64bit. Or if it is for platform 21.
You are in a much better shape if there is a prebuilt STL library next to your .so. This file can be binary compared to the versions packed in each NDK release.

Categories

Resources