

.. _Android_Binary_Package_with_NDK:


Using C++ OpenCV code with Android binary package
*************************************************

The Android way is writing all your code in Java. But somethimes it is not enough and you need to go to a native level and write part of your application in C/C++.
This is important when you already have some computer vision functionality which is written in C++ and uses OpenCV, and you want to use it in your Android application,
but do not want to rewrite the C++ code to Java. 
In this case the only way is to use JNI mechanism. 
It means, that you should add a class with native methods wrapping your C++ functionality into the Java part of your Android application.

This tutorial describes a fast way how to create and build Android applications containing OpenCV code written in C++. It shows how to build an application which uses OpenCV inside its JNI calls.

Please note that before starting this tutorial you should fulfill all the steps, described in the tutorial :ref:`Android_Binary_Package`.

This tutorial was tested using Ubuntu 10.04 and Windows 7 SP1 operating systems. 
Nevertheless, it should also work on Mac OS X. If you encounter errors after following the steps described here, feel free to contact us via *android-opencv* discussion group https://groups.google.com/group/android-opencv/ and we will try to help you.


Prerequisites: Setup NDK
========================

To compile C++ code for Android platform you need Android **N**\ ative **D**\ evelopment **K**\ it (*NDK*).

You can get the latest version of NDK from the page http://developer.android.com/sdk/ndk/index.html .

To install Android NDK just extract the archive to some folder on your computer. (Here is installation instructions on the NDK home page: http://developer.android.com/sdk/ndk/index.html#installing)

.. note:: Before start you can read official Android NDK documentation which is in the Android NDK archive, in the folder :file:`docs/`.

    The main article about using Android NDK build system you can read in the file :file:`ANDROID-MK.html`. 

    Also some additional useful information you can read in the files
    :file:`APPLICATION-MK.html`, :file:`NDK-BUILD.html`, and in the files :file:`CPU-ARM-NEON.html`, :file:`CPLUSPLUS-SUPPORT.html`,  :file:`PREBUILTS.html`. 

Theory: Android application structure
=====================================

Usually code of an Android application has the following structure:

+ :file:`root folder of the project/`

  - :file:`jni/`

  - :file:`libs/`

  - :file:`res/`

  - :file:`src/`

  - :file:`AndroidManifest.xml`

  - :file:`default.properties`

  - :file:`... other files ...`

where

+ the :file:`src` folder contains Java code of the application,

+ the :file:`res` folder contains resources of the application (images, xml files describing UI layout , etc),

+ the :file:`libs` folder will contain native libraries after successful build,

+ and the :file:`jni` folder contains C/C++ application source code and NDK's build scripts :file:`Android.mk` and :file:`Application.mk`.
   
  These scripts control the C++ build process (they are written in Makefile language). 


Also the root folder should contain the following files 

* :file:`AndroidManifest.xml` file presents essential information about application to the Android system 
  (name of the Application, name of main application's package, components of the application, required permissions, etc) 
  
  It can be created using Eclipse wizard or :command:`android` tool from Android SDK

* :file:`default.properties` is a text file containing information about target Android platform and other build details.
  
  This file is generated by Eclipse or can be created with :command:`android` tool from Android SDK

.. note:: Both files (:file:`AndroidManifest.xml` and :file:`default.properties`) are required to compile the C++ part of the application (NDK build system uses information from these files). If any of these files does not exist, compile the Java part of the project before the C++ part. 


Theory: How to build Android application having C++ native part (from command line)
===================================================================================

Here is the standard way to compile C++ part of an Android application: 

#. Open console and go to the root folder of Android application

   .. code-block:: bash

        cd <root folder of the project>/

   .. note:: Alternatively you can go to the folder :file:`jni` of Android project but samples from OpenCV binary package are configured for building from project root level (because of relative path to the OpenCV library).

#. Run the following command

   .. code-block:: bash

        <path_where_NDK_is_placed>/ndk-build

   .. note:: If you are working in *cygwin* shell and encounter an error saying that NDK does not find some *cygwin*\ 's path then you might need to define the following environment variable:

       .. code-block:: bash

            export NDK_USE_CYGPATH=1

#.   After executing this command the C++ part of the source code is compiled.

After that the Java part of the application can be (re)compiled (using either *Eclipse* or :command:`ant` build tool). 

.. note:: Some parameters can be set for the :command:`ndk-build`:

    **Example 1**: Verbose compilation

    .. code-block:: bash

        <path_where_NDK_is_placed>/ndk-build V=1

    **Example 2**: Rebuild all

    .. code-block:: bash

        <path_where_NDK_is_placed>/ndk-build -B


.. _Android_NDK_integration_with_Eclipse:


Theory: How to build Android application having C++ native part (from *Eclipse*)
================================================================================

There are several possible ways to integrate compilation of C++ code by Android NDK into Eclipse compilation process. We recommend the approach taken from this site: http://mobilepearls.com/labs/ndk-builder-in-eclipse/

.. important:: This instructions should be applied for each Android project in *Eclipse* workspace. So if you have 3 projects having C++ part then you need to configure 3 builders.

Below is an adapted version of this guide:

#. Navigate to :guilabel:`Package Explorer` window and expand your project having JNI resources.

   If you can not see :file:`libs` folder under this project then you need to create it manually. (It will be required on step 7, but you need to create it before you open project properties.)

#. Right click on your project in :guilabel:`Package Explorer` window and select :guilabel:`Properties`.

#. In the :guilabel:`Properties` dialog select :guilabel:`Builders` menu and press the :guilabel:`New...` button:

     .. image:: images/eclipse_builders.png
        :alt: Configure builders
        :align: center

#. In the resulting dialog select the :guilabel:`Program` type and press :guilabel:`OK` button:

     .. image:: images/eclipse_builder_types.png
        :alt: Choose builder type
        :align: center

#. In the :guilabel:`Main` tab fill the following fields:

    * :guilabel:`Name` - any name for your builder. ("Tutorial 2.1 Builder" in my case.)

      .. note:: This name has to be unique for each project in your workspace.

    * :guilabel:`Location` - full path to :command:`ndk-build` tool.

      + *UNIX*

        Just put full path to :command:`ndk-build` into this filed. Also you can add some options to the :guilabel:`Arguments:guilabel:` fied, for example ``-B`` option.

      + *Cygwin*

        - Instead of path to the :command:`ndk-build` tool you need to put full path to *cygwin*\ 's :program:`bash.exe` location. E.g: :file:`C:\\cygwin\\bin\\bash.exe`.

        - Put full path to :command:`ndk-build` into the :guilabel:`Arguments` field E.g. :file:`C:\\Android\\android-ndk-r6\\ndk-build`.

        - Go to the :guilabel:`Environment` tab and define an environment variable:
             
           * :envvar:`PATH` - full path to the *cygwin* tools. E.g. :file:`C:\\cygwin\\bin`
    
     .. image:: images/eclipse_windows_environment.png
                :alt: Define environment variables
                :align: center

    * :guilabel:`Working Directory` - put path to your project into this field. Instead of hardcoding full path you can click :guilabel:`Browse Workspace...` button and select your project.

     .. image:: images/eclipse_edit_configuration_main.png
                :alt: Define environment variables
                :align: center

#. Go to the :guilabel:`Refresh` tab and select both :guilabel:`Refresh resources upon completion` and :guilabel:`Recursively include sub-folders`.

   Next set the :guilabel:`Specific resources` option and click :guilabel:`Specify resources...` button:

    .. image:: images/eclipse_edit_configuration_refresh.png
               :alt: Define environment variables
               :align: center

#. Select :file:`libs` folder under your project and click :guilabel:`Finish`:

    .. image:: images/eclipse_edit_configuration_specify_resources.png
               :alt: Select resources folder to refresh automatically 
               :align: center

#. Go to the last tab :guilabel:`Build options`. Make sure that all checkboxes are set as shown on the next screen:

    .. image:: images/eclipse_edit_configuration_build_options.png
               :alt: Configure build options
               :align: center

#. Next, click the :guilabel:`Specify resources...` button.

#. Select :file:`jni` folder of your project and click the :guilabel:`Finish` button:

    .. image:: images/eclipse_edit_configuration_build_resources.png
               :alt: Select resources to build
               :align: center

#. Finally press :guilabel:`OK` in the builder configuration and project properties dialogs. If you have automatic build turned on then console showing build log should appear:

    .. image:: images/eclipse_NDK_build_success.png
               :alt: Select resources to build
               :align: center

Theory: The structure of :file:`Android.mk` and :file:`Application.mk` scripts
==============================================================================

The script :file:`Android.mk` usually have the following structure: 

.. code-block:: make

        LOCAL_PATH := $(call my-dir)

        include $(CLEAR_VARS)
        LOCAL_MODULE    := <module_name>
        LOCAL_SRC_FILES := <list of .c and .cpp project files>
        <some variable name> := <some variable value>
        ...
        <some variable name> := <some variable value>

        include $(BUILD_SHARED_LIBRARY)

This is the minimal file :file:`Android.mk`, which builds a C++ source code of an Android application. Note that the first two lines and the last line are mandatory for any :file:`Android.mk`.

Usually the file :file:`Application.mk` is optional, but in case of project using OpenCV, when STL and exceptions are used in C++, it also should be written. Example of the file :file:`Application.mk`:

.. code-block:: make

        APP_STL := gnustl_static
        APP_CPPFLAGS := -frtti -fexceptions
        APP_ABI := armeabi-v7a

Practice: Build samples from OpenCV binary package
==================================================

OpenCV binary package includes two samples having JNI resources:

* *Tutorial 2 Advanced - 1. Add Native OpenCV*
  
  This sample illustrate how you can use OpenCV in C++ but without OpenCV Java API.

* *Tutorial 2 Advanced - 2. Mix Java+Native OpenCV*

  This sample shows how you can mix OpenCV Java API and native C++ code.

To build these samples you need to:

#. Fulfill all the steps, described in the tutorial :ref:`Android_Binary_Package`.

#. Setup one builder for *"Tutorial 2 Advanced - 1. Add Native OpenCV"* project (as described in :ref:`Android_NDK_integration_with_Eclipse`)

#. Setup second builder for *"Tutorial 2 Advanced - 2. Mix Java+Native OpenCV"* project (repeat the steps from :ref:`Android_NDK_integration_with_Eclipse`)

#. Clean these projects (in the main Eclipse menu: :menuselection:`Project --> Clean...`)

#. Run Eclipse build command (if option :guilabel:`Build Automatically` is not set)


Practice: Create an Android application, which uses OpenCV
==========================================================

To build your own Android application, which uses OpenCV from native part, the following steps should be done:

#. The archive with OpenCV binary package should be downloaded and extracted to some folder (as example, into the home folder)

#. We recommend to use an environment variable to specify the location of OpenCV package. Full or relative path hardcoded in :file:`jni/Android.mk` will also work.

   So, the environment variable :envvar:`OPENCV_PACKAGE_DIR` should be defined. 
   The value of the variable should points to the folder, where the OpenCV package has been extracted. 


   As an example, on *UNIX* you can add add the following line into the hidden file :file:`.bashrc` placed in your home folder: 
   
   .. code-block:: bash

        export OPENCV_PACKAGE_DIR = <path to the extracted OpenCV package>
        
   Then relogin (or better reboot your computer).
   
   .. attention:: without rebooting (or logout) this change might not work.

   If you are a *Windows* user, then navigate to:

   * *Windows 7 / Windows Vista*

     :menuselection:`My Computer (Right Click on Icon) --> Properties (Link) --> Advanced System Settings (Link) --> Advanced (Tab) --> Environment Variables (Button) --> System variables (Section)`

   * *Windows XP*

     :menuselection:`My Computer (Right Click on Icon) --> Properties (Link) --> Advanced (Tab) --> Environment Variables (Button) --> System variables (Section)`

   Create new variable :envvar:`OPENCV_PACKAGE_DIR` and similarly to *UNIX* relogin or reboot.

   If you are setting NDK builder as described above in :ref:`Android_NDK_integration_with_Eclipse`, then you can define this variable in builder settings. It can be done on third :guilabel:`Environment` tab of the builder configuration window (we have already added some variables to this tab on *Windows* but skipped it for other platforms). 

#.  The file :file:`jni/Android.mk` should be written for the current application using the common rules for the file.

    For detailed information see the Android NDK documentation from the Android NDK archive, in the file
    :file:`<path_where_NDK_is_placed>/docs/ANDROID-MK.html`

#. The line 

   .. code-block:: make

           include $(OPENCV_PACKAGE_DIR)/share/OpenCV/OpenCV.mk

   should be inserted into the :file:`jni/Android.mk` file right after the line

   .. code-block:: make

        include $(CLEAR_VARS)

   .. note:: If your application utilize both native (C++) OpenCV and its Java API you need to put the following line before including :file:`OpenCV.mk` to avoid conflict between C++ and Java builders:

       .. code-block:: make

            OPENCV_CAMERA_MODULES:=off

#. The file :file:`Application.mk` should exist and should contain lines

   .. code-block:: make

        APP_STL := gnustl_static 
        APP_CPPFLAGS := -frtti -fexceptions

   Also the line

   .. code-block:: make

                 APP_ABI := armeabi-v7a 

   is recommended for the applications targeting modern ARMs 
   
#. To build the C++ code the Android NDK script :command:`ndk-build` should be run in the root directory of application. 
   Then the C++ source code using OpenCV will be built by Android NDK build system. 
   After that the Java part of the application can be rebuild and the application can be installed on an Android device. 

   Note that this step requires calling the :command:`ndk-build` script from the console. Instead of this step you can use integration of Android NDK into Eclipse
   as stated above in  the section :ref:`Android_NDK_integration_with_Eclipse` .


Additional C++ support in Eclipse
==================================

Note that you can install additional C++ plugins in Eclipse:

#. Open :guilabel:`Help / Install New Software`. This shows the :guilabel:`Install` dialog.

#. In the :guilabel:`Work with` drop-down list choose :guilabel:`Helios - http://download.eclipse.org/releases/helios` (or :guilabel:`Indigo - http://download.eclipse.org/releases/indigo` depending on your Eclipse version) and wait while the list of available software is loaded.

#. From the list of available software select :menuselection:`Programming Languages --> C/C++ Development Tools`.

#. Click :guilabel:`Next`, click :guilabel:`Next` again, accept the agreement, and click the :guilabel:`Finish` button.

#. When installation is finished, click :guilabel:`Reload`
