Setting up OpenCV 3 on Android

Rick from Rick and Morty!

Requirements

Set Up

Android Studio

  1. Download and install 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
  1. Add the NDK Path to your PATH environment variable
  2. Add an environment variable called ANDROID_NDK with (again) your NDK Path

OpenCV

  1. Download and extract both OpenCV 3 and the OpenCV 3 extra modules if you want the non-free features (such as SIFT and SURF)
  2. Copy the contents of the extra module into the regular OpenCV’s folder

Ninja

  1. Download and extract Ninja
  2. Add it to your PATH environment variable

CMake

  1. Download and install CMake
  2. Download the Android NDK CMake toolchain and extract it in a folder
  3. Restart your computer to refresh your PATH environment variable

Compiling

  1. Open CMake GUI
  2. Set the OpenCV source code folder and the destination folder
  3. Press configure and select Ninja as the generator
  4. Choose your own toolchain file and select the file named android.toolchain.cmake which you downloaded previously
  5. Choose your ABI in the CMake variables (you will probably want to enable NEON if you compile for ARMv7)
  6. If you want shared libraries instead of the default static ones you must enable BUILD_SHARED_LIBS
  7. Generate the configuration
  8. 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

Folder structure in Android Studio

  1. Create a new Android Gradle project with Android Studio
  2. Activate Gradle 2.5 (experimental for now)
  3. Import OpenCV4Android/sdk/java as a new Module
  4. Activate Gradle 2.5 on the imported OpenCV4Android Module
  5. Copy OpenCV4Android/sdk/native/jni/include into /src/main/jni
  6. Copy OpenCV4Android/sdk/native/3rdparty/libs & OpenCV4Android/sdk/native/libs into /src/main/jniLibs
  7. 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'
}