Latest Android NDK To Build OpenCV
New Android NDK toolchain to build any arbitrary project.
In my previous post, i talked about the building arch
specific toolchain. But Google has obsolete standalone toolchain and hence it won’t be possible to build the native toolchain. Its quite inevitable that you can’t stick to r14b (or earlier version before r19c), incase you need newer NDK features (example camera-2 APIs : camera ZOOM ratio, etc). For that you have to start using latest NDK versions i.e. at least version r21e and above.
I have tested following combination but not all NDKs are compatible with OpenCV:
- NDK r19c with OpenCV 4.0.1
- NDK r19c with OpenCV 4.3.0
- NDK r21e with OpenCV 3.4.3, 4.0.1, 4.3.0.
My builds were for arm64
but it should work for other Android supported ABI types.
I have create a Google-Colab script that can generated stripped shared object for configured modules, the same code snippet is pasted below:
Steps:
Download Android SDK
$ sudo apt update && sudo apt install android-sdk
$ sudo apt install cmake
Download NDK r21e
$ wget https://dl.google.com/android/repository/android-ndk-r21e-linux-x86_64.zip
$ unzip android-ndk-r21e-linux-x86_64.zip
$ rm android-ndk-r21e-linux-x86_64.zip
Download OpenCV Source:
Tested on following releases: 3.4.3, 4.0.1, 4.3.0
$ git clone https://github.com/opencv/opencv.git
$ cd /content/opencv/
$ git checkout 3.4.3
Compiling OpenCV using NDK toolchain
$ export ANDROID_NDK=/content/android-ndk-r21e
$ export ANDROID_SDK_ROOT=/usr/lib/android-sdk
$ export ANDROID_SDK_ROOT=/usr/lib/android-sdk
$ export ANDROID_NATIVE_API_LEVEL=24
$ export STRIP=/content/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-strip
create build directory.
$ mkdir -p /content/opencv/build
$ cd /content/opencv/build/
Configure the build as per your requirements:
$ cmake -DCMAKE_TOOLCHAIN_FILE=../android-ndk-r21e/build/cmake/android.toolchain.cmake -DANDROID_TOOLCHAIN=clang++ -DANDROID_ABI=arm64-v8a -D CMAKE_BUILD_TYPE=Release -D ANDROID_NATIVE_API_LEVEL=24 -D WITH_CUDA=OFF -D WITH_MATLAB=OFF -D BUILD_ANDROID_EXAMPLES=OFF -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -D ANDROID_STL=c++_shared -D BUILD_SHARED_LIBS=ON -D BUILD_opencv_objdetect=OFF -D BUILD_opencv_video=OFF -D BUILD_opencv_videoio=OFF -D BUILD_opencv_features2d=OFF -D BUILD_opencv_flann=OFF -D BUILD_opencv_highgui=ON -D BUILD_opencv_ml=ON -D BUILD_opencv_photo=OFF -D BUILD_opencv_python=OFF -D BUILD_opencv_shape=OFF -D BUILD_opencv_stitching=OFF -D BUILD_opencv_superres=OFF -D BUILD_opencv_ts=OFF -D BUILD_opencv_videostab=OFF -DBUILD_ANDROID_PROJECTS=OFF ..
compile the build
$ make -j nproc
Time to verify the build for desired output:
$ ls -l ./lib/arm64-v8a/
$ du -h ./lib/arm64-v8a/libopencv_core.so
The build have debug information, if you are going for production you may strip it out using following command:
$ /content/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip — strip-unneeded ./lib/arm64-v8a/libopencv_core.so ./lib/arm64-v8a/libopencv_dnn.so ./lib/arm64-v8a/libopencv_highgui.so ./lib/arm64-v8a/libopencv_imgcodecs.so ./lib/arm64-v8a/libopencv_imgproc.so ./lib/arm64-v8a/libopencv_ml.so
check the size, if MUST have reduced substantially:
$ du -h ./lib/arm64-v8a/libopencv*
$ sudo apt install file
Make sure the arch
type is as required arm64
:
$ file ./lib/arm64-v8a/libopencv_core.so
This process should work quite same for other builds too, make sure you export correct flags and paths.
Happy compiling !!!