Installing the OpenCV and Android development environment manually
To choose to manually install OpenCV and the Android development environment, you probably have the following installed components on your machine:
- Java SE Development Kit 6
- Android Studio
- Android SDK
- Eclipse IDE
- ADT and CDT plugin for Eclipse
- Android NDK
- OpenCV4Android SDK
You could go through the manual installation steps to make sure that you have all the needed components in order to start developing Android applications with OpenCV is ready and properly configured.
Java SE Development Kit 6
You can download the JDK installer for your OS from http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Android Studio
Another very good option to work with is the Android Studio. You can download the Android Studio from http://developer.android.com/sdk/index.html. Note that Android Studio comes bundled with Android SDK, so you don't need to install it if you go with this option. Additionally, you can skip the Eclipse and ADT installation and note that starting from Android Studio 1.3; you will find built-in support for NDK as well.
Android SDK
To download and install Android SDK, follow these steps:
- Go to http://developer.android.com/sdk/index.html.
- Scroll down to the SDK Tools Only section and click on the
.exe
file of Windows installer link. - After you have read and accepted the terms and conditions, click the download button.
- Save the installer on your disk and click on the
.exe
file to start the installer and then follow the onscreen instructions. - Keep a note of the SDK directory to refer to it later from the command line.
- Once the installation is done, Android SDK Manager will start.
- Select to install Android SDK Tools, revision 20 or newer.
- For the SDK platform, Android, select Android 3.0 (API 11) or higher. In my case, I used API 15 and you are recommended to do so.
- Read and accept the license agreement, then click Install.
Eclipse IDE
For OpenCV 2.4.x, it is recommended to have Eclipse 3.7 (Indigo) or Eclipse 4.2 (Juno); you can download your selected version from Eclipse's official website at http://www.eclipse.org/downloads/.
ADT and CDT plugins for Eclipse
Assuming that you have already downloaded Eclipse, you can follow these steps to download the Android Developer Tools (ADT) and C/C++ Development Tool (CDT) plugins:
- Launch Eclipse and then navigate to Help | Install New Software.
- Click the Add button in the top corner to the right.
- In the Add Repository dialog, write
ADT Plug-in
in the Name field and copy and paste this URL, https://dl-ssl.google.com/android/eclipse/, in the Location field. - Click OK.
- Check the Developer Tools checkbox.
- Click Next.
- A list of the tools to be downloaded will be shown in the next window. Just make sure that it includes the native support tools (CDT) and click Next.
- Read and accept the license agreement and click Finish.
- Once the installation is complete, you will need to restart Eclipse.
Android NDK
In order to develop for Android in C++, you will need to install Android NDK.
Note
Android NDK is not meant to be used in all situations. As a developer, you need to balance between the performance gains that come with using a native API and the introduced complexity.
In our case, as the OpenCV
libraries are written in C/C++, we might have to use NDK. However, using NDK shouldn't be just because the programmer prefers to write in C/C++.
Downloading Android NDK
You can download Android NDK by following these steps:
- Go to the Android NDK home page, http://developer.android.com/tools/sdk/ndk/index.html.
- In the Downloads section, select the version corresponding to your operating system. In my case, it is Windows 64-bit.
- Read and agree with the terms and conditions.
- Click the Download button.
Installing and configuring Android NDK
Once the download has finished, you will need to follow these steps to configure NDK:
- Navigate to the NDK download folder.
- Double-click on the downloaded file to extract it.
- Rename and move the extracted folder; I'll refer to the
ndk
folder as<ndk_home>
. Now you are ready to use NDK to build your projects. - If you prefer to build from the command line, you will need to add the
<ndk_home>
folder (in my case,C:/android/android-ndk-r10d
) to your PATH environment variable. For Windows, open CMD. Enter the following command and replace thendk
directory with yours:set PATH=%PATH%;c:/android/android-ndk-r10d
- To check that NDK is configured properly, go to the directory that contains your project. For simplicity, you can test on the
hello-jni
sample project. You can find it under<ndk_home>/samples/
. - Change the directory by executing the command
cd <your_project_directory>/
. Run the following command:ndk-build
- As depicted in the console output, the files with the
.so
extension are the compiled version of the C/C++ source code used in this project:
Building native code using Eclipse
If you prefer to build from Eclipse, which is more convenient, you will need to tell Eclipse where to find NDK so that you can build your apps:
- Launch Eclipse and open Window | Preferences.
- In the left-hand side pane, open the Android tree.
- Select the NDK tree node and in the right-hand side pane, click Browse and select the
<ndk_home>
directory. - Click OK.
- Import the
hello-jni
sample project from<ndk_home>/samples/
as an Android project. - Open the Project Explorer and right-click on the
hello-jni
project. - In the context menu, navigate to Android Tools | Add Native Support to convert this project to a C++ project.
- Accept the default library name and click Finish.
- Build the application.
In the console, you will see a list of .so
files, which are the compiled C++ part of this project. Still, if you open any C/C++ file from the imported project, you will see many highlighted errors. You just need to do some extra steps related to the CDT plugin:
- Navigate to Project | Properties. In the left-hand side pane, expand the C/C++ General node.
- Select Paths and Symbols.
- In the right-hand side pane, select the Includes tab.
- Click Add and then File system to add the following paths:
- If you installed NDK r8 or prior:
<ndk_home>/platforms/android-9/arch-arm/usr/include<ndk_home>/sources/cxx-stl/gnu-libstdc++/include<ndk_home>/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/include
- If you installed NDK r8b or later:
<ndk_home> /platforms/android-9/arch-arm/usr/include
<ndk_home>/sources/cxx-stl/gnu-libstdc++/4.6/include
<ndk_home> /sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include
- If you installed NDK r8 or prior:
- Click OK. Eclipse will rebuild the project and all the syntax errors should be cleared from Eclipse.
- Now, you can build the project to package both the Java and native code in one APK. To install the application on the emulator of your choice, use the menu item, Run | Run As | Android Application.
OpenCV4Android SDK
To be able to use the OpenCV collection of native (C/C++) libraries on your Android device, you need to install OpenCV4Android SDK, which is a part of OpenCV to run on the Android operating system.
- First, go to the OpenCV download page, http://sourceforge.net/projects/opencvlibrary/files/opencv-android/.
- Download the latest available version, which, at the time this book is being written, was 2.4.10.
- Extract the compressed file to a convenient path, for example,
C:\opencv\
.Note
It is highly recommended to use paths with no spaces to avoid any problems with
ndk-build
.