rdxdkr
rdxdkr

Reputation: 1189

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.

Upvotes: 16

Views: 11053

Answers (3)

rdxdkr
rdxdkr

Reputation: 1189

This is the complete procedure that currently works for me with OpenCV 4.5.2 on Android Studio 4.1.3.

  1. In your project click on File > New > Import Module... and select the /sdk directory 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 /app directory resides;
  2. open the Project Structure (for example by clicking on File > Project Structure...), then go to Dependencies (on the left), click on app and on the + icon in the Declared Dependencies tab (not the one in the Modules tab);
  3. click on Module Dependency and 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 on Apply and OK to exit from the Project Structure;
  4. open the build.gradle file of your app module, copy the values of compileSdkVersion, minSdkVersion and targetSdkVersion, then paste them in the build.gradle file of the OpenCV module replacing the default ones so they match exactly. You can also update the sourceCompatibility and targetCompatibility fields to JavaVersion.VERSION_1_8;
  5. 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")
}

Edit: There's a minor addition with 4.8.0 OpenCV version. Open the app level build.gradle file for org.opencv and add this block at the end of the android block:

buildFeatures {
    aidl = true
    buildConfig = true
}

Upvotes: 18

JOE
JOE

Reputation: 373

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})

Upvotes: 0

Bereket Kassahun
Bereket Kassahun

Reputation: 51

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"
        }
    }

Upvotes: 2

Related Questions