Android Studio:构建NDK应用程序时Gradle执行失败

时间:2016-11-03 10:59:07

标签: android android-studio android-ndk

build.gradle文件中的这一部分似乎导致执行应用程序失败。

控制台上的错误消息:

Error:Execution failed for task ':app:ndkBuild'.
> Process 'command '/Users/marshall/android-sdks/ndk-bundle/ndk-build'' finished with non-zero exit value 2

ndkBuild任务是我在build.gradle文件中定义的任务。这是代码块。

task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
    def ndkDir = project.plugins.findPlugin('com.android.application').sdkHandler.getNdkFolder()
    commandLine "$ndkDir/ndk-build",
            '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
}

我仔细检查了ndk存储库的目录是否正确,并且它似乎不是失败的原因。

这是整个build.gradle文件的源代码。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.my.project"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

android {
    sourceSets {
        main {
            jni.srcDirs = []
        }
    }
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
    def ndkDir = project.plugins.findPlugin('com.android.application').sdkHandler.getNdkFolder()
    commandLine "$ndkDir/ndk-build",
            '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
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support:cardview-v7:25.0.0'
    compile project(':openCVLibrary310')
    compile 'com.android.support:appcompat-v7:25.0.0'
}

1 个答案:

答案 0 :(得分:0)

我通过将Android SDK和NDK路径添加到.bash_profile文件解决了这个问题,如下所示。

export ANDROID_HOME=/usr/local/opt/android-sdk export PATH=${PATH}:/usr/local/opt/android-ndk