Build native OpenCV 4 for use with Qt 5 on Android

04/02/2020 - 12:56
Average: 5 (4 votes)

This tutorial explains how to build and setup OpenCV 4 for the Qt 5 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: You will need an older version, r17c, of the NDK that still supports gcc and gnustl as that is what Qt 5.11 is built with at this time. If you are using 5.12 or later you must use a later version of the NDK as Qt 5.12 or later 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.2.0.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.2.0
 mkdir build
 cd build

Configure the OpenCV build

How you configure OpenCV for building depends on the target Qt version (5.11 or older, 5.12 or later) and Android target, 32-bit or 64-bit. For older Qt versions (5.11 or older) you need to use a Android NDK that still supports gcc, for 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.11 or older

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.

~/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 \

This will configure the OpenCV build for building Android native shared libraries for the armv7 with NEON platform, using gcc and gnustl.

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

If you are using Qt 5.12 or later version, you can now build 64-bit Android apps. In this case you need to build 64-bit native libraries of OpenCV.

~/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.1.1/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.1.1/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