Cannot find opencv2 in Android Studio

本秂侑毒 提交于 2021-02-07 04:26:00

问题


I am trying to write a C++ file in Android Studio and I would like to use OpenCV in my project.

However, when I try to use the following includes, I get an error saying Cannot Find 'opencv2'.

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>

To set up OpenCV in Android, you have to take the following steps, found here, to use the Java part of OpenCV in Android Studio.

To the best of my knowledge to use the C++ part of OpenCV in Android Studio, you have to add Android.mk and Application.mk to the folder containing your native code.

My Android.mk file looks like the following.

LOCAL_PATH := $(call my-dir)

CVROOT := C:/Users/Dan/Documents/Repos/Android-Studio/Assets/opencv-3.4.1-android-sdk/OpenCV-android-sdk/sdk/native/jni

include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(CVROOT)/OpenCV.mk

LOCAL_MODULE    := app
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_LDLIBS +=  -llog -ldl

include $(BUILD_SHARED_LIBRARY)

My Application.mk file looks like the following.

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-27

Am I doing something wrong to set my project up to use the C++ part of OpenCV on Android?


Additional Information for if it makes a difference

My project outline looks like this.

My gradles look like the following

app module

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.test.test.esra"
        minSdkVersion 24
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    // Room - Managing local databases
    implementation 'android.arch.persistence.room:runtime:1.0.0'
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
    //Jetbrains Annotations
    implementation 'org.jetbrains:annotations-java5:15.0'
    implementation project(':openCVLibrary341')
}

openCVLibrary341 module

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 24
        targetSdkVersion 27
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

project build.gradle

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

回答1:


For anyone else who arrives here with the same issue, the problem is fairly easy to fix.

You no longer need the .mk files so they can be deleted.

If you are starting a project from scratch, make sure you have Android NDK and CMake installed as well as enabling them for your project.

After following the initial steps posted in this answer to set up the Java part of OpenCV on Android. You will need to change your app module's build gradle to reflect the snippet below.

defaultConfig {
    ...
    externalNativeBuild {
        cmake {
            cppFlags "-frtti -fexceptions"
            abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
        }
    }
}
sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs']
    }
}

Then you want to edit the file CMakeLists.txt. In this file you want to include the following code underneath the cmake_minimum_required(VERSION X.X.X) line.

Also, you will need to add lib_opencv to the target_link_libraries near the bottom of CMakeLists.txt. This will help prevent undefined reference errors.

include_directories(your-path-to/OpenCV-android-sdk/sdk/native/jni/include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

...

target_link_libraries( # Specifies the target library.
                   native-lib

                   # OpenCV lib
                   lib_opencv

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )

Make sure to replace your-path-to with your actual path to OpenCV for Android.

Finally, clean your project and refresh your linked C++ projects. This will remove the error.

I got this information from this really nice GitHub page. Credits to leadrien


Clarification on You no longer need the .mk files. This is true for Android Studio 3. I have not tested this on older versions of Android Studio or Eclipse, so you might need .mk files for those IDEs.



来源:https://stackoverflow.com/questions/49244078/cannot-find-opencv2-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!