How to Build OpenCV 3.3.X for Android

In this post I’m sharing the required steps for building OpenCV 3.3.X (to be precise, OpenCV 3.3.0 which was released about a week ago) for Android. You can also download the pre-built libraries from here but it’s always a good idea to build the libraries yourself in case you want any special configurations such as Nvidia Carotene, OpenCV World and so on. So here it is:

First let’s go over the prerequisites:

  • OpenCV Source Files (You can also clone the latest source codes from OpenCV Git Repository but the source codes in release are usually “Stable”er)
  • CMake (just download and use the latest version and be done with it, but just in case I’m using 3.8.1)
  • MinGW (this is what I use as the Make system, but you might be able to achieve the same with other Make systems too. You don’t need the MinGW compiler but the installation usually includes the whole kit. I use MinGW 530 included in the Qt 5.9.1 Installer for Windows)
  • Android NDK (I used the latest version which is r15c but I assume some of the earlier versions will also work, you need to try this for yourself if you’re curious)
  • JDK (Install the latest, and it’ll keep itself updated regularly)

After getting all of the requirements we need to make sure all environment values are set. Make sure the following environment values are set:

  • Add MinGW installation folder to PATH
  • Create an environment variable called ANDROID_NDK and set it to Android NDK folder (this is where you extracted Android NDK)

Now start by extracting OpenCV source codes somewhere (in my case it’s “d:\cv\opencv_3.3.0\sources” but you can use whatever you like)

Run CMake, set the source codes folder (1), a binary folder of your choice (2), hit Configure button (3), select MinGW Makefiles as the generator (4), select “Specify toolchain …” option (5) and hit “Next” button (6), as seen in the image below:

In the dialog that appears select “android.toolchain.cmake” file which is inside the folder where you extracted OpenCV source codes, under /platforms/android (similar to what is seen below):

After some moments (or minutes depending on what your idea of a PC is) you will see something similar to the following:

Make sure you see the “Configuring Done”, otherwise you might need to start again with this guide or post a question in the comments section below.

Select any options you like and press Generate button. I usually build for armeabi-v7a, with BUILD_opencv_world checked and WITH_CAROTENE unchecked but you can try different configurations for yourself.

After a few moments OpenCV is ready to be built. Just make sure you see “Configuring done” and “Generating done” messages at the end of CMake log.

Now run a Command Prompt and switch to the binaries folder you provided to CMake. Just to compare with mine, it’s “D:/cv/opencv_3.3.0/build_android”

Run “mingw32-make” command.

OpenCV build process will start, and you’ll see some colorful stuff for quite a while:

Wait until you see 100% built. When building is finished you need to execute another command. “mingw32-make install”.

Wait until it finished and you’ll have all your beloved libraries inside binaries folder you chose on CMake. Again, just to compare with mine, here’s where I got all produced files after building with the above instructions: D:/cv/opencv_3.3.0/sources/build_android/install

6 Replies to “How to Build OpenCV 3.3.X for Android”

  1. I get this output:
    Check for working CXX compiler: D:/Desenvolvimento/Programas/NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++.exe
    CMake Error: Generator: execution of make failed. Make command was: “D:\Desenvolvimento\Programas\CMake\bin” “cmTC_f1189/fast”
    Check for working CXX compiler: D:/Desenvolvimento/Programas/NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++.exe — broken
    CMake Error at D:/Desenvolvimento/Programas/CMake/share/cmake-3.11/Modules/CMakeTestCXXCompiler.cmake:45 (message):
    The C++ compiler


    is not able to compile a simple test program.

    It fails with the following output:

    Change Dir: D:/Desenvolvimento/Programas/OpenCV/Source/opencv-master/platforms/android-arm/CMakeFiles/CMakeTmp

    Run Build Command:”D:\Desenvolvimento\Programas\CMake\bin” “cmTC_f1189/fast”
    Acess Denied
    Generator: execution of make failed. Make command was: “D:\Desenvolvimento\Programas\CMake\bin” “cmTC_f1189/fast”

    CMake will not be able to correctly generate this project.
    Call Stack (most recent call first):
    CMakeLists.txt:127 (project)

    1. You seem to be having some sort of access issue, if you check the output carefully.
      Make sure the folders used are all accessible, or run the build commands as administrator.

      Let me know if this helps.

  2. I was able to build successfully by following the above mentioned steps
    But the when i checked the libs folder it doesnt contain the . I need this file in order to execute the opencv on arm device. Can u help me with this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.