Version 1/13 - Next » - Current version
Alexander Smorkalov, 2014-10-02 11:43 am


Building the trunk OpenCV for Android

Contents

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 http://code.google.com/p/android-cmake/

Prerequisites

Additional requirements for Java wrapper and Android samples

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 r7 or use one of alternative downloads. These two are tested to work for OpenCV:

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 Trunk_OpenCV_for_Android section for instructions.

Linux (Ubuntu) and Mac OS (10.6.7)

Export full path to your copy of NDK

export ANDROID_NDK=~/android-ndk-r7

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-r7b/ 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/android
sh ./scripts/cmake_android.sh

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

cd build
make -j8

That will build most of the OpenCV modules, except for those that don't make sense on android - gpu, etc..

Troubleshooting

If you get an error saying `install_name_tool not found`, then:
  1. search for the file install_name_tool on your file system (usually it is in /usr/bin/);
  2. open the file /opt/local/share/cmake-2.8/Modules/CMakeFindBinUtils.cmake;
  3. after the line FIND_PROGRAM(CMAKE_INSTALL_NAME_TOOL NAMES install_name_tool HINTS ${_CMAKE_TOOLCHAIN_LOCATION}) add the line
    set( CMAKE_INSTALL_NAME_TOOL <path to install_name_tool>)
    

    e.g. something like
    set( CMAKE_INSTALL_NAME_TOOL /usr/bin/install_name_tool)
    

    Next remove your build folder and restart from the ./scripts/cmake_android.sh script.

Windows (native)

Define the following environment variables:
  • ANDROID_NDK - full path to the Android NDK. This path should not have tailing slash.
  • CMAKE_EXE - full path to cmake executable (including executable name).
  • MAKE_EXE - full path to native port of make (including executable name). This path should not contain spaces.

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\android folder. Run script cmake_android.cmd from the scripts folder:

cd opencv\android\
scripts\cmake_android.cmd

When script finishes you will have OpenCV compiled.

Troubleshooting

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.

Prerequisities

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'':http://google.com 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 Trunk_OpenCV_for_Android 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.