30

I'm having trouble getting my NDK to compile properly in Android Studio. Whenever I try running to compile I am getting the following error.

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

I have the following setup

enter image description here

And my build.gradle file is the following.

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "edu.uky.cs.www.diagramaphone"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        sourceSets.main{
            jniLibs.srcDir 'src/main/libs'
            jni.srcDirs = [] //disable automatic ndk-build call
        }
        project.ext.versionCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3, 'mips':5, 'mips64':6, 'x86':8, 'x86_64':9] //versionCode digit for each supported ABI, with 64bit>32bit and x86>armeabi-*
        android.applicationVariants.all { variant ->
            // assign different version code for each output
            variant.outputs.each { output ->
                output.versionCodeOverride =
                        project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + defaultConfig.versionCode
            }
        }
        // call regular ndk-build(.cmd) script from app directory
        task ndkBuild(type: Exec) {
            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
            } else {
                commandLine 'ndk-build', '-C', file('src/main').absolutePath
            }
        }
        tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn ndkBuild
        }
        //ndk {
          //  moduleName "shape-detect"
            //cFlags "-DANDROID_NDK -D_DEBUG DNULL=0" // Define some macros
            //ldLibs "EGL", "GLESv3", "dl", "log"         // Link with these libraries!
            //stl "stlport_shared"                        // Use shared stlport library
        //}

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

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':libraries:tess-two')
    compile project(':libraries:opencv')
}

At this point I'm lost as to what can be wrong. I've followed several tutorials to try setting up the NDK to work properly, but I keep getting the error I showed above. Can anyone here provide some feedback on what I need to do in order to get the NDK to compile?

EDIT: Here's the entire message that occurs.

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:libraries:opencv:compileLint
:libraries:opencv:copyReleaseLint UP-TO-DATE
:libraries:opencv:mergeReleaseProguardFiles UP-TO-DATE
:libraries:opencv:preBuild UP-TO-DATE
:libraries:opencv:preReleaseBuild UP-TO-DATE
:libraries:opencv:checkReleaseManifest
:libraries:opencv:prepareReleaseDependencies
:libraries:opencv:compileReleaseAidl UP-TO-DATE
:libraries:opencv:compileReleaseRenderscript UP-TO-DATE
:libraries:opencv:generateReleaseBuildConfig UP-TO-DATE
:libraries:opencv:generateReleaseAssets UP-TO-DATE
:libraries:opencv:mergeReleaseAssets UP-TO-DATE
:libraries:opencv:generateReleaseResValues UP-TO-DATE
:libraries:opencv:generateReleaseResources UP-TO-DATE
:libraries:opencv:packageReleaseResources UP-TO-DATE
:libraries:opencv:processReleaseManifest UP-TO-DATE
:libraries:opencv:processReleaseResources UP-TO-DATE
:libraries:opencv:generateReleaseSources UP-TO-DATE
:libraries:opencv:compileReleaseJava UP-TO-DATE
:libraries:opencv:processReleaseJavaRes UP-TO-DATE
:libraries:opencv:packageReleaseJar UP-TO-DATE
:libraries:opencv:compileReleaseNdk UP-TO-DATE
:libraries:opencv:packageReleaseJniLibs UP-TO-DATE
:libraries:opencv:packageReleaseLocalJar UP-TO-DATE
:libraries:opencv:packageReleaseRenderscript UP-TO-DATE
:libraries:opencv:bundleRelease UP-TO-DATE
:libraries:tess-two:compileLint
:libraries:tess-two:copyReleaseLint UP-TO-DATE
:libraries:tess-two:mergeReleaseProguardFiles UP-TO-DATE
:libraries:tess-two:preBuild UP-TO-DATE
:libraries:tess-two:preReleaseBuild UP-TO-DATE
:libraries:tess-two:checkReleaseManifest
:libraries:tess-two:prepareReleaseDependencies
:libraries:tess-two:compileReleaseAidl UP-TO-DATE
:libraries:tess-two:compileReleaseRenderscript UP-TO-DATE
:libraries:tess-two:generateReleaseBuildConfig UP-TO-DATE
:libraries:tess-two:generateReleaseAssets UP-TO-DATE
:libraries:tess-two:mergeReleaseAssets UP-TO-DATE
:libraries:tess-two:generateReleaseResValues UP-TO-DATE
:libraries:tess-two:generateReleaseResources UP-TO-DATE
:libraries:tess-two:packageReleaseResources UP-TO-DATE
:libraries:tess-two:processReleaseManifest UP-TO-DATE
:libraries:tess-two:processReleaseResources UP-TO-DATE
:libraries:tess-two:generateReleaseSources UP-TO-DATE
:libraries:tess-two:compileReleaseJava UP-TO-DATE
:libraries:tess-two:processReleaseJavaRes UP-TO-DATE
:libraries:tess-two:packageReleaseJar UP-TO-DATE
:libraries:tess-two:compileReleaseNdk UP-TO-DATE
:libraries:tess-two:packageReleaseJniLibs UP-TO-DATE
:libraries:tess-two:packageReleaseLocalJar UP-TO-DATE
:libraries:tess-two:packageReleaseRenderscript UP-TO-DATE
:libraries:tess-two:bundleRelease UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72103Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42103Library UP-TO-DATE
:app:prepareDiagramaphoneLibrariesOpencvUnspecifiedLibrary UP-TO-DATE
:app:prepareDiagramaphoneLibrariesTessTwoUnspecifiedLibrary UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:ndkBuild FAILED
Error:Execution failed for task ':app:ndkBuild'.
> A problem occurred starting process 'command 'ndk-build.cmd''
Information:BUILD FAILED
Information:Total time: 2.226 secs
Information:1 error
Information:0 warnings
Information:See complete output in console
Valrok
  • 1,514
  • 7
  • 30
  • 49
  • Sorry for a misleading answer. `-C` is the correct form of parameter for `ndk-build.cmd`. Try to add the NDK root directory to your **`PATH`**. – Alex Cohn Apr 20 '15 at 06:01
  • How can I confirm that my ndk is properly set up? I'll edit my post again to show what I currently have, ideally the error is something small that Im missing – Valrok Apr 20 '15 at 13:24
  • NDK setup is only to unpack the archive and maybe add the resulting directory to PATH. A good check would be to open command prompt and try to run `ndk-build.cmd` in that window. Actually, there is one non-trivial rule: don't use paths with spaces, like `C:\Program Files` – Alex Cohn Apr 20 '15 at 18:10
  • Hi, I am getting the same issue in Android studio Ubuntu, I am using ubuntu and in build.gradle OSFamily set to windows.How to solve the issue ?.Please help me ASAP.Thanks in Advance. – Naveen Aug 01 '16 at 07:50

9 Answers9

52

I met this kind of problem. First of all You have to give your NDK path in local.properties of your app.

e.g. ndk.dir=/home/user/bin/android_ndk/android-ndk-r10e

Then in my build.gradle file I had something like this for calling ndk build command.

commandLine 'ndk-build', '-C', file('src/main/jni').absolutePath

I'v change it to

commandLine '/home/user/bin/android_ndk/android-ndk-r10e/ndk-build', '-C', file('src/main/jni').absolutePath

I gave full path of NDK build. Hope it'll help you.

Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61
  • 8
    It's better to use a value from `local.properties` instead of hard-coded path: `def ndkDir = plugins.getPlugin('com.android.library').sdkHandler.ndkFolder`. And then the command line will be like this: `commandLine '$ndkDir/ndk-build', '-C', file('src/main/jni').absolutePath` – std.denis Jun 06 '16 at 10:38
  • 1
    If you had this `ndk.dir` defined in `local.properties`, why did you give the full path ? – dum4ll3 Jun 07 '16 at 21:39
  • in local properties I have path to android-ndk-r10e, in build.gradle I have path to ndk_build. I agreed that instead of home/user/bin/android_ndk/android-ndk-r10e I could use ndk.dir from local properties. – Hayk Nahapetyan Jun 08 '16 at 20:04
  • 3
    bump on this answer. Also, if you are using Windows and getting the same error, edit accepted answers line `'/home/user/bin/android_ndk/android-ndk-r10e/ndk-build'` to `'/home/user/bin/android_ndk/android-ndk-r10e/ndk-build.cmd'` – 9301293 Nov 14 '16 at 01:21
  • i still got error after changed it .. how to solve it ? – MAS. John Jun 30 '17 at 08:06
13

In android studio, File->Project Structure -> SDK Location, Set the Android NDK location for your computer.

Thanks!

jungwook
  • 371
  • 4
  • 5
8

try this

commandLine'E:\\Android\\ndk\\ndkbuild.cmd','-C',file('src/main/jni').absolutePath

replace (E:\\Android\\ndk\\) with your ndk path.

Nooh
  • 1,548
  • 13
  • 21
7

its easy for android studio 2.3.3. just follow File->Project Structure -> android NDK Location-> Click download button. after install ndk solve my problem. enter image description here

Md Tariqul Islam
  • 2,736
  • 1
  • 20
  • 35
4

Just in case if it helps somebody else:

Giving NDK path(parent folder where ndkBuild.cmd resides) in Environment Variables also solves the problem.

Alternatively, one can follow @Hayk 's answer if you are on Linux or @Nooh 's answer for Windows.

My observation:

As, if I put direct NDK path solved my problem in Android Studio sometimes but again it failed to build and threw error and then I put path in Environment Variables and the project build successfully.

In that case for Windows one has to put *

  • ndk-build.cmd

* and for Linux environment only *

  • ndk-build

*

abhy
  • 933
  • 9
  • 13
3

Add the ndk path (C:\android-ndk-r10e) to the path variable's value in the environment variable(System variable) which left my "path" variable like this

Settings --> System --> Advenced System Settings --> System Variables enter image description here


Then open the the command window (cmd). Enter the location of your project folder with cd commands and write "ndk-build" then press enter.

3
  1. First of all you have to check local.properties file in your project. In local.properties check ndk path. It looks like this ndk.dir=D:\\sdk\\ndk-bundle.

  2. Now go to the build.gradle(Module library) and find getNdkPath(). It will return ndk path location which concatnate the ndk-build

  3. Change ndk-build --> ndk-build.cmd. It looks like this:

    enter image description here

Hope it will help you!

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Jatinder Kumar
  • 455
  • 5
  • 13
1

The accepted answer is OK, but it doesn't work since android tools gradle plugin 2.3: No sdkHandler field in LibraryPlugin after updating to build tools 2.3.0, so you need to use project.android.ndkDirectory.absolutePath variable then, ie:

task ndkBuild(type: Exec) {
    commandLine project.android.ndkDirectory.absolutePath + '/ndk-build', '-C', file('src/main/jni/').absolutePath
}

Alternatively you can read ndk.dir directly from local.properties: https://stackoverflow.com/a/32649204/1028256

Mixaz
  • 4,068
  • 1
  • 29
  • 55
0

Have a look in these link below.

You must know the path where the NDK is usually at

Library/Android/sdk/ndk/<version_number>

https://developer.android.com/studio/releases?utm_source=android-studio#4-0-0-ndk-dir

and

https://developer.android.com/studio/projects/install-ndk

and

https://wiki.appcelerator.org/display/guides2/Installing+the+Android+NDK

Thiago
  • 12,778
  • 14
  • 93
  • 110