Android Studio NDK构建问题错误:任务执行失败':app:ndkBuild'

时间:2016-05-12 08:57:50

标签: android android-ndk build.gradle

我使用最新的android studio build 1.5,因为我想导入一个需要NDK的eclipse项目。我的项目正在运作。我尝试导入org.apache.tools.ant.taskdefs.condition.Os但是当我构建APK时它正在工作,它会导致错误:

Error:Execution failed for task ':app:ndkBuild'.
> A problem occurred starting process 'command 'ndk-build.cmd''

我的build.gradle:

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

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "vaeapp.gamecard.vn"
        minSdkVersion 14
        targetSdkVersion 19
        multiDexEnabled = true
        versionCode 4
        versionName "1.3.4"

        ndk {
            moduleName "gc"
        }
    }
    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine 'C\\:\\\\Users\\\\Android\\\\AppData\\\\Local\\\\Android\\\\sdk\\\\ndk-bundle', '-C', file('src/main/jni').absolutePath
        }
    }

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

    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    compile 'com.facebook.android:facebook-android-sdk:4.2.0'
    // compile 'com.android.support:appcompat-v7:20.0.0'
//    compile 'com.google.android.gms:play-services:+'
    compile 'com.google.android.gms:play-services:4.0.30'
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/commons-io-2.4.jar')
    compile files('libs/error-reporter.jar')
    //compile files('libs/httpclient-4.0.1.jar')
    compile files('libs/mail.jar')
    compile files('libs/universal-image-loader-1.9.3.jar')
    compile fileTree(dir: 'libs', include: 'Parse-*.jar')
    // compile 'com.parse:parse-android:1.10.1'
    compile 'com.parse.bolts:bolts-android:1.+'
    compile 'com.parse:parse-android:1.+'
}

我的local.properties:

ndk.dir=C\:\\Users\\Android\\AppData\\Local\\Android\\sdk\\ndk-bundle
sdk.dir=C\:\\Users\\Android\\AppData\\Local\\Android\\sdk

请帮我修复错误。非常感谢你!

1 个答案:

答案 0 :(得分:0)

我们使用以下包装器:

task ndkBuild(type: Exec) {

    File ndkDir = project.getPlugins().getPlugin('com.android.library').sdkHandler.getNdkFolder()
    if (ndkDir == null) {
        ndkDir = file(System.getenv('NDK_ROOT'))
    }

    if (ndkDir == null) {
        def gradle_project_root = project.rootProject.rootDir
        throw new GradleException("NDK is not configured. Make sure there is a local.properties " +
                "file with an ndk.dir entry in the directory ${gradle_project_root}, or set the " +
                "ANDROID_NDK envrionment variable")
    }

    def ndkBuildExecutable = new File(ndkDir, 'ndk-build')
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        ndkBuildExecutable += '.cmd'
    }
    if (!ndkBuildExecutable.exists()) {
        throw new GradleException("Could not find ndk-build. The configured NDK directory ${ndkDir} may not be correct.")
    }
    commandLine(ndkBuildExecutable, '-j8', '-C', file('src/main').absolutePath)
}

在您的平台中,为非Windows平台设置 ndk-build.cmd 的实际路径。我认为这是错误的,因为你设置的路径是一个非常Windows-y的路径。并且,为了避免转义字符乘法的疯狂,您可以在gradle文件中使用正斜杠/来定义路径,例如'C:/Users/Android/AppData/Local/Android/sdk/ndk-bundle'。只确保您指定的所有路径都没有空格(例如'C:/Program Files')。