OpenCV in Android Studio project

published in Android Development, Tutorials
by Slawomir Onyszko

Are you building an Android app to detect if a photo is blurry or overexposed? Then you should definitely try OpenCV: the library, which provides extensive solutions for analysing and processing photographs. If you’re interested in knowing how to add OpenCV to your project in Android Studio, keep on reading.

Including the library files in Android application project

You don’t have to be an advanced Android developer to use OpenCV in your Android app. First of all, download the OpenCV SDK and add the library to the project you’re working on. This is how you do it: the Android library can be downloaded from the organization’s main website

The OpenCV package contains three directories:

  • apk (build directories of the OpenCV application manager)
  • samples (compiled app examples along with their source code)
  • sdk (the library’s source code)

The sdk directory is split into two parts: the Android module (inside the java folder) and the native, compiled code located in the native/libs folder.

OpenCV library configuration

After getting initially acquainted, you can move on to the most important step: integrating OpenCV with Android Studio.

First of all, add the OpenCV native code to your project inside Android Studio. For starters, let’s change the directory display mode to Project. Adding libraries will become a bit easier thanks to that.

The OpenCV library configuration

Change the directory display mode to Project

Now create a new folder in your app’s main directory and name it jniLibs: go to app/src/main and right click on in. From the context menu select: new > Directory. In the dialog box type in jniLibs and confirm. Now copy the native/libs folder content and paste it into the one you have just created in the project.

android app building

Including the OpenCV module in your project’s file structure

In order for the native code to work, we need to include the OpenCV module itself in the project. As mentioned earlier, the OpenCV module is located in sdk/java. We have to import the entire java directory: go to File > New > Import Module. In the dialogue box enter the path to the module and its name.

Next, change the default name to OpenCV3.1 and press OK. Now you should see an error message about a missing Android API in our sdk.

android app building

Without additional configuration, an error will pop up

To fix it, you have to properly configure the module, which means making some changes in just two files. First, go to the OpenCV3.1 module folder (next to src), open the AndroidManifest.xml file and then the build.gradle file.

Now, in the AndroidManifest.xml file, you have to delete the uses-sdk tag and the properties: android:versionCode, android:versionName. The property values have to be transferred to the build.gradle file.

Next in the build.gradle file, set the range of  the supported Android API SDK so that it corresponds to project’s settings and add the versionCode plus the versionName, that were in the AndroidManifest.xml file.

After these changes you have to synchronize the gradle files: click the Sync Project with Gradle Files icon located in the menu bar.

Android application

Syncing your project with Gradle files

If no errors pop up, then the OpenCV library in your Android application is configured correctly.

Almost there!

Now what we need to do is add the dependency in the project’s settings. From the Android Studio menu pick: New > Project Structure. In the dialog box move on to the Dependencies bookmark, there on the left select app and then click the green plus button on the right. Choose Module dependency and pick OpenCV in the dialog box.

advanced android development

Adding OpenCV to your project dependencies

The last step is adding the library loading code. The code should be joined to the launcher activity of our application.

 static {
        if (OpenCVLoader.initDebug()) {
            Log.i(TAG, "OpenCV initialize success");
        } else {
            Log.i(TAG, "OpenCV initialize failed");

It’s testing time: start the Android application on your smartphone. If your logcat shows something similar to the screenshot below, that means that you created the application correctly. Congrats!

Android application

The OpenCV library configuration

For more interesting content follow our blog and tutorials! Next up: few examples of the OpenCV library usage.

A code sample that we’ve used in a commercial Android application can be found on our GitHub page.


Slawomir Onyszko Mobile Developer

Sławek is our mobile developer who mostly takes care of creating Android applications. He is constantly enhancing his skills of advanced Android app development and he wants to share this knowledge via Zaven’s blog. In his spare time Sławek enjoys watching a good movie at the cinema.

Popular posts

Clutch Announces Zaven as Top Polish Development in  2019 Eastern European Leaders Award

Clutch Announces Zaven as Top Polish Development in 2019 Eastern European Leaders Award

Since 2011, Zaven has been providing digital solutions for global clients. Our team is composed of experts in custom development, UX/UI design, data analytics, backend integration and more. We aim to build long-term business relationships with our clients in order to create the highest quality and most innovative solutions. Clutch, a B2B market research platform, […]

Read more
How to overcame the fear of hiring outsourced developers?

How to overcame the fear of hiring outsourced developers?

There is many a legend about outsourcing which successfully keeps away entrepreneurs. Stories about software which never even existed, despite the fact that the developer had promised that everything was fine - even ahead of deadline. No communication with the developer for a long, long time and then it turns out that nobody has been working because it is Chinese New Year! These are telling examples and they are not isolated. I can only imagine the fear that grips some people when the subject of outsourcing steps out of the shadows! Let’s just deal with the most common fears of hiring outsourced developers.

Read more
Why we moved to Kotlin for Android development (and maybe you should too)

Why we moved to Kotlin for Android development (and maybe you should too)

Recently Kotlin programming is really endorsed by Google and most of the developers. We are not an exception. We moved to Kotlin for Android development and it was worth it. Here are 8 reasons why we did it.

Read more
Mobile Apps

Get your mobile app in 3 easy steps!


Spec out

with the help of our
business analyst



design, implement
and test, repeat!



get your app out
to the stores

back to top