OpenCV for Android Studio,ndkbuild失败

时间:2016-04-11 13:53:52

标签: android opencv android-ndk

我遵循了本教程https://www.youtube.com/watch?v=JIHfqzTdOcQ,在最后阶段我无法编译项目。我的ndk有问题,但我不知道该怎么做。我尝试了一切,例如:

NDKBuild Failure

经过多次改动后,我现在有点困惑。

以下是build.gradle的代码

import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
defaultConfig {
    applicationId "com.example.aidan.opencvdemo"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = [] //disable automatic ndk-build call
}

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
    } else {
        commandLine 'ndk-build', '-C', file('src/main').absolutePath
    }
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile project(':openCVLibrary310')
}

这是消息gradle build

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:openCVLibrary310:compileLint
:openCVLibrary310:copyReleaseLint UP-TO-DATE
:openCVLibrary310:mergeReleaseProguardFiles UP-TO-DATE
:openCVLibrary310:preBuild UP-TO-DATE
:openCVLibrary310:preReleaseBuild UP-TO-DATE
:openCVLibrary310:checkReleaseManifest
:openCVLibrary310:prepareReleaseDependencies
:openCVLibrary310:compileReleaseAidl UP-TO-DATE
:openCVLibrary310:compileReleaseRenderscript UP-TO-DATE
:openCVLibrary310:generateReleaseBuildConfig UP-TO-DATE
:openCVLibrary310:generateReleaseAssets UP-TO-DATE
:openCVLibrary310:mergeReleaseAssets UP-TO-DATE
:openCVLibrary310:generateReleaseResValues UP-TO-DATE
:openCVLibrary310:generateReleaseResources UP-TO-DATE
:openCVLibrary310:packageReleaseResources UP-TO-DATE
:openCVLibrary310:processReleaseManifest UP-TO-DATE
:openCVLibrary310:processReleaseResources UP-TO-DATE
:openCVLibrary310:generateReleaseSources UP-TO-DATE
:openCVLibrary310:compileReleaseJava UP-TO-DATE
:openCVLibrary310:processReleaseJavaRes UP-TO-DATE
:openCVLibrary310:packageReleaseJar UP-TO-DATE
:openCVLibrary310:compileReleaseNdk UP-TO-DATE
:openCVLibrary310:packageReleaseJniLibs UP-TO-DATE
:openCVLibrary310:packageReleaseLocalJar UP-TO-DATE
:openCVLibrary310:packageReleaseRenderscript UP-TO-DATE
:openCVLibrary310:bundleRelease UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72221Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42221Library UP-TO-DATE
:app:prepareOpenCVdemoOpenCVLibrary310UnspecifiedLibrary UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:ndkBuild FAILED
Error:Execution failed for task ':app:ndkBuild'.
> A problem occurred starting process 'command 'ndk-build.cmd''
Information:BUILD FAILED
Information:Total time: 1.673 secs
Information:1 error
Information:0 warnings
Information:See complete output in console

这是我的项目结构

http://oi68.tinypic.com/152eu86.jpg

1 个答案:

答案 0 :(得分:0)

根据Gradle docs for Exec tasks,我猜您的Windows上的构建脚本存在两个问题:

  1. 与Linux不同,Windows并不认为shell脚本是可执行文件。没有#!在Windows上。您需要将cmd.exe指定为可执行文件,并将ndk-build.cmd指定为第一个参数。
  2. Windows喜欢查看/之前的选项,而不是Linux使用的选项。所以/ c而不是-c。
  3. 您可能还想明确设置workingDir。它默认为项目根目录,可能是您想要的,也可能不是。