Setting up latest OpenCV for Android Studio and Kotlin — 2021 edition

Photo by Shahadat Rahman on Unsplash

OpenCV is an amazing library used by millions of developers around the world. Sadly the documentation is outdated and sometimes misleading. Here I present a working way to set up the latest (4.5.1 as of today, with 5.0 lurking around the corner) OpenCV to be used in Android Studio (4.1.1) and Kotlin.

Download the latest SDK

Sadly, you find a lot of wrong links and guides telling you to download version 2.4 or similar if you want to use OpenCV. No! Go to the official Sourceforge page (https://sourceforge.net/projects/opencvlibrary/files/) and select the latest version (4.5.1 as of 28 December 2020), inside the folder you find the Android SDK (opencv-4.5.1-android-sdk.zip). Download it. Then extract it somewhere on your harddisk.

Import into Android Studio

Now import the SDK into your Android project. Open Android Studio, load your project and select File->New->Import Module. Then select the sdk directory inside the opencv-sdk directory which you extracted. The sdk directory is the one with the build.gradle inside.

After clicking Finish Android Studio imports the module into your project, this means it copies all relevant data into a subfolder of your project. The subfolder is named sdk, because that’s the name of the folder in the zip archive. As this might be confusing, right click on the module named sdk in your project and select Refactor->Rename and then Rename Module. Name the module opencv.

Next you have to add the module to your build process. In your apps build.gradle file add the following lines and sync your project.

// OpenCV 4.5.1
implementation project(":opencv")

Now you are ready to add OpenCV code!

Add OpenCV functionality

Before you can use OpenCV functionality in your code you have to load the OpenCV libraries at runtime. For this simply add the folowing line for example in the onCreate(…) function of your main activity.

OpenCVLoader.initDebug()

All the tutorials with initAsync(…) are deprecated. Sadly even samples in the OpenCV SDK use the deprecated version.

Next you can finally add OpenCV code to your program. For example this function take a Bitmap and returns the grayscale version.

fun makeGray(bitmap: Bitmap) : Bitmap {

// Create OpenCV mat object and copy content from bitmap
val mat = Mat()
Utils.bitmapToMat(bitmap, mat)

// Convert to grayscale
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY)

// Make a mutable bitmap to copy grayscale image
val grayBitmap = bitmap.copy(bitmap.config, true)
Utils.matToBitmap(mat, grayBitmap)

return grayBitmap
}

Now enjoy OpenCV on Android :-).

Additional information: Seems like OpenCV sometimes causes compilation errors when used with other libraries who also rely on libstc_c. To fix the error just tell gradle to pick the first library. In your app build.gradle add the following part:

android {
packagingOptions {
pickFirst 'lib/arm64-v8a/libc++_shared.so'
pickFirst 'lib/x86/libc++_shared.so'
pickFirst 'lib/x86_64/libc++_shared.so'
pickFirst 'lib/armeabi-v7a/libc++_shared.so'
}
}

Machine learning and neuroscience | Coding python (and recently JS and Kotlin) | Building apps you love