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

Effortless deployment of Azure CosmosDB JS functions with GitLab-CI

Effortless deployment of Azure CosmosDB JS functions with GitLab-CI

Working with CosmosDB JavaScript triggers and stored procedures can be problematic, especially when your application consists of three or four separate development environments. The solution we need is found in the effortless deployment of Azure CosmosDB JS functions with GitLab-CI.

Read more
Interviews with startup founders: Fonn Byggemappen

Interviews with startup founders: Fonn Byggemappen

Fonn Byggemappen is an iOS, Android and web application for construction industry projects. The tool’s co-founder Jan Tore Grindheim told us about the beginnings and crucial moments of the app.

Read more
Core ML: Machine Learning for iOS

Core ML: Machine Learning for iOS

You have probably heard phrases like “Machine Learning” or “Artificial Intelligence” without knowing exactly what they mean. In this article, I will shed some light on “What Machine Learning is” by showing you a Core ML iOS sample.

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

Contact us

Hire us now!

back to top