OpenCV in Android Studio project

Android Development, Tutorials

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

Augmented Reality as a trend in Mobile Development

Augmented Reality as a trend in Mobile Development

3D maps, realistic games, virtual showrooms and fitting rooms, interactive participation in a conference or concert, visiting museums and exhibitions. These are just a few examples of where Augmented Reality can be used. Applications created with the help of AR effectively combine the real world with the virtual one. Can your business also benefit from […]

Read more
How to use Natural Language Processing in Mobile App?

How to use Natural Language Processing in Mobile App?

The ability to communicate and interact using language is a remarkable skill. It distinguishes humans from other species and machines. Even the most advanced systems and technologies need specific, defined commands to take action. But what would happen if an application could interpret our language and, importantly, recognize the context of our speech? This is […]

Read more
5 Technology Trends Moving into 2021

5 Technology Trends Moving into 2021

What will technology trends dominate in 2021? We prepared statement of 5 the most popular, latest trends in business e.g. Artificial Intelligence, Agile Methodology. Read our article and know more about 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