A Proper Beginner’s Guide to Installing OpenCV Android in Android Studio (Updated)

Kashaf Ahmed
5 min readNov 20, 2018

--

I recently started a project that involved working with OpenCV on Android for my mid year Project. Most of the guides on setting up the library on Android were outdated or not complete.So, I decided to just write a systematically dead simple guide on this.

What is OpenCv ?

OpenCV is an open source computer vision and machine learning Software library and it is aimed at implementing Image Processing and Computer Vision functionalities in real time applications. Yes-it’s open source and it was originally written in C++. You’d be amazed at the ways in which such a library could come handy.

PS: The following tutorial will show you integrating OpenCV 3.4.3 in Native Android project which means integrate it in Android Studio(version 3.2.1)

Setup and Integration: OpenCV for Android

Step 1: Download the OpenCV SDK for Android. At the time of making this article,OpenCv 3.4.3 is latest. Make note of the destination folder- OpenCV-android-sdk.

Step 2: Create a new project.

Step 3: Import the OpenCV Module. Go to File -> New -> Import Module. This is where your destination folder comes into play. My OpenCV-android-sdkfolder is in Downloads -> Compressed, so that’s exactly where I’m headed! Select the Source Directory path to the sdk/java folder (If the download goes well, you WILL have this folder in the OpenCV-android-sdk folder ).Here are some screenshots that will give you more clarity.

Step 4: A Gradle project sync error! Gradle script of your app and of openCV doesn’t match. So, there’ll be a sync error which is completely NORMAL

Let’s clear each error one by one

First error : The targetSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.

Select Android from the drop-down at the top of the Project window

Remove minSdkVersion and targetSdkVersion. if it’s in AndroidManifest of openCVLibrary

Now here’s the confusing bit- there are so many .gradle files, which one should you be choosing? Select Project from the drop-down at the top of the Project window.You need to match the following between the build.gradle under the app folder and the build.gradle of the openCVLibrary folder:

· compileSdkVersion

· buildToolsVersion

· minSdkVersion

· targetSdkVersion

Alter the build.gradle of the OpenCV Library and modify it to the values of these parameters in the build.gradle of the app. Do NOT tamper with the build.gradle of the app.

Then press sync.

Step 5: Adding the OpenCV Module Dependencies.Right click the app module as shown and proceed by clicking on Open Module Settings.Under Dependencies, choose the ‘+’ sign and select the option called Module Dependency. You will see the OpenCVLibrary module, click on OK after choosing it.

Step 6:The libsfolder present in OpenCV-android-sdk/sdk/native has to be COPIED to the app/src/main folder of your Android project.

Step 7: RENAME the copied libs folder tojniLibs. Build your app- it should work fine!

Step 8: Now you need to set the NDK path for that you need to download NDK from this link you can download the NDK according to your OS.

Copy the link where you have downloaded the NDK and paste it inside your local.properties (as shown)

Sync your project. Build -> Make Project .

Step 9: Open MainActivity.java and write following code inside onCreate(Bundle savedInstanceState)

if(OpenCVLoader.initDebug()){
Toast.makeText(this, "openCv successfully loaded", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "openCv cannot be loaded", Toast.LENGTH_SHORT).show();
}

Run your App on your android device but if you have skipped step 6 and 7 then this optional step is what you need to run your app on android device.

If there’s any error about Android.mk then add this piece of code into your build.gradle(Module: app) below buildTypes{}

sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jniLibs/'] } }
externalNativeBuild {
ndkBuild {
path 'build/intermediates/ndk/debug/Android.mk'
}
}

Step 10 (Optional Step): Running the App on your Android Device.I don’t recommend testing the app via an emulator. Use an actual Android device. You CANNOT run it on your phone without this.

The OpenCV Manager available at the Google Play Store

That is all.

This is my first attempt at writing a technical article for Medium so any suggestion would be appreciated.

If you enjoyed this story, please click the 👏 button and share to help others find it!

--

--

Kashaf Ahmed

Android developer|enthusiastic about Android|Unicorn developer|5alco and a good guy|cheers🍻