Tutorial: Setting up OpenCV on Android Studio made simple and easy !

Why make this tutorial?

I recently started working on a project in which I needed to build an application for the android device that used computer vision. As OpenCV is one of the best open source tools available for the development of computer vision technology, I decided to use it. It was when I tried to configure it with my Android Studio, I realized how tricky it was and how useless and unhelpful the internet can be. Now that I have set up OpenCV, I decided to write a short tutorial on how I did it so that I might be able to help someone like me out there.

Setting things up!

There are two ways that I came across to set up OpenCV, one is very very simple, and requires only a few lines of code, without even the need to download the OpenCV library. There is only one drawback of this method, and that is that an you would not be able to use the most up to date version of OpenCV but an older version. The other method requires you to download the OpenCV library from its website and then import it into your project.

Method 1: (openCV version 2.4.8)

For this method, you just need to open your Android Studio, set up a new project and then open its build.gradle.

build.gradle(before)
build.gradle(before)

Now only the following lines need to be added here to add the OpenCV dependency:

repositories {
    maven {
        url 'https://raw.github.com/vRallev/mvn-repo/master/'
    }
}

dependencies {
    compile 'org.opencv:opencv-android:2.4.8'
}

Now your build.gradle file should look somewhat like this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.haidermushtaq.bullincarnadine.opencvtesting"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'org.opencv:opencv-android:2.4.8'
}

repositories {
    maven {
        url 'https://raw.github.com/vRallev/mvn-repo/master/'
    }
}

For your ease, I have highlighted the lines that I have added in blue and the name of my package in red which if you copy this, you would probably need to change.

After you have done all this, you just need to build your gradle file by clicking on the sync project with Gradle files button.

build.gradle(after)
build.gradle(after)

AND THAT IS ALL!

Method 2:  (openCV version 3.0 and above)

For the second method, you would need to go to the openCV offical website and download the latest version of OpenCV for Android:

openCV_website
openCV_website
downloadOpenCV
downloadOpenCV Mirror link for OpenCV (Version 3.0)

After you have downloaded the .rar file, you just need to extract its content

opencvExtract
opencvExtract

After this, you need to go to >> \OpenCV-android-sdk\sdk by opening first the extracted folder and then sdk folder. The sdk folder should contain 3 files:

sdk
sdk

You need to copy the java file from this folder and open your Android Studio project, i.e the root file for the project that you are working on, in which you want to use the openCV library. In my case, my project name is : piClone. Here you need to create a new Folder by the name of libraries.

Project root folder
Project root folder

You need to paste the java file that you copied here, inside the libraries folder and then rename it to opencv

paste java here
paste java here and rename to opencv

Next, you need to open this opencv folder and create a new text file, by the name build.gradle  . Paste the following code inside it:

apply plugin: 'android-library'

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
  }
}

android {
  compileSdkVersion 22
  buildToolsVersion "22.0.1"

  defaultConfig {
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 2490
    versionName "2.4.9"
  }

  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      java.srcDirs = ['src']
      resources.srcDirs = ['src']
      res.srcDirs = ['res']
      aidl.srcDirs = ['src']
    }
  }
}

You would probably need to change the code that I have highlighted in blue, it should be the same as your project’s build.gradle file. In my case, my project’s build.gradle file is in its  >> C:\Users\Haider\AndroidStudioProjects\piClone\app you can also access this from Android studio under Gradle Scripts by the name build.gradle(Module:app). The content of my build.gradle file is as follows:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.haidermushtaq.bullincarnadine.piclone"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1' 
}

After all of this is done, you need to open your settings.gradle file (also located under Gradle Scripts) and add the following code in red:

include ':app'
include ':libraries:opencv'

Now sync your project by clicking on the Sync Project with Gradle Files button:

sync project
sync project

Now you need to add  Module Dependency, you can do this by selecting, in your Android Studio File/Project Structure

selecting project structure
selecting project structure

A popup will appear. Here you need to select app under Modules, then choose its Dependencies tab, and then click on the + button then choose Module dependency

project structure
project structure

Now another popup with the name Choose Modules would appear, you need to select :libraries:opencv then press ok to all

Choose Modules
Choose Modules

And now for the final step, you need to copy the libs file from your extracted opencv file. You can find the libs folder in >> \OpenCV-android-sdk\sdk\native

copy libs
copy libs

and paste this folder into your Projects file in the app\src\main directory, which in my case is: >> C:\Users\Haider\AndroidStudioProjects\piClone\app\src\main. Now you need to rename this file to jniLibs. Your app\src\main directory should now look like this:

jnilibs
app\src\main directory

And that is it, all is set, Just Sync Project with Gradle File again and you have sucessfully configured OpenCV with your project.

sync project
sync project again

3 Comments


  1. Awesome Tutorial Sir…
    Thank you so much

    Reply

  2. I did exactly what has been taught in the tutorial but got this error:”Error:Configuration with name ‘default’ not found.” What should I do??

    Reply

Leave a Reply

Your email address will not be published.