How To: Setup Android IDE with SDK, NDK for OpenCV

on under asbach
3 minute read

Out Of Date Warning

This article was published on 03/05/2011, this means the content may be out of date or no longer relevant.
You should verify that the technical information in this article is still up to date before relying upon it for your own purposes.

/Gastbeitrag von Sebastian Starke/

This tutorial will give a brief overview about the installation and configuration of the opencv Framework for Android.

The tutorial is tested for:

  • SWIG: Version 1.3.40 – Compiled with g++ [i686-pc-linux-gnu]
  • Android-SDK: android-sdk_r10-linux_x86.tgz, Checksum: c022dda3a56c8a67698e6a39b0b1a4e0
  • Android-SDK Platform-tools, revision 3
  • SDK Platform Android 2.2, API 8, revision 2
  • OpenCV; Rev: 4962; URL: https://code.ros.org/svn/opencv/trunk/opencv; Autor: vp153

Reference documentations/tutorials:

  • http://android-cmake.googlecode.com/hg/documentation.html
  • http://opencv.willowgarage.com/wiki/AndroidExperimental
  • http://opencv.willowgarage.com/wiki/Android

Setup Ubuntu 11.04

{% highlight bash }
$ sudo apt-get install swig mercurial subversion cmake cmake-gui
{
endhighlight %}

Setup Android-SDK

Download the android-sdk from:

  • http://developer.android.com/sdk/index.html

Unpack the Downloaded Package:

{% highlight bash }
$ cd ~/; tar xvzf android-sdk_r10-linux_x86.tgz
{
endhighlight %}

Add this to your .bashrc:
{% highlight sh }
export ANDROID_SDK=~/android-sdk-linux_x86
export PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$PATH
{
endhighlight %}

Update your environment:
{% highlight sh }
$ . .bashrc
{
endhighlight %}

Setup SDK and test the environment Variables:

{% highlight sh }
$ android
{
endhighlight %}

  • Available Packages → Install at least the packages „Android-SDK Platform-tools“ and “SDK Platform Android 2.2”
  • Virtual Devices → Create a new AVD (Android Virtial Device) with the Target “Android 2.2 – API Level 8”

Add this to your .bashrc:
{% highlight sh }
export ANDROID_SDK=~/android-sdk-linux_x86
export PATH=$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$PATH
{
endhighlight %}

Test your environment

Try hello-cmake:

  • http://android-cmake.googlecode.com/hg/docs/hello-cmake.html
    Try hello-android-cmake:
  • http://android-cmake.googlecode.com/hg/docs/hello-android-cmake.html

Setup Android-NDK 5

Add this to your .bashrc:

{% highlight sh }
export NDK=~/android-ndk-r5b
export NDK_ROOT=~/android-ndk-r5b
{
endhighlight %}

Setup Android-Cmake

Add this to your .bashrc:
{% highlight sh }
alias android-cmake=’cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN
export ANDROID_CMAKE=~/android-cmake
{
endhighlight %}

Setup standalone Android-Toolchain

Add this to your .bashrc:
{% highlight sh }
export ANDTOOLCHAIN=$ANDROID_CMAKE/toolchain/android.toolchain.cmake
export ANDROID_NDK_TOOLCHAIN_ROOT=$HOME/android-toolchain
{
endhighlight %}
Install the toolchain with:

{% highlight sh }
$NDK/build/tools/make-standalone-toolchain.sh -platform=android-5 —install
dir=$HOME/android-toolchain
{
endhighlight %}

Setup OpenCV

{% highlight sh %}
cd ~/
svn co https://code.ros.org/svn/opencv/trunk/opencv opencv-r4964 -r 4964
ln -s ~/opencv-r4964 ~/opencv

  1. Build OpenCV Static Lib
    ln -s ~/android-ndk-r5b ~/android-ndk-r4-crystax
    cd ~/opencv/android
    sh cmake_android.sh
    cd ~/opencv/android/build
    make -j8
    {% endhighlight %}

If you get a error message like this Compile error: something with gtest fix it with:

{% highlight sh }
cmake-gui …
{
endhighlight %}

  • disable Google Test

If you get a error like this Compile error ~/opencv/modules/ts/src/ts_gtest.cpp:6950: clone not declared in this scope fix it with:

  • Edit the file ~/opencv/modules/ts/src/ts_gtest.cpp and add the declaration of the function at line 6944:
    {% highlight c }
    int clone (int (*fn) (void *), void *child_stack, int flags, void *arg);
    {
    endhighlight %}

Build OpenCV Dynamic Lib

{% highlight sh }
cd ~/opencv/android/android-opencv
sh cmake_android.sh
cd ~/opencv/android/android-opencv/build
make
cd ~/opencv/android/android-opencv
sh project_create.sh
{
endhighlight %}

List available targets, so we can define it in the next step

{% highlight sh }
android list target
{
endhighlight %}

Edit „project_create.sh“ and add the Argument „—target 1“

{% highlight sh }
ant compile
{
endhighlight %}

If you get a error like this Compile/Build Error: libraries not found! fix it with:

  • Edit the file build.xml and add the following lines just before the project closing tag </project>:

{% highlight xml }


{
endhighlight %}

Compile again:

{% highlight sh }
ant compile
{
endhighlight %}

Build CVCamera App
{% highlight sh }
cd ~/opencv/android/apps/CVCamera
mkdir build
cd build
android-cmake ..
cd ..
ant compile
ant install
{
endhighlight %}

YOU DID IT! IT’S DONE, FINISHED! LET IT ROCK ;)