Stpn
Stpn

Reputation: 6394

Path variable not recognized in Android Studio (ndk-build command is not recognized)

I have an Android Studio project which won't recognize the PATH variable set for ndk-build.

If I run ndk-build from Terminal I get:

stpns-MacBook-Pro:~ stepanboltalin$ ndk-build
  Android NDK: Could not find application project directory !    
  Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.    
  /usr/local/Cellar/android-ndk/r10b/build/core/build-local.mk:148: *** Android NDK: Aborting    .  Stop.

But if I try to compile project in Android Studio, I get error at 'ndk-build' commmandLine (below is the excerpt from build.gradle:

task ndkBuild(type: Exec) {

 # some stuff... 

    if (ant.properties.os == 'windows') {
        commandLine 'ndk-build.cmd'
    } else {
        commandLine 'ndk-build'
    }


}

Now if I add the absolute path everything works fine:

task ndkBuild(type: Exec) {

 # some stuff... 

    if (ant.properties.os == 'windows') {
        commandLine 'ndk-build.cmd'
    } else {
        commandLine '/usr/local/opt/ndk-build'
    }


}

Although the problem is seemingly solved, having build.gradle like that is sub-optimal for development. How can I fix this?

Upvotes: 4

Views: 3428

Answers (1)

You can add the path to your NDK in the local.properties file of the root project:

ndk.dir=/opt/android/ndk

Then replace the invocation of ndk-build like this:

def localProperties = new Properties()
localProperties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkDir = localProperties.getProperty('ndk.dir')
def ndkBuildPrefix = ndkDir != null ? ndkDir + '/' : '';

if (ant.properties.os == 'windows') {
    commandLine ndkBuildPrefix + 'ndk-build.cmd'
} else {
    commandLine ndkBuildPrefix + 'ndk-build'
}

Upvotes: 5

Related Questions