Build native OpenCV 4 for use together with Qt 5.12 or later in Android environments

04/01/2021 - 11:15
Average: 5 (4 votes)

This tutorial explains how to build and setup OpenCV 4 for the Qt 5.12 C++ framework on Android. There are some quirks that needs to be dealt with so that the libraries are built correctly so that they work correctly with Qt 5.

The tutorial assumes that you have Qt 5 already installed with all required Android SDK & NDK dependencies working already. If not, install Android SDK & NDK and Qt first. This tutorial will not go into specific details on that.

Note: For Qt 5.12 you must use a current NDK with as Qt is built with clang.

Start by verifying that you can build and run a Qt application on your Android device. When that is working, continue with this tutorial.

Download and extract OpenCV sources

 tar xf 4.5.1.tar.gz

Prepare building OpenCV

Prepare a build directory for OpenCV by creating a directory  under the source tree, you can use pretty much any name you like, but a good one is build, or for example if you are building both 32-bit and 64-bit versions you might use build32 and build64.

 cd opencv-4.5.1
 mkdir build
 cd build

Configure the OpenCV build

How you configure OpenCV for building depends on the target Qt version, 5.12 or later and Android target, 32-bit or 64-bit. For Qt 5.12 and later you need to use NDK with clang. The following shows configuration examples for various combinations. Remember to adjust SDK paths to suit your installation of the Android SDK and NDK below.

Configure OpenCV build for 32-bit, Qt 5.12 or later

Configure the OpenCV build with the following parameters to cmake, adjust any SDK paths and versions to wherever you have the Android SDK installed on your system.

This will configure OpenCV build for building Android native shared libraries for the armv7 (32-bit) with NEON platform.

~/Android/Sdk/cmake/3.6.4111459/bin/cmake .. \ -DANDROID_ABI="armeabi-v7a with NEON" -DANDROID_NATIVE_API_LEVEL=23 \
-DCMAKE_TOOLCHAIN_FILE=/home/xxx/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake \
-DANDROID_NDK=/home/xxx/Android/Sdk/ndk-bundle -DANDROID_SDK=/home/xxx/Android/Sdk \

Configure OpenCV build for 64-bit, for Qt 5.12 and later

This will configure OpenCV build for building Android native shared libraries for the armv9 (64-bit) platform.

~/Android/Sdk/cmake/3.6.4111459/bin/cmake .. \
-DCMAKE_TOOLCHAIN_FILE=/home/xxx/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake \
-DANDROID_NDK=/home/xxx/Android/Sdk/ndk-bundle -DANDROID_SDK=/home/xxx/Android/Sdk \

Build and install OpenCV

As we are only interested in a native library we skip building of android projects and also enable shared libraries. If all goes well you should

now have the build configured and ready to go. Just run make and the OpenCV should build.

 make -j4 && make install/strip/fast

The Android install target will not install anything to your system, instead it will make directory structure with headers and libraries under a install/sdk directory. From here you can then copy the libraries and headers to a suitable location.

Note: If you need debugging information in the libraries then install using the install target instead.

We are interested in the libraries and they can now be found in the install/sdk/native/libs/armeabi-v7a/ (32-bit) or install/sdk/native/libs/arm64-v8a/ (64-bit) directory, you can copy all of them or just the ones you need, to your Qt project.

I tend to use a directory structure like "project-root/3rdparty/opencv-armv7/" for architecture specific 3rd-party binary libraries.

Reference the libraries & headers from your Qt project file

Next you need to reference the Android ARM libraries for linking and packaging from your project.

Add a section with the following contents, adjusted for your needs. In my application I only needed a couple of libraries but depending on what features of OpenCV you are using you need to add more of them.

You also need add the INCLUDEPATH setting that points to the OpenCV C++ header files. Adjust any example paths as needed for you installation.

32-bit Android (armeabi-v7a)

contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
        $$PWD/3rdparty/opencv-armv7/ \
        $$PWD/3rdparty/opencv-armv7/ \
        $$PWD/3rdparty/opencv-armv7/ \
    INCLUDEPATH = /opt/Android/opencv-4.2.0/build-armv7/install/sdk/native/jni/include
    # Order might be important it seems, linker in older droids (4.2) are dumb
    LIBS += -L$$PWD/3rdparty/opencv-armv7/ -ltbb -lopencv_core -lopencv_imgproc -lopencv_dnn

64-bit Android (arm64-v8a)

contains(ANDROID_TARGET_ARCH,arm64-v8a) {
        $$PWD/3rdparty/opencv-armv8/ \
        $$PWD/3rdparty/opencv-armv8/ \
        $$PWD/3rdparty/opencv-armv8/ \

    INCLUDEPATH = /opt/Android/opencv-4.2.0/build-armv8/install/sdk/native/jni/include
    LIBS += -L$$PWD/3rdparty/opencv-armv8/ -ltbb -lopencv_core -lopencv_imgproc -lopencv_dnn

Thats it

That should be it. You should be able to use OpenCV for your Qt application Android builds now.

Next Tutorial will show how to use OpenCV with Qt. Stay tuned.

22/11/2018 - 17:35
Qt, Android, OpenCV, C++, Android NDK