Android Studio NDK设置正确,但不兼容

huangapple go评论87阅读模式
英文:

Android Studio NDK being incompatible though having been set properly

问题

以下是您提供的代码的翻译部分:

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(
             native-lib
             SHARED
             native-lib.cpp )

find_library(
              log-lib
              log )

target_link_libraries(
                       native-lib
                       ${log-lib}
                       lib_opencv)

native-lib.cpp 文件:

#include <jni.h>

build.gradle(app) 文件:

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

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

感谢您的支持!您提供的代码中包含了与在 Android Studio 中集成 OpenCV 以及处理 NDK 问题相关的内容。如有更多问题,请随时问我。

英文:

I am trying to integrate OpenCv with android studio and in the process NDK issues are being faced by me .
The NDK always remains unselected in the Project Structure.The header files always remain unavailable because the path get unmentioned . I am trying to resolve this issue since days but not able to find the solution of the above if someone would help.
The NDK does not connect , though having been tried many ways to integrate NDK.
I had used all possible ways of integrating the NDK and have taken possible measure to replicate my needs but nothing works.
I have seen how others tried to resolve the issue but none of those worked for me.

Here below is my code
CMakeLists.txt file

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -std=gnu++11&quot;)

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)

native-lib.cpp

#include &lt;jni.h&gt;

Here above the jni.h file is linted to be not found and hence the JNIEXPORT and JNICALL also remains hidden.

build.gradle(app)

apply plugin: &#39;com.android.application&#39;

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId &quot;com.karuneshpalekar.copencv&quot;
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName &quot;1.0&quot;

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

        ndk {
            moduleName &quot;test&quot;
            cFlags &quot;-std=c++11 -fexceptions&quot;
            stl &quot;gnustl_shared&quot;
        }


    }

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

    packagingOptions {
        pickFirst &#39;lib/armeabi-v7a/libopencv_java4.so&#39;
        pickFirst &#39;lib/arm64-v8a/libopencv_java4.so&#39;
        pickFirst &#39;lib/x86/libopencv_java4.so&#39;
        pickFirst &#39;lib/x86_64/libopencv_java4.so&#39;
    }


}

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

}

build.gradle(java)

apply plugin: &#39;com.android.library&#39;

println &quot;OpenCV: &quot; + project.buildscript.sourceFile

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

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
    }

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

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

dependencies {
}

Thank-You for helping
I have Ndk-rc21 and Android Studio AGP Version 4.0.1 and Gradle Versopion 6.1.1

答案1

得分: 3

## 如何在 NDK(v19) 中使用 OpenCV SDK(v4.4)##

创建一个带有 `app` 模块的 Android Studio 项目。

`settings.gradle`

```groovy
include ':app'

build.gradle

// 顶层构建文件,您可以在此处添加所有子项目/模块共有的配置选项。
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"

        // 注意:不要在此处放置应用程序的依赖项;它们应位于各个模块的 build.gradle 文件中
    }
}

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

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

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 的头文件和库。因此,从以下地址下载并解压 OpenCV SDK(v4.4)的 Android 包:https://opencv.org/releases/

按照以下方式将 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} )

# 添加 OpenCV 库

# ...(省略其他库的添加)

# 添加第三方库

# ...(省略其他库的添加)

在主 CMake 构建脚本中包含 opencv.cmake 文件,设置 OpenCV 的包含目录并链接库。

app/src/main/cpp/CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

include( opencv/opencv.cmake )

# ...(查找和链接其他库)

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;

<details>
<summary>英文:</summary>

## How to use OpenCV SDK (v4.4) with NDK (v19) ##

Create an Android Studio project with an `app` module.

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


Set Android SDK and NDK (v19) paths in `local.properties` file.

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


Specify the path to the main cmake build script file, cmake version to use, and Android ABIs to compile in `app/build.gradle` file.

apply plugin: 'com.android.application'

android {
compileSdkVersion 29

defaultConfig {
    minSdkVersion 23
    targetSdkVersion 29
    versionCode 1
    versionName &quot;1.0&quot;

    ndk {
        abiFilters &quot;arm64-v8a&quot;, &quot;armeabi-v7a&quot;
    }
}

externalNativeBuild {
    cmake {
        path &quot;src/main/cpp/CMakeLists.txt&quot;
        version &quot;3.10.2&quot;
    }
}

}

dependencies {
...
}


Now we need header files and libraries from OpenCV Android SDK. So, download and extract OpenCV SDK (v4.4) package for Android from : https://opencv.org/releases/

Copy OpenCV header files and libraries from extracted package to the Android Studio project as follows.

1. Copy `OpenCV-android-sdk/sdk/native/staticlibs/*` to `app/src/main/cpp/opencv/staticlibs/`

2. Copy `OpenCV-android-sdk/sdk/native/3rdparty/*` to `app/src/main/cpp/opencv/3rdparty/`

3. Copy `OpenCV-android-sdk/sdk/native/jni/include/*` to `app/src/main/cpp/opencv/include/`

Create following cmake file in the project.

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

```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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${OPENCV_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
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 &quot;${THIRD_PARTY_LIBS_DIR}/lib${lib_name}.a&quot; )
list( APPEND THIRD_PARTY_LIBS ${lib_name} )

Include opencv.cmake file in the main cmake build script, set OpenCV include directory, and link libraries as follows.

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

Now you can use OpenCV APIs in your C++ source file.

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

#include &lt;jni.h&gt;
#include &lt;opencv2/core.hpp&gt;

using namespace cv;

Mat matImage;

答案2

得分: 1

我遇到的问题与NDK工具无关,而是与我放置它的目录有关。

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

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

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

根据Lakindu上面的答案,无需降级NDK库,只需确保NDK位于正确的位置。

还要别忘了在项目结构中添加NDK工具,你可以通过同时点击Ctrl + Shift + Alt + S来找到它。

英文:

The Issue I had was not with the NDK tool , it was with the directory where i had placed it.

There was space between file name which resulted in the error eg"User space Name".eg:C:/User/Elon Musk/SDK/ndk

I rectified this mistake of mine after a warning display by Android Studio lint .

I changed the Android SDK directory placement and rerun the code and the issue got resolved.

As per the answer above by Lakindu , there is no need for downgrading the Ndk library just make sure that the NDK is in the right place .

And also dont forget to add the NDK tool in the Project Structure which you can find by clicking Ctrl + Shift +Alt + S simultaneously.

huangapple
  • 本文由 发表于 2020年8月30日 00:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63649479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定