Alexander Alekhin edited this page Jun 16, 2016 · 3 revisions
Clone this wiki locally

Building OpenCV4Android from trunk

The new android build is based on android-cmake project originally developed by Ethan Rublee.
You can find the latest version of his project at


  • git client
  • cmake tool
  • Android NDK (revision !r5 or newer)
  • OpenCV master: You can clone repo with the following command:
    git clone

Additional requirements for Java wrapper and Android samples

  • JDK 6
  • Android SDK
    • “Android SDK Tools, revision 14” or newer is recommended
    • SDK platform Android 2.2, API 8” or newer package installed (for the Java API and tests)
    • SDK platform Android 3.0, API 11” or newer for the samples (they can run on Android 2.2 but require API 11 at compile time)
  • Apache Ant 1.8.0 or newer
  • Python 2.6 or newer (but not 3.x)

Windows additional prerequisites

At the moment OpenCV cross compilation under cygwin is not supported.

However you can cross-compile OpenCV on Windows using ’’’native port of make’’’.
Android NDK !r7 and newer already includes the make executable.
If you need to build OpenCV with older NDK, then you can get it from NDK or use one of alternative downloads. These two are tested to work for OpenCV:

  • (see “make-3.7z” under the Downloads section)

OpenCV build

By default OpenCV and the most of Android samples are configured for modern ARM-v7a architecture.
If you want to compile OpenCV for emulator or older device, or want to build with NEON support please read the Android_targets section for instructions.

Linux (Ubuntu) and Mac OS (10.6.7)

Export full path to your copy of NDK

export ANDROID_NDK=~/android-ndk-r8e

Alternatively to build with standalone toolchain extracted from NDK you can export toolchain location

export ANDROID_STANDALONE_TOOLCHAIN=~/android-toolchain

If you have not declared any of these variables then build scripts will try to find NDK under the /opt folder
(so if you have placed your NDK to /opt/android-ndk-r8e/ then it will be detected automatically).

There is a scripts folder in the android folder for running cmake with the proper cache
variables set. It is recommended that you use this to setup a cmake build directory.

cd opencv/platforms
sh ./scripts/

You should now see a build directory, that is ready to be made

cd build_android_arm
make -j8

That will build most of the OpenCV modules, except for those that don’t make sense on android – gpu, ocl, python wrapper etc..

Windows (native)

Define the following environment variables:

  • ANDROID_NDK – full path to the Android NDK. This path should not have tailing slash;
  • Add path to cmake and make tools to PATH environment variable.

Instead of modifying your environment you can create file opencv\android\scripts\wincfg.cmd and put all settings into this file. OpenCV provides a template for this file named opencv\android\scripts\wincmd.cfg.tmpl. Please note that settings from wincfg.cmd takes precedence over global environment variables.

Then open Windows console (cmd.exe) and go to opencv\platforms folder. There is no ready script for Windows platform, you need to build OpenCV step by step:

cd opencv\platfroms\
mkdir build_android_arm
cd build_android_arm
cmake -G “Unix Makefiles” -DCMAKE_TOOLCHAIN_FILE=..\android\android.toolchain.cmake ..\..

When script finishes you will have OpenCV compiled.


First, make sure that paths to OpenCV sources, Android NDK and make.exe do not contain spaces and other suspicious symbols.

If make fails with message similar to

  • target pattern contains no `%’. Stop.

    Then you are probably using cygwin tools instead of their native ports. Remove cygwin from your PATH, delete build directory and rebuild.

Or if make fails with Java related messages, then make sure your version of python is a Windows app instead of the cygwin one, such as ActiveState Python v2.7.

Building console “Hello World” with OpenCV

This application is not usual Android application. This is a Unix console which may be used as a template for development. Console application is much more convenient for development, debugging and performance optimization stages. Then, if you are finished with your CV algorithm, you can switch to normal Android development with Java + NDK.


First of all you need an Android device with root privileges. The goal is to have ability to run console applications on Android device using adb shell. Even chmod is not available by default (for example on Nexus One). You have the following options:

  • Use emulator
  • Enable root access to your device. Use ’’Google’’: to find instructions for your device.
  • Use developer device, for example NVidia Tegra devkits have root access by default.

If the device allows you to do chmod and run executables, then follow the instructions below.

Second, you need OpenCV compiled for your device. This sample is configured for emulator. It is different from default settings for OpenCV. Read Android_ABIs to know how to build OpenCV for emulator.
Also you need some free space on sdcard to run the HelloAndroid application – it will write small image to /mnt/sdcard. Please note, that even with emulator you need a sdcard. You can create it with AVD Manager from Android SDK.

Linux (Ubuntu) and Mac OS (10.6.7)

Export full path to your copy of NDK

export ANDROID_NDK=~/android-ndk-r8b

or path to standalone toolchain

export ANDROID_STANDALONE_TOOLCHAIN=~/android-toolchain

Run the following commands to build project

cd opencv/samples/android/hello-android
sh ./
cd ./build_armeabi
cd ..

Connect to your device with adb tool from Android SDK and run the application

sh ./

If you can see HelloAndroid.png in your current directory and Hello Android! message in the console – you’ve arrived.

If you want to build this sample for ARM-v7a device (with or without NEON support) you need to edit script. Change BUILD_DIR path and ANDROID_ABI CMake parameter.


Set the same environment variables as needed for OpenCV build (variables set in opencv\android\scripts\wincfg.cmd work as well).
If you are using wincfg.cmd file then ensure that ANDROID_ABI and BUILD_DIR are set to correct values. Use the following values for emulator:

SET BUILD_DIR=build_armeabi

When all variables are set run the commands

cd opencv\samples\android\hello-android

Connect to your device with adb tool from Android SDK and run the script


If you can see HelloAndroid.png in your current directory and “Hello Android!” message in console – you’ve arrived.


If you see error message similar to

ld: cannot find -lopencv_contrib

or (on Windows)

ld.exe: cannot find -lopencv_contrib240

It means that CMake failed to find OpenCV compiled for Android. Check that all paths are set to correct values, remove build directory and try to rebuild the application.

Native camera support in OpenCV

Native interface of Android camera is platform-dependent. So OpenCV isolates platform-dependent part of code into separate shared library and provides several prebuilt libraries for native camera:


These libraries are tested to work for Android 2.2.0, 2.2.1, 2.2.2, 2.3.3, 2.3.4, 3.0.1, 3.2.1, 4.0.0, 4.0.3, 4.1.1.
You should include these libraries into your .apk to get camera working. OpenCV will automatically load one of these libraries compatible with your Android OS version.

Use native camera in your Android project (Java)

See android samples for example of native camera application.
This one – opencv/samples/android/tutorial-2-opencvcamera is a good point to start.

Use native camera in your NDK project (C++)

Tutorial is coming…

Use native camera in your cmake project (C++)

Include the highgui.hpp header to you source

#include "opencv2/highgui/highgui.hpp"

Create a new instance of VideoCapture and start grabbing frames

cv::VideoCapture capture(CV_CAP_ANDROID + 0);
//cv::VideoCapture capture(CV_CAP_ANDROID + 1);//front camera for Android 2.3.3 or newer

if( !capture.isOpened() )

capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

for(int i=0; i < 100; ++i)
cv::Mat frame;

capture >> frame;
if( !frame.empty() )

Finally, add following lines to your cmake


Where PROJECT_NAME variable contains name of module that uses native camera.

Warning: Native camera from OpenCV does not work inside console applications.

Advanced: build your camera wrapper

If prebuilt libraries don’t work for you for whatever reason then you can build your own camera wrapper lib.

To build your lib you have to download sources for your version of Android and build entire OS first.
See and ’’google’’ for instructions.

When you finish with Android OS run cmake for OpenCV (scripts/ will run it for you). You should see following line in its output

— AndroidNativeCamera: use prebuilt libraries

Next, you need to set cmake cache variable ANDROID_SOURCE_TREE to the path to Android OS source tree and rerun cmake. (You can set this variable with cmake-gui application, or editing build/CMakeCache.txt file with text editor, or passing it as cmake argument).

If path is set correctly then you should see line similar to following in cmake output

— AndroidNativeCamera: build for Android 2.3.4

Run make command to compile the sources.

Most probably you will need to make changes in file opencv/modules/androidcamera/camera_wrapper/camera_wrapper.cpp to fix compiler errors.
If you do some changes please post your patch to OpenCV_issue_tracker to help with OpenCV development.


Building wrapper library for Android 4.0.3:

cd openvc/android
mkdir build_camera
cd build_camera
make native_camera_r4.0.3

Advanced build options

Using OpenCV in your own CMake projects

Use the CMake find script for OpenCV

find_package(OpenCV REQUIRED)

Export path to OpenCV source tree

export OPENCV_ROOT=~/opencv

Then when you run cmake, use:

cmake -DOpenCV_DIR=$OPENCV_ROOT/android/build -DCMAKE_TOOLCHAIN_FILE=$OPENCV_ROOT/android/android.toolchain.cmake ..

And direct your cmake cache for OpenCV_DIR to the path where you built OpenCV for Android.
something like : opencv/android/build

To avoid setting the cmake cache for OpenCV_DIR, you can just “install” OpenCV to your
Android toolchain/NDK. Run the following from the opencv/android/build path:

make install/strip

Android hardware targets

You may wish to build OpenCV and samples for multiple hardware targets.

Just change the cmake cache ANDROID_ABI to either:

  • armeabi
  • armeabi-v7a
  • armeabi-v7a with NEON
  • armeabi-v7a with VFPV3
  • armeabi-v6 with VFP
  • x86
  • mips

Android API levels

You may wish to build OpenCV and samples for different level of Android native API.

Just change the cmake cache ANDROID_NATIVE_API_LEVEL to appropriative value.

The following levels are currently supported:

Platform Version SDK API Level NDK API Level Version Code
Android 4.2 17 14 JELLY_BEAN_MR1
Android 4.1.1 16 JELLY_BEAN
Android 4.1
Android 4.0.4 15 ICE_CREAM_SANDWICH_MR1
Android 4.0.3
Android 4.0.2 14 ICE_CREAM_SANDWICH
Android 4.0.1
Android 4.0
Android 3.2 13 9 HONEYCOMB_MR2
Android 3.1.x 12 HONEYCOMB_MR1
Android 3.0.x 11 HONEYCOMB
Android 2.3.4 10 GINGERBREAD_MR1
Android 2.3.3
Android 2.3.2 9 GINGERBREAD
Android 2.3.1
Android 2.3
Android 2.2.x 8 8 FROYO
Android 2.1.x 7 5 ECLAIR_MR1
Android 2.0.1 6 ECLAIR_0_1
Android 2.0 5 ECLAIR
Android 1.6 4 4 DONUT
Android 1.5 3 3 CUPCAKE
Android 1.1 2 - BASE_1_1
Android 1.0 1 BASE