OpenCv on Android

OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision, written in C++.

OpenCV for Android SDK comes with handy Android (java) wrappers so you can concentrate on writing java and let the wrappers take care of communicating with the underlying native (C++) OpenCV libraries. That’s great if you want to keep the C++ abstracted as far away as possible and happy with the version of OpenCV libraries shipped with the sdk. But if you want to customize OpenCV in any way or build a different version from source code, then it seems you have to look further than the sdk which doesn’t seem to come with the necessary build files.

Building C++ projects for android requires installing not just Android Studio, but also the Android NDK (don’t forget LLDB for debugging and CMake which is essentially a makefile generator).

Unfortunately, Android libc (Bionic) has very limited functionality (which differs from one Android version to another), so developers have been forced to take into account all its peculiarities, detecting in run-time which specific Android version the application is running on and applying specific workarounds. A better alternative is to use CrystaX NDK, a drop in replacement for the Android NDK. With Crystax developers can forget about such problems, since CrystaX NDK provides complementary libcrystax, which is used to hide the differences between Android versions or even re-implement many of libc functions. Most importantly, with the help of libcrystax, the application behaves the same on all Android devices regardless of Android version. So downloading CrystaX and configuring Android Studio to use this instead (Module Settings -> Project Structure -> SDK Location) is recommended. As a bonus you also get pre-built boost libraries out of the box.

Build OpenCV from source for Android:

The detailed instructions here are apply to a development machine running Linux, but the procedure should be very similar on Windows/MacOS(X)

  • Clone the main OpenCV repository from GitHub:
    git clone {path_to_local_clone_of_opencv_repo}
  • Change to the opencv platforms dir
    cd {path_to_local_clone_of_opencv_repo}/platforms
  • Add android’s cmake to your PATH environment variable:
    export PATH={path_to_local_android_installation}/android/sdk/cmake/3.6.3155560/bin/:$PATH
    Note: You may need to modify the cmake version number depending on your android installation
  • Point ANDROID_NDK to the install location:
    export ANDROID_NDK={path_to_local_android_installation}/android/sdk/ndk-bundle/
    export ANDROID_NDK={path_to_local_crystax_installation}/crystax/crystax-ndk-10.3.2
  • Configure the build
    sh ./scripts/ .This creates a build directory and runs a CMake command to populate the directory with a particular configuration of OpenCV.
    You can edit this script (or pass it parameters that override any defined in the script itself) if you want a different configuration. For example:”-DBUILD_SHARED_LIBS=ON” –  if you want shared libs, instead of the default dynamic libs.
    “-DANDROID_STL=gnustl_shared” – which c++ standard library to use.
    “-DANDROID_ABI=armeabi-v7a” – which abi to build.
    “-DANDROID_TOOLCHAIN_VERSION=gcc-4.9” – which compiler and version to use.
    “-DANDROID_NATIVE_API_LEVEL=9” – use the lowest value that works (forward compatible). See {ndk-dir}/platforms .
    “-DCMAKE_INSTALL_PREFIX=${path_where_youd_like_header_and_libs_copied}” –  see “make install” below.“-DCMAKE_TOOLCHAIN_FILE={path_to_crystax_ndk}/cmake/toolchain.cmake” –  only necessary when using crystax.

    eg. sh ./scripts/ -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=./my_opencv_install_dir

    For a list of variables that can be passed to the script see
    as well as the toolchain.cmake file

    Check out the other scripts too for the defaults used for different platforms eg

  • Start the build
    cd build_android_arm
    make -j8

    Note: The –j8 flag specifies that the make command will use 8 threads. You could specify a different number if you have reason for doing so.

You should now have the freshly built OpenCV libraries in

If you set “DCMAKE_INSTALL_PREFIX” as above then

make install will copy the headers and libs to the directory you specified via -DCMAKE_INSTALL_PREFIX above, or the default if you didn’t.

Note: After the first build cmake caches options in the following location:
You can peruse and edit these (and then repeat the previous 2 steps), if you want to perform any customizations of the build.

Note:  If you want to build again, its safer to delete the whole build dir (eg “build_android_arm”) first.