This project stands as an entry point for building OpenCV and OpenCV Contrib for the Android Platform. Besides the two bash scripts from this repository, other scripts, toolchains and SDKs are necessary. I compiled a list in the tutorial below.
OpenCV already offers pre-build Android bundles, available on sourceforge. Those bundles do not contain all the (if any) modules from contrib.
This was an issue for me, as I intended to use the Face Recognizer class, so I ended up building both OpenCV and OpenCV Contrib from sources.
Although the scripts are supposed to work both on Linux and Windows, I encountered more difficulties on Windows. In order to fix and avoid future frustration, I adapted some existing tools, performed troubleshooting and compiled this tutorial.
- for Windows: bash support
- Git
- Python >=2.4 [1]
- CMake >=2.8
- JDK
- Apache Ant
- Android SDK Tools (version 25.2.5 or earlier [2])
- Android NDK (will be downloaded by the
android-ndk-downloader
) - android-ndk-downloader (will be downloaded by
setup.sh
) - android-cmake (will be downloaded by
setup.sh
) - for Windows: MinGW/ GNU Make [3] or Ninja [4]
[1] Android NDK comes with python
in ANDROID_NDK_ROOT/prebuilt/linux-<variant>/bin/
(for Linux), or python.exe
in ANDROID_NDK_ROOT/prebuilt/windows-<variant>/bin/
(for Windows)
[2] Starting with SDK Tools 23.5.0, Google removed Ant build support. See: opencv/opencv#8460
[3] Android NDK comes with make.exe
, in ANDROID_NDK_ROOT/prebuilt/windows-<variant>/bin/
directory
[4] In my experience, Ninja performs builds faster than MinGW Make and much faster than GNU Make
Clone repository and run setup.sh
$ git clone https://github.com/JDFind/build-opencv-for-android.git
$ cd build-opencv-for-android
$ ./setup.sh [-v <opencv_version>]
This script will clone and checkout the specified version (default 3.3.0) of OpenCV and OpenCV Contrib and the latest versions of android-ndk-downloader and android-cmake.
The android-ndk-downloader will automatically be launched. After specifying the version, the Android NDK will be downloaded under ./android-ndk-downloader
.
For the toolchain to work correctly, the following environment variables are needed:
JAVA_HOME
set to the root of JDKJAVA_HOME/bin
added toPATH
ANDROID_SDK_HOME
set to the root of Android SDK directoryANDROID_SDK_HOME/bin
added toPATH
ANDROID_NDK_ROOT
set to the root of Android NDK directoryANT_HOME
set to the root of Apache Ant directoryANT_HOME/bin
added toPATH
ANT_HOME/bin
added toPATH
git
added toPATH
python
added toPATH
cmake
added toPATH
make
orninja
added toPATH
To include extra modules in the Java Wrapper, you must follow the steps below:
- navigate to
opencv_contrib/modules/<target_module>
- open
CMakeLists.txt
- check the parameters of
ocv_define_module
and addjava
afterWRAP
keyword if not present - if the
ocv_define_module
does not containWRAP
, add that beforejava
ocv_define_module(face opencv_core opencv_imgproc opencv_objdetect WRAP python java)
Note: re-running setup.sh
restores the contents of opencv, opencv-contrib, android-cmake and ndk-downloader to their original state, overriding any changes
After completing the steps above, run the appropriate script for your machine (build-linux
, build-win-make
or build-win-ninja
).
$ ./build-win-ninja.sh
The final library will be located in android_opencv/opencv
directory.
If you have any suggestions or questions regarding this project, feel free to submit issues or create pull requests.