简体   繁体   English

获得支持NDK的OpenCV可在Android Studio中使用

[英]Getting OpenCV with NDK support to work in Android Studio

I'm very new to openCV and android programming and I want to use openCV in my project. 我是openCV和android编程的新手,我想在我的项目中使用openCV。 For now I'm trying to run openCV's 2nd tutorial in my android studio but I always get the following NDK error: 现在我正试图在我的android工作室中运行openCV的第二个教程,但我总是得到以下NDK错误:

Error:Execution failed for task ':openCVTutorial2MixedProcessing:compileDebugNdk'.

NDK not configured. NDK未配置。 Download the NDK from http://developer.android.com/tools/sdk/ndk/.Then add ndk.dir=path/to/ndk in local.properties. http://developer.android.com/tools/sdk/ndk/下载NDK。然后在local.properties中添加ndk.dir = path /到/ ndk。 (On Windows, make sure you escape backslashes, eg C:\\ndk rather than C:\\ndk) (在Windows上,请确保转义反斜杠,例如C:\\ ndk而不是C:\\ ndk)

Then I looked at internet and some guys suggested that I should add these to my gradle.build file: 然后我看了互联网,有些人建议我将这些添加到我的gradle.build文件中:

    jniLibs.srcDirs = ['native-libs']
    jni.srcDirs = [] //disable automatic ndk-build

after adding these it works but I get the following error: 添加这些后它工作,但我收到以下错误:

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/org.opencv.samples.tutorial2-2/base.apk"],nativeLibraryDirectories=[/data/app/org.opencv.samples.tutorial2-2/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libopencv_java3.so"

This is my gradle file: 这是我的gradle文件:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "org.opencv.samples.tutorial2"
        minSdkVersion 21
        targetSdkVersion 23

        ndk {
            moduleName "mixed_sample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    sourceSets{main {jni.srcDirs = ['src/main/jni','src/main/jniLibs']
        jniLibs.srcDirs = ['native-libs']
        jni.srcDirs = [] //disable automatic ndk-build
    }}
}

dependencies {
    compile project(':openCVLibrary310')
}

Well, that code you pasted in does exactly what the comment says: 那么,你粘贴的代码完全符合评论的内容:

jni.srcDirs = [] //disable automatic ndk-build

You don't see that first error is because the entire compileDebugNdk step is skipped. 您没有看到第一个错误是因为跳过了整个compileDebugNdk步骤。 This means the NDK side of your application isn't built, which means none of the OpenCV libraries are in the APK, which means Java isn't able to load them, hence couldn't find "libopencv_java3.so" . 这意味着您的应用程序的NDK端未构建,这意味着APK中没有OpenCV库,这意味着Java无法加载它们,因此couldn't find "libopencv_java3.so"

You need to write a bit of logic to build the C++ side of your project. 您需要编写一些逻辑来构建项目的C ++端。 Currently there are a couple of ways to do this, the easiest of which is to reference the NDK samples which document how to build and use native code with only a build.gradle. 目前有两种方法可以做到这一点,其中最简单的方法是引用NDK示例 ,该示例记录了如何仅使用build.gradle构建和使用本机代码。 The build.gradle file from hello-libs is a good place to start for a project with 3rd-party dependencies. 来自hello-libsbuild.gradle文件是启动具有第三方依赖项的项目的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM