?? Adding OpenCV to Your Kotlin Android Project: A Comprehensive Step-by-Step Guide ??
Ahmet Bostanciklioglu
Android Kotlin Developer | Jetpack Compose | iOS Swift | KMM | CMP
Step-by-Step Guide to Adding OpenCV Library to Kotlin:
Firstly, we search for "release opencv" on Google. Then we click on the GitHub Release opencv/opencv link:
Secondly, when we enter GitHub, we choose the latest version and then click on the opencv-4.10.0-android-sdk.zip link to download the OpenCV SDK for Android as a zip file:
Thirdly, after downloading, we open the zip file. We return to Android Studio to add the OpenCV library. Here, follow the steps to add the library and configure Android Studio.
In Android Studio, we click on 'File > New > Import Module ' .
In the "Import Module from Source" screen, click the file browsing button to choose the downloaded OpenCV library:
We select the opencv file, then sdk, and click open. Here is an image showing the selected file:
In Android Studio, import the module from the source. We select the downloaded OpenCV and give a name to the library:
We switch from the Android directory to the Project directory. The project should look like this:
As you can see, the OpenCV library is added to the project, but there is an error because the OpenCV library adding process is not completed yet. To fix the error, we continue configuring the project:
Firstly, open settings.gradle:
Cut (Command + X or Ctrl + X) the code from the project and delete the settings.gradle file. Right-click on the settings file, then Settings.gradle > Open In > Finder, and delete settings.gradle in the project file:
Next, add include(":opencv") to settings.gradle.kts , then Sync:
After we click File > Project Structure in Android Studio. Select the Module part to check if the Compile Sdk Version and Build Tool Version are the same in the OpenCV library and app module:
and
When the dependencies are similar, apply and click OK.
领英推荐
In build.gradle.kts(:app) , add OpenCV as implementation to dependencies:
After that, Sync.
Now, we apply OpenCV to MainActivity. We check if there is an OpenCV library in the project with this code:
When we run the project on a real device, we get this error:
The error text form is that:
Execution failed for task ':opencv:compileDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain
The solution for this error is to change sourceCompatibility and targetCompatibility to JJavaVersion.VERSION_17 and change jvmTarget to "17" in build.gradle.kts(Module: app). Here's the changed code snippet:
// build.gradle.kts(Module: app)
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
// the rest...
}
And apply similar changes in build.gradle(:opencv) :
// build.gradle(:opencv)
android {
// The rest...
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
After that, Sync the project. Then run the project.
Here's the screenshot of the project running on a device:
Finally, to add the OpenCV library to a Kotlin project in Android Studio, you need to download the SDK from GitHub, import the module into your project, adjust the settings, and ensure compatibility between Java versions. After setting up and syncing, you can integrate OpenCV functionality into your application. Finally, resolving any build errors by aligning Java and Kotlin compatibility settings ensures smooth execution.
The help resources:
Source Code Link: https://github.com/ahmetbostanciklioglu/Open_CV_Template.git
Happy Coding. ??