Android Studio + Kotlin + Open CV

Ankit Sachan
3 min readAug 14, 2023

--

so this is if you are getting frustrated with how to integrate OpenCV 4.x.x with Android Studio.

  1. Download Open CV SDK (https://opencv.org/releases/)

2. Unzip the SDK

3. Create an Android project

4. Add OpenCV SDK as a dependency

As soon as you’ll add you’ll see this error

Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
namespace 'com.example.namespace'
}

5. Let's solve this error

open android project as Project:

access the build.gradle file of opencv

add namespace ‘org.opencv’ under android

sync the gradle by tapping on Try again

you should see something like this

6. Let's add opencv as a dependency to the main app

open project structure

add ‘opencv’ as a dependency to the main app

7. Run the app in debug mode you should see this error:

error: package org.opencv.engine does not exist
import org.opencv.engine.OpenCVEngineInterface;
^

to fix this we need to modify the build feature to support aidl

open build.gradle of ‘opencv’ and add

buildFeatures {
aidl true
}

8. After this you should get this error

import org.opencv.BuildConfig;

Fix:

9. Let's check if OpenCV is successfully added to the project or not

Add these lines to MainActivity.kt

You should see this output.

Hope this helps you.

Happy Coding.

Satya Mallick: Please get the documentation updated

Acknowledgements:

https://github.com/opencv/opencv/pull/23447

--

--