'How to import OpenCV 4.5 in Android Studio
The fact that this question is still occasionally getting new upvotes makes me think I'm not the only one in this kind of situation, so I've decided to write a step by step explanation of what worked for me in hope that others might find it useful in the future. Everything is based on this video, but I think it's better to write a proper guide here in case it gets taken down.
If you're looking for help with OpenCV 3.4, this is what I was following before trying version 4.5.
Solution 1:[1]
This is the complete procedure that currently works for me with OpenCV 4.5.2 on Android Studio 4.1.3.
- In your project click on
File > New > Import Module...and select the/sdkdirectory inside your OpenCV download. Give it a meaningful name and wait for the procedure to finish: the directory you selected should have been copied in the root of your project where the default/appdirectory resides; - open the Project Structure (for example by clicking on
File > Project Structure...), then go toDependencies(on the left), click onappand on the+icon in theDeclared Dependenciestab (not the one in theModulestab); - click on
Module Dependencyand select the checkbox for the OpenCV SDK that you imported earlier. You should now see it in the list with the other dependencies, so click onApplyandOKto exit from the Project Structure; - open the
build.gradlefile of your app module, copy the values ofcompileSdkVersion,minSdkVersionandtargetSdkVersion, then paste them in thebuild.gradlefile of the OpenCV module replacing the default ones so they match exactly. You can also update thesourceCompatibilityandtargetCompatibilityfields toJavaVersion.VERSION_1_8; - finally, sync your project with Gradle files.
To check if it works, add this snippet to your code, for example in MainActivity:
if (OpenCVLoader.initDebug()) {
Log.d("myTag", "OpenCV loaded")
}
Solution 2:[2]
The sdk directory for opencv version 4.5.3 is "opencv/sources/modules/java/android_sdk". After doing each steps on the https://stackoverflow.com/a/65571017/9486652, i got some errors and i solved it by commenting or deleting the 'arguments "-DANDROID_STL=@ANDROID_STL@"' line which is found inside build.gradle of the opencv module.
externalNativeBuild {
cmake {
// arguments "-DANDROID_STL=@ANDROID_STL@"
targets "opencv_jni_shared"
}
}
Solution 3:[3]
I had success to import opencv 4.5.2 in android studio. It is not so difficult. The key is to provide the correct OpenCV_DIR path for CMake to install the OpenCV.
Download SDK https://opencv.org/releases/
Import module by File > New > Import Moduleā¦
Add OpenCV module in setting.gradle
include "opencv"
project(":opencv").projectDir = file("sdk")
In application build.gradle, add OpenCV_DIR cmake argument under android > defaultConfig > externalNativeBuild > cmake
arguments "-DOpenCV_DIR=" + file('../sdk').absolutePath + "/native/jni",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared"
Add OpenCV module dependency in application build.gradle
implementation project(':opencv')
Link OpenCV library in your application cmake.
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
message(STATUS "ANDROID_ABI=${ANDROID_ABI}")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
......
target_link_libraries(${PROJECT_NAME} ${ANDROID_OPENCV_COMPONENTS})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Bereket Kassahun |
| Solution 3 | Martijn Pieters |
