Installing OpenCV-2.4.9 in Windows 7 for Android - Detailed Explanation

After a 2 days of  frustration and surfing almost all the OpenCV tutorial sites and Stack Overflow, I finally got how to set up OpenCV for android SDK.

 Here goes the story,

1. Download and Install the new ADT (Android Development Kit) from Android SDK, unzip it in any drive.

(NOTE: To instal ADT, it requires Java 1.6 or higher, Java 1.7 is recommended, download it from oracle site, install it, and set environment variable in,
My Computer->Properties->Advanced System Settings->Advanced Tab->Environment Variables->Under SystemVariables->Edit the path variable and set the variable name to your java installation directory for ex: In my case, C:\Program Files\Java\jdk1.7.0_04\bin)

2. Open the eclipse folder in android-sdk folder open the eclipse.exe, to open the eclipse, and create workspace for store your projects.

3. To make OpenCV projects work we need to install CDT plug-in since OpenCV contains C++     code, and we also require Android Native Development Tools, to install this, go to

eclipse->help->install new software-> add work path as, and select Android Native Development Tools to install, if you downloaded new version of ADT it comes with it.

And, to install CDT plugin, go to, eclipse->help->install new software-> add work path as, and select basic C/C++ development tools. Once all the plug-ins installed restart the eclipse to take effect.

4. Next, download the OpenCV for Android from always download recent version of OpenCV repositories. After downloading extract that .zip into any drive.

5. Next, download the Android NDK from download recent version and extract them in any drive you wanted, C:\ drive is recommended.

6. Set environment variable for Android NDK as follows, My Computer->Properties->Advanced System Settings->Advanced Tab->Environment Variables-> Under user variables for project -> create new variable name as NDK and variable value as C:\android-ndk-r9d (in my case)->OK.

7. Open the Eclipse, File->import->Android->Existing Android Code into Workspace, browse to the folder where your OpenCV for Android SDK located, in my case, E:\OpenCV-2.4.9-android-sdk, after importing it imports the all the projects and make sure that openCV-library-2.4.9 is also there in the list, check the copy projects into workspace.

Make sure that sdk\java OpenCV Library - 2.4.9 in the project import box (see above figure). In the above image you can see Select at least one project error, since i already imported, it is not showing. Don't panic it shows the list in Project to import if your doing it first time. check all the projects to be imported into your workspace.

8. After importing all the projects clean the projects, go to Project-> clean -> select all projects to clean. Restart eclipse again.

9. select any project->right-click->properties->android->under Libray delete existing library and add new ( see below image)

select OpenCV Library-2.4.9 ->Ok->Ok. Repeat this process for all the projects you have imported into workspace. except the OpenCV Library - 2.4.9 project

If your project doesn't contain any C/C++ code/ jni folder in the source folder. it is not required to follow the bellow steps.

10.After adding library into the project most of the error in code with only Android project are without errors now. exept, OpenCV Sample - 15 Puzzle, Native Activity, And Mixed Processing, since they have /jni folder inside the project structure, to remove the C/C++ errors and JNI problems you need to follow the following steps:

a) Open Project Properties -> Select C/C++ general -> paths & symbols, It looks like following image,

Select GNU C++ in Includes tab, and add the following path to it:
select add, select File System option in the popup or you can type it. better you select file system and browse for the path to be set.

   Path 1: ${NDKROOT}/platforms/android-19/arch-arm/usr/include - (Please check the version and                             change it in /platforms/android-10 or API level you are using for Android app development)
   Path 2: ${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.6/include
   Path 3: ${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include
   Path 4 : ${NDKROOT}/sources/android/native_app_glue (Optional)
   Path 5: E:/OpenCV-2.4.9-android-sdk/sdk/native/jni/include (Path where your Android SDK resides)

Here, ${NDKROOT} was the root directory of where your NDK is residing, we already set the path in environmental variable step 6. If it is not working please do restart the eclipse. Or you can put the complete path for ${NDKROOT} as C:/android-ndk-r9d/ in all path settings.

NOTE: Please do check twice that you have put "/" for separating directories in the path, if it is "\" your program won't work, eclipse doesn't understand the path you set.

Once everything set say OK. it asks for do you want to remember C/C++ perspective? select No/Yes your wishes, it never hurts you anymore.

b) Add build command to build the JNI part of the program i.e., C++ part of the code must be compiled using the following command, to compile C++ part, Open Project->properties->go to C/C++ build , uncheck the use default build command and set that to C:/android-ndk-r9d/ndk-build.cmd  under builder settings (see image below). It shows the ${NDKROOT} which is a environment variable set in step 6, but you can also set it to fully qualified path like i mentioned everywhere. (But make sure that it should be done for all the projects)

And also make sure that the Behavior tab in the C/C++ build screen looks like the image shown below.

c) After doing all this you may see the errors like the image shown below,

To remove these errors we need to open the file in the /jni folder of  /project folder. Initially the include in the file consists like include ../../sdk.native/jni/include, change this to complete path of Android sdk for OpenCV, in my case it was, E:/Android_OpenCV/...../, make sure that the directory separator is "/" not "\", see the image below.

 d) Still you may not see any errors in the C++ file but it shows red mark over the project folder, this is because we need to remove the native support for Android in the eclipse project imported, to do this, follow this:

Open your workspace of your eclipse project and select the project to which the native support is removed, the project directory structure looks like, see image below.

You can see to files named .cproject and .project, delete the .cproject file from this folder and open the .project file in text editor or Notepad++, and do the following changes to it, look into the image below before modification and after modification.

Initially it looks like this,

From the .project file remove the following lines, you may find them in end of the .project file,

After removing it looks like the image below,

Restart your eclipse and run as administrator. It works fine.

I think this tutorial makes you to understand  how to set up OpenCV for Android in Windows-7.


Popular posts from this blog

Android Custom Navigation Drawer

Working with OpenCV in Visual Studio in Windows-7