A Beginner’s Guide to Installing OpenCV Android in Android Studio

Sukriti Paul
Jun 22, 2017 · 4 min read

Albeit the plethora of resources and documentation pertaining to OpenCV and OpenCV Android, I’ve yet to come across an article which correctly explains something as simple as the integration of OpenCV Android with Android Studio systematically, for newbies. Hence the motive behind compiling such an article. The OpenCV library 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, especially during a hackathon!

P.S. The following steps will help you in integrating the OpenCV library solely, so don’t expect a preprocessed image to pop up at your ImageView magically. We’ll keep that for another day :)

Getting Started: The Pre-requisites

Irrespective of the OS that you’re using, you will require the following:

  1. If you haven’t installed Java yet, go ahead — install it from the Oracle website. Confirm your installation; java –version does the trick!

Setup and Integration: OpenCV for Android

Step 1: Download the OpenCV SDK for Android. 2.4.13 should work fine. Make note of the destination folder- OpenCV-android-sdk (you will be needing this later).

The OpenCV-android Download Page

Step 2: Create a new project. It’s always preferable to select higher API versions as the Minimum SDK, but since my spare phone is old, I’m going to stick with API 21: Android (5.0) Lollipop.

Step 3: Import the OpenCV Module. Go to File -> New -> Import Module. This is where your destination folder comes into play. My OpenCV-android-sdk folder is in Downloads, 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.

The Import Module Option
Selecting the sdk/java Folder
The Source Directory

Step4: A Gradle project sync error! Now what? If things go berserk at this step, it’s completely NORMAL because the Gradle script of your app and of OpenCV do not match! Let’s get that right, before proceeding.

The ‘OMG What Did I Just Do’ Scenario

Now here’s the confusing bit- there are so many .gradle files, which one should you be choosing?Select Project from the dropdown at the top of the Project window (this will help you in viewing your project structure).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.

build.gradle files of the App and the OpenCVLibrary

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.

The Open Module Settings Option
Selecting the OpenCVLibrary Module

Just a few more steps and you’re almost good to go!

Step 6: Remember how I’d mentioned about keeping the OpenCV-android-sdk destination path in mind? Here’s where you’ll need it yet again. The libs folder present in OpenCV-android-sdk/sdk/native has to be COPIED to the app/src/main folder of your Android project.

Step 7: Don’t close that window yet! RENAME the copied libs folder to jniLibs. Build your app- it should work fine!

Step 8: Running the App on your Android Device.You must be wondering how OpenCV applications work on an Android phone.OpenCV Manager beautifully handles it all! I don’t recommend testing the app via an emulator. Use an actual Android device in such a scenario. You CANNOT run it on your phone without this.

The OpenCV Manager available at the Google Play Store

If your app runs without crashing and you have no errors, give yourself a huge pat.Viola!(This is my first attempt at writing a technical article for Medium so any suggestion would be appreciated :))