Android Studio NDK 不兼容,尽管已正确设置

Android Studio NDK being incompatible though having been set properly

我正在尝试将 OpenCv 与 android studio 集成,在此过程中我遇到了 NDK 问题。 NDK 在项目中始终保持未选中状态 Structure.The 头文件始终保持不可用,因为未提及路径。几天以来我一直在努力解决这个问题,但如果有人愿意提供帮助,我就无法找到上述解决方案。 NDK 不连接,虽然已经尝试了很多方法来集成 NDK。 我已经使用了所有可能的方式来集成 NDK,并采取了可能的措施来复制我的需求,但没有任何效果。 我已经看到其他人如何尝试解决问题,但 none 对我有用。

下面是我的代码 CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

include_directories(${OpenCV_DIR}/jni/include)

add_library( lib_opencv SHARED IMPORTED )

set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${OpenCV_DIR}/libs/${ANDROID_ABI}/libopencv_java4.so)

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       native-lib

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

                       lib_opencv)

原生-lib.cpp

#include <jni.h>

上面的 jni.h 文件被检测为未找到,因此 JNIEXPORT 和 JNICALL 也保持隐藏状态。

build.gradle(应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.karuneshpalekar.copencv"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
                abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
                arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native"
            }
        }

        ndk {
            moduleName "test"
            cFlags "-std=c++11 -fexceptions"
            stl "gnustl_shared"
        }


    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }

    packagingOptions {
        pickFirst 'lib/armeabi-v7a/libopencv_java4.so'
        pickFirst 'lib/arm64-v8a/libopencv_java4.so'
        pickFirst 'lib/x86/libopencv_java4.so'
        pickFirst 'lib/x86_64/libopencv_java4.so'
    }


}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
    implementation project(path: ':java')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

build.gradle(java)

apply plugin: 'com.android.library'

println "OpenCV: " + project.buildscript.sourceFile

android {
    compileSdkVersion 29
    //buildToolsVersion "x.y.z" // not needed since com.android.tools.build:gradle:3.0.0

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/jniLibs']
            java.srcDirs = ['src/main/java']
            aidl.srcDirs = ['src']
            res.srcDirs = ['/build/master_pack-android/opencv/modules/java/android_sdk/android_gradle_lib/res']
            manifest.srcFile 'java/AndroidManifest.xml'
        }
    }
}

dependencies {
}

感谢您的帮助 我有 Ndk-rc21 和 Android Studio AGP 版本 4.0.1 和 Gradle Versopion 6.1.1

如何将 OpenCV SDK (v4.4) 与 NDK (v19) 一起使用

使用 app 模块创建一个 Android Studio 项目。

settings.gradle

include ':app'

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.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
}

local.properties 文件中设置 Android SDK 和 NDK (v19) 路径。

sdk.dir=/home/USER/Android/Sdk
ndk.dir=/home/USER/Android/Sdk/ndk/android-ndk-r19c

指定主 cmake 构建脚本文件的路径、要使用的 cmake 版本和 Android 要在 app/build.gradle 文件中编译的 ABI。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        ndk {
            abiFilters "arm64-v8a", "armeabi-v7a"
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}

dependencies {
    ...
}

现在我们需要来自 OpenCV Android SDK 的头文件和库。因此,从 https://opencv.org/releases/

下载并提取 Android 的 OpenCV SDK (v4.4) 包

将提取包中的 OpenCV 头文件和库复制到 Android Studio 项目,如下所示。

  1. 复制OpenCV-android-sdk/sdk/native/staticlibs/*app/src/main/cpp/opencv/staticlibs/

  2. 复制OpenCV-android-sdk/sdk/native/3rdparty/*app/src/main/cpp/opencv/3rdparty/

  3. 复制OpenCV-android-sdk/sdk/native/jni/include/*app/src/main/cpp/opencv/include/

在项目中创建如下cmake文件。

app/src/main/cpp/opencv/opencv.cmake

set( CURRENT_DIR ${CMAKE_CURRENT_LIST_DIR} )
set( OPENCV_INC_DIR ${CURRENT_DIR}/include )
set( OPENCV_LIBS_DIR ${CURRENT_DIR}/staticlibs/${ANDROID_ABI} )
set( THIRD_PARTY_LIBS_DIR ${CURRENT_DIR}/3rdparty/libs/${ANDROID_ABI} )

# Add opencv libs

set( lib_name opencv_calib3d )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_dnn )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_features2d )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_flann )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_gapi )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_highgui )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_imgcodecs )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_imgproc )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_ml )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_objdetect )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_photo )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_stitching )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_video )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_videoio )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

set( lib_name opencv_core )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${OPENCV_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND OPENCV_LIBS ${lib_name} )

# Add 3rd-party libs

set( lib_name ade )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name cpufeatures )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name IlmImf )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name ittnotify )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name libjasper )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name libjpeg-turbo )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name libpng )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name libprotobuf )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name libtiff )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name libwebp )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name quirc )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name tbb )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

set( lib_name tegra_hal )
add_library( ${lib_name} STATIC IMPORTED )
set_target_properties( ${lib_name} PROPERTIES IMPORTED_LOCATION "${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a" )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

在主 cmake 构建脚本中包含 opencv.cmake 文件,设置 OpenCV 包含目录和 link 库如下。

app/src/main/cpp/CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

include( opencv/opencv.cmake )

find_library( log-lib log )
find_library( android-lib android )
find_library( jnigraphics-lib jnigraphics )
find_library( z-lib z )

add_library( native-lib SHARED
    native-lib.cpp
)

target_include_directories( native-lib PRIVATE
    ${OPENCV_INC_DIR}
)

target_link_libraries( native-lib
    ${log-lib}
    ${android-lib}
    ${jnigraphics-lib}
    ${OPENCV_LIBS}
    ${THIRD_PARTY_LIBS}
    ${z-lib}
)

现在您可以在 C++ 源文件中使用 OpenCV API。

app/src/main/cpp/native-lib.cpp

#include <jni.h>
#include <opencv2/core.hpp>

using namespace cv;

Mat matImage;

我遇到的问题不是 NDK 工具,而是我放置它的目录。

文件名之间有 space,导致错误,例如“用户 space 名称”。例如:C:/User/Elon Musk/SDK/ndk

我在 Android Studio lint 显示警告后纠正了我的这个错误。

我更改了 Android SDK 目录位置并重新运行代码,问题得到解决。

根据 Lakindu 的上述回答,无需降级 Ndk 库,只需确保 NDK 位于正确的位置即可。

同时不要忘记在项目结构中添加 NDK 工具,您可以同时单击 Ctrl + Shift +Alt + S 找到该工具。