
Requirements
- Android Studio 1.3+
- OpenCV 3
- OpenCV extra modules
- Ninja build system
- CMake
- Android NDK CMake toolchain
Set Up
Android Studio
- During installation include SDK Manager if you didn’t have it already installed
- After installation install Android NDK and Google Play Library from the SDK Manager
- Add the NDK Path to your
PATHenvironment variable - Add an environment variable called
ANDROID_NDKwith (again) your NDK Path
OpenCV
- Download and extract both OpenCV 3 and the OpenCV 3 extra modules if you want the non-free features (such as SIFT and SURF)
- Copy the contents of the extra module into the regular OpenCV’s folder
Ninja
- Download and extract Ninja
- Add it to your PATH environment variable
CMake
- Download and install CMake
- Download the Android NDK CMake toolchain and extract it in a folder
- Restart your computer to refresh your PATH environment variable
Compiling
- Open CMake GUI
- Set the OpenCV source code folder and the destination folder
- Press configure and select Ninja as the generator
- Choose your own toolchain file and select the file named
android.toolchain.cmakewhich you downloaded previously - Choose your ABI in the CMake variables (you will probably want to enable NEON if you compile for ARMv7)
- If you want shared libraries instead of the default static ones you must enable
BUILD_SHARED_LIBS - Generate the configuration
- Navigate to the destination folder with cmd and execute
ninja
But I just want to add the contrib modules to OpenCV4Android!
You can also follow these same steps but only compile the extra modules. After the compilation is done just copy the files inside the OpenCV4Android folder.
Setting up a new project

- Create a new Android Gradle project with Android Studio
- Activate Gradle 2.5 (experimental for now)
- Import OpenCV4Android/sdk/java as a new Module
- Activate Gradle 2.5 on the imported OpenCV4Android Module
- Copy OpenCV4Android/sdk/native/jni/include into /src/main/jni
- Copy OpenCV4Android/sdk/native/3rdparty/libs & OpenCV4Android/sdk/native/libs into /src/main/jniLibs
- Modify build.gradle to include the include folder and link the shared libraries
Sample build.Gradle:
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with {
applicationId = "com.example.uemcar"
minSdkVersion.apiLevel = 19
targetSdkVersion.apiLevel = 23
}
}
compileOptions.with {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
android.ndk {
moduleName = "jniPart"
ldLibs += ['log']
cppFlags += "-std=c++11"
cppFlags += "-fexceptions"
cppFlags += "-I${file("src/main/jni/include")}".toString()
ldLibs += ["android", "EGL", "GLESv2", "dl", "log", "z"]
stl = 'gnustl_shared'
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}
android.productFlavors {
create("arm7") {
ndk.with {
ndk.abiFilters += "armeabi-v7a"
File curDir = file('./')
curDir = file(curDir.absolutePath)
String libsDir = curDir.absolutePath + "\\src\\main\\jniLibs\\armeabi-v7a\\"
ldLibs += libsDir + "libopencv_java3.so"
ldLibs += libsDir + "libopencv_core.a"
ldLibs += libsDir + "libopencv_highgui.a"
ldLibs += libsDir + "libopencv_imgcodecs.a"
ldLibs += libsDir + "libopencv_calib3d.a"
ldLibs += libsDir + "libopencv_features2d.a"
ldLibs += libsDir + "libopencv_xfeatures2d.a"
ldLibs += libsDir + "libopencv_flann.a"
ldLibs += libsDir + "libopencv_hal.a"
ldLibs += libsDir + "libopencv_imgproc.a"
ldLibs += libsDir + "libopencv_ml.a"
ldLibs += libsDir + "libopencv_objdetect.a"
ldLibs += libsDir + "libopencv_photo.a"
ldLibs += libsDir + "libopencv_shape.a"
ldLibs += libsDir + "libopencv_stitching.a"
ldLibs += libsDir + "libopencv_superres.a"
ldLibs += libsDir + "libopencv_ts.a"
ldLibs += libsDir + "libopencv_video.a"
ldLibs += libsDir + "libopencv_videoio.a"
ldLibs += libsDir + "libopencv_videostab.a"
ldLibs += libsDir + "libIlmImf.a"
ldLibs += libsDir + "liblibjasper.a"
ldLibs += libsDir + "liblibjpeg.a"
ldLibs += libsDir + "liblibpng.a"
ldLibs += libsDir + "liblibtiff.a"
ldLibs += libsDir + "liblibwebp.a"
ldLibs += libsDir + "libtbb.a"
}
}
}
}
dependencies {
compile project(':openCVLibrary300')
compile 'com.google.android.gms:play-services:7.8.0'
compile 'com.android.support:appcompat-v7:23.0.1'
}