android – ‘:app:ndkBuild’的执行失败.进程’命令ndk-build.cmd’以非零退出值2结束

我已经坚持这个问题2天了,并尝试了stackoverflow上给出的所有可能的解决方案.下面是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    sourceSets.main.jni.srcDirs = []
    sourceSets.main.jniLibs.srcDir 'src/main/libs'

defaultConfig {
    applicationId "com.example.anannyauberoi.testingcam"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    ndk {
        moduleName "app"
        cFlags "-std=c++11 -fexceptions"
        ldLibs "log"
        stl "gnustl_shared"
        abiFilter "armeabi-v7a"
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
sourceSets { main { jni.srcDirs = []
    res.srcDirs = ['src/main/res']
    jniLibs.srcDirs=['src/main/libs']
} }
//sourceSets.main.jni.srcDirs = []
// disable automatic ndk-build call, which ignore our Android.mk

task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
    commandLine "C:/Users/Anannya-Uberoi/AppData/Local/Android/sdk/ndk-bundle/ndk-build.cmd",
            'NDK_PROJECT_PATH=build/intermediates/ndk',
            'NDK_LIBS_OUT=src/main/jniLibs',
            'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
            'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

tasks.all { task ->
    if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) {
        task.enabled = false
    }
}

// call regular ndk-build(.cmd) script from app directory

}
    //Modify the below set of code to the ndk-build.cmd location in your computer.

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile project(':openCVLibrary249')
}

我已经尝试了所有可能的解决方案 – 删除build文件夹中的obj文件夹,尝试通过设置sourceSets.main来避免自动Android.mk调用,试图避免调用compileDebugNdk任务.我也没有任何cmake.txt文件.我似乎无法克服这个问题.

我使用过Android Studio 2.3.2和2.1.1,这两个问题都存在.

任何帮助,将不胜感激.

最佳答案 你应该使用最新的Android Studio,2.3.2就行了.它在android gradle插件中集成了externalNativeBuild,因此您不需要使用自定义gradle任务的技巧.

我实际上无法测试下面的build.gradle脚本,所以请原谅我任何错别字:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

defaultConfig {
    applicationId "com.example.anannyauberoi.testingcam"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    externalNativeBuild {
        ndkBuild {
            targets "app"
            cppFlags "-std=c++11 -fexceptions"
            arguments "APP_STL=gnustl_shared"
            abiFilters "armeabi-v7a"
        }
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
sourceSets { main { 
  res.srcDirs = ['src/main/res']
} }

externalNativeBuild {
    ndkBuild {
        path "src/main/jni/Android.mk"
    }
}

    //Modify the below set of code to the ndk-build.cmd location in your computer.

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile project(':openCVLibrary249')
}
点赞