Build native OpenCV 4.0 for Android for use with Qt 5

10/12/2018 - 16:02
No votes yet

This tutorial will explan how to use OpenCV on Android but with Qt 5. There are some quirks that needs to be dealt with so that the libraries are built so they work correctly with Qt 5.

The tutorial assumes that you have Qt with all required Android SDK & NDK dependencies installed and working already. If not, install Qt and Android SDK+NDK 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 is built with at this time.
Note: AFAIK Qt 5.12 will be built with clang and should be able to use later NDKs, but 5.12 is not yet released and not tested at this time.

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.0.0.tar.gz

Prepare building OpenCV

 cd opencv-4.0.0
 mkdir build
 cd build

Configure OpenCV build for 32-bit

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 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

Install will copy all the needed file into a directory under the build tree, not into your system itself.

Note: If you don't care about debugging, then you can install using the install/strip target

We are interested in the libraries and they can now be found in the install/sdk/native/libs/armeabi-v7a/ 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 any 3rd-party 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 path as needed.


contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    # QMAKE_CXXFLAGS += -mfpu=neon

        $$PWD/3rdparty/opencv-armv7/ \
        $$PWD/3rdparty/opencv-armv7/ \
        $$PWD/3rdparty/opencv-armv7/ \

    INCLUDEPATH = /home/xxx/src/opencv-4.0.0/build/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

That should be it.

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

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