Android OpenCV Compilation


When developing Computer Vision applications using OpenCV for the Android platform, developers often face difficulties in getting a OpenCV build for their project. The official OpenCV Android builds often fall short as they do not ship with the OpenCV contrib modules. So most developers end up compiling OpenCV to suit their needs in the product development journey. This is when the options available on the compilation process create confusion and result in much of the compilation woes. The article aims to answer some of the compilation choices and provides best practice suggestions for OpenCV compilation for Android. If you are interested only in using the prebuilt 64bit OpenCV binaries for Android, you can skip the contents below and download the binaries from this Dropbox location.

Which CMake to use ?

Always use the CMake supplied from the Android NDK for building C / C++ programs for Android. The regular CMake build, outside of the NDK, also has support for Android NDK, but this is often broken when using this CMake to build libraries like OpenCV, OpenSSL etc for Android. Android strongly discourages the use of CMake that is not supplied as a part of its NDK. There is an active open issue on android NDK Github repository on this topic.

Which compiler to use ?

The OpenCV project is quite mature and supports a wide range of compilers including GCC and Clang. Traditionally the Android NDK had supported both the GCC and Clang compilers. During the release of the v17 version of the NDK, the changelog of the release mentioned that the GCC will no longer be supported and will be removed from the subsequent NDK releases. The NDK version r18b has removed the support for GCC compiler completely and Clang is the only version that is supported from r18 and above. Please refer to the Announcements section on corresponding NDK releases for further clarifications.

Which ARM target to use ?

The Android NDK supports the ABI versions arm64-v8a, armeabi-v7a, x86 and x86_64. There has been a greater push to support both 32 bit and 64 bit versions of the apps that are published in the Play Store. Beginning from August 2019, all applications will require support for 64 bit version of their application. When using OpenCV NDK code, it is essential that OpenCV is compiled with support for arm64-v8a. The following build configuration just works out of the box for building OpenCV and has been tested by us. The pre-build binaries for the below configuration is available from this Dropbox location.

CMake version
NDK version r20
Android SDK tools version 25.2.5
OpenCV version 3.4.6
Targets supported arm64-v8a and armeabi-v7a
Compiler clang

The build command used to build the above configuration is as below

/home/cm/Android/Sdk/cmake/ -DANDROID_ABI=arm64-v8a -DANDROID_SDK=/home/cm/Android/Sdk -DANDROID_NDK=/home/cm/Android/Sdk/android-ndk-r20 -DCMAKE_TOOLCHAIN_FILE=/home/cm/Android/Sdk/android-ndk-r20/build/cmake/android.toolchaincmake -DANDROID_NATIVE_API_LEVEL=23 -DANDROID_TOOLCHAIN=clang -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../dist_arm64-v8a -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DOPENCV_EXTRA_MODULES_PATH=/home/cm/opencv/opencv_contrib-3.4.6/modules -DBUILD_EXAMPLES=OFF -DBUILD_DOCS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DANDROID_ARM_NEON=ON -DBUILD_ANDROID_EXAMPLES=OFF ../opencv-3.4.6

Related Articles

Get a free quote for your next AI project

We can help you build cognitive products faster. Our expert Data Scientists can help you in driving business value with exceptional quality.

Contact us