每当我尝试构建一个使用ndk运行本机代码的Android应用程序时,我都会收到以下错误:
Error:Gradle: Execution failed for task ':sampleNDK:ndkBuild'.
> Process 'command 'C:\NDK\android-ndk-r10e/ndk-build.cmd'' finished with non-zero exit value 2
我使用Intellij和爷爷项目。我认为错误出现在build.gradle文件中的此函数中:
task ndkBuild(type: Exec) {
def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.getNdkFolder()
if(org.gradle.internal.os.OperatingSystem.current().windows){
commandLine "$ndkDir/ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
//does not work too
//commandLine "$ndkDir\\ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
}else{
commandLine "$ndkDir/ndk-build", 'NDK_PROJECT_PATH=src/main'
}
}
事实上,我正在使用Windows,我认为错误会出现在if的情况下。
有谁知道出了什么问题或者我可以尝试避免失败?
更新 这就是整个构建脚本。也许这很有用。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "de.anmi.android.samplendk"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
sourceSets.main.jni.srcDirs = []
sourceSets.main.jniLibs.srcDirs = ['src/main/libs']
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
task ndkBuild(type: Exec) {
// http://stackoverflow.com/questions/28615439/android-gradle-plugin-1-1-0-getndkfolder-not-found-anymore-any-replacement
def ndkDir = plugins.getPlugin('com.android.application').sdkHandler.getNdkFolder()
if (org.gradle.internal.os.OperatingSystem.current().windows) {
commandLine "$ndkDir/ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
//does not work too
// commandLine "$ndkDir\\ndk-build.cmd", 'NDK_PROJECT_PATH=src/main'
} else {
commandLine "$ndkDir/ndk-build", 'NDK_PROJECT_PATH=src/main'
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}