A Beginner’s Guide to Setting up OpenCV Android Library on Android Studio

Elvis Chidera
Published in
6 min readAug 13, 2018


I recently started a project that involved working with OpenCV on Android. Most of the guides on setting up the library on Android were outdated or not complete. So, after getting multiple requests from team mates on how to set this up, I decided to just write a dead simple guide on this.

Image Detection (Source)

Step 1: Download OpenCV Android Library

Go to the OpenCV Android Sourceforge page and download the latest OpenCV Android library. As at the time of writing this post, the latest available version was 3.4.1.

When the download completes, you should extract the contents of the zip file into a folder.

Step 2: Setup project

Create a new Android project using Android Studio only if you have not created one already for your computer vision project.

Note: Skip this step if you already have an Android project you want to use the OpenCV library in.

Step 3: Import OpenCV Module

After successfully creating an Android project, it is time to import the OpenCV module into your Android project. Click on File -> New -> Import Module…

It should bring up a popup like the image below where you can select the path to the module you want to import.

Browse to the folder where you extracted the OpenCV Android library zip file contents. Select the java folder inside of the sdk folder.

After selecting the correct path and clicking OK, you should get a screen like the image below.

Click on Next to go to the next screen. On the next screen (the image below) you should leave the default options checked and click on Finish to complete the module import.

Step 4: Fixing Gradle Sync Errors

You should get a Gradle build error after you finish importing the OpenCV library. This happens because the library is using an old Android SDK that you probably don’t have installed yet.

To quickly fix this error, switch from the Android pane to the Project pane on the left side of Android Studio.

Browse to OpenCV library module and open its build.gradle file.

To fix the error, you just have to change the compileSdkVersion and targetSdkVersion to the latest Android SDK version or the one you have installed on your PC. After changing the version you should click on the sync button so that Gradle can sync the project.

Quick tip: buildToolsVersion can be ignored

Step 5: Add the OpenCV Dependency

To work with the OpenCV Android library, you have to add it to your app module as a dependency. To easily do this on Android Studio, click on File -> Project Structure.

When the project structure dialog opens, click on the app module or any other module that you want to use OpenCV library in.

After navigating to the module, click on the Dependencies tab. You should see a green plus button on the far right of the dialog, click on it and select Module dependency.

When the choose modules dialog opens, select the OpenCV library module and click on OK.

When you return to the dependencies page, confirm that the module was actually added as a dependency then click on the OK button to continue.

Step 6: Add Native Libraries

On your file explorer, navigate to the folder where you extracted the content of the OpenCV Android library zip file. Open the sdk folder and then the native folder (Use the image below as a guide).

Copy the libs folder in the native folder over to your project app module main folder (Usually ProjectName/app/src/main).

Rename the libs folder you just copied into your project to jniLibs.

Step 7: Add Required Permissions

To successfully use OpenCV, your app should have the camera permission added to its AndroidManifest.xml file.

Tip: Don’t forget to request for the camera permission at runtime on Android 6 and above.

Step 8: Try out Sample

To confirm that you successfully integrated the OpenCV Android library, you can try out one of the samples included in the library zip file.

Let’s try out the color-blob-detection sample. You can see the sample in action below:

Quickly update your app main activity with the code below.

Then create a new class called ColorBlobDetector and copy the code below into it.

Finally, update your app main activity layout file with the layout code below.

Step 9: Using OpenCV Manager

Because you are bundling native libraries in your app APK, you will end up with a very large app APK size.

One way to solve this is to use ABI splits, so you only have the neccessary libraries needed for each device in an APK.

The other way around the size issue is to use the OpenCV Manager. In the folder where you extracted the library contents into, there is a folder called apk that contains the OpenCV manager for various architectures.

Select the APK with the right architecture of your test device and use ADB via command prompt/line to install it on your device.

Step 10: Test the App

Finally, hit the run button and run the app on your test device.

Note: Don’t forget to grant camera permission on Android 6 and above.

That is all.

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