Search code examples
androidopencvgradleandroid-ndkandroid-gradle-plugin

Execution failed for ':app:ndkBuild'. Process 'command ndk-build.cmd' finished with non-zero exit value 2


I have been stuck on this problem for 2 days and have tried out all possible solutions given on stackoverflow. Below is my build.gradle file:

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

I have already tried all possible solutions- deleting the obj folder in the build folder, trying to avoid automatic Android.mk call by setting the sourceSets.main, trying to avoid the compileDebugNdk task from getting called. I also do not have any cmake.txt files. I cannot seem to get over the problem.

I have used Android Studio 2.3.2 and 2.1.1 and the problem has persisted in both of them.

Any help would be appreciated.


Solution

  • You should use the latest Android Studio, 2.3.2 is OK. It has integrated externalNativeBuild in android gradle plugin, so you don't need the tricks with custom gradle task.

    I could not actually test the build.gradle script below, so please forgive me any typos:

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