1

I'm using the latest android studio build 1.5 ,since i want to import an eclipse project that needs NDK. My project is working. i try to import org.apache.tools.ant.taskdefs.condition.Os But it's working when i built APK, It's make error:

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

My 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.+'
}

My local.properties:

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

Please help my fix error. Thanks you so much!

Hoang Aloevera
  • 131
  • 2
  • 10

1 Answers1

0

We use the following wrapper:

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

In your gradle, the actual path for ndk-build.cmd is set for non-Windows platform. I believe this is wrong, because the path you set there is a very Windows-y path. And, to avoid the craziness of escape-character multiplication, you can use forward slash / in your gradle files to define paths, e.g. 'C:/Users/Android/AppData/Local/Android/sdk/ndk-bundle'. Only make sure all paths you specify don't have spaces (e.g. 'C:/Program Files').

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • com.android.library what is this for? – Ahmad Arslan Dec 13 '16 at 10:29
  • 1
    @AhmadArslan: I use native code mostly for [library modules](https://developer.android.com/studio/projects/android-library.html); you will use `com.android.application` for an app module. There are other ways to find **ndkDir**: http://stackoverflow.com/questions/21999829/how-do-i-read-properties-defined-in-local-properties-in-build-gradle – Alex Cohn Dec 13 '16 at 15:19