4

I have downloaded a project that uses Android's ndk. The gradle file contains the lines:

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

This works fine when running ./gradlew assembleDebug. I have the following contents in my ~/.bashrc:

# Append android sdk paths and stuff.
export ANDROID_HOME=/Users/gradha/instalacion_manual/android-sdk-r10-mac_x86
#export ANDROID_NDK_ROOT=/Users/gradha/instalacion_manual/android-ndk-r8b
export ANDROID_NDK_ROOT=/Users/gradha/instalacion_manual/android-ndk-r10e
export NDK_PATH="${ANDROID_NDK_ROOT}"
export NDK_HOME="${ANDROID_NDK_ROOT}"
export PATH=$PATH:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools:/Users/gradha/instalacion_manual/apache-maven-3.3.3/bin:"${ANDROID_NDK_ROOT}"

However, when I try to build the project from inside Android studio I get

process.internal.ExecException: A problem occurred starting process 'command 'ndk-build''
    at org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:196)
    at org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:325)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:83)
    ... 1 more
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'ndk-build'
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
    at net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:65)
    ... 1 more
Caused by: java.io.IOException: Cannot run program "ndk-build" (in directory "/Users/gradha/project/questionity/archivo/SuperpoweredSDK/Android/CrossExample/app"): error=2, No such file or directory
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
    ... 3 more
Caused by: java.io.IOException: error=2, No such file or directory
    ... 4 more

Which suggests that the PATH variable is not being set properly in the environment and not even the ndk-build process is able to run. Touching .bashrc is supposed to make this work. Why does Android Studio not pick up the PATH environment variable specified in the .bashrc file?

EDIT PSEUDO ANSWER: Android Studio follows other development environments like Xcode to avoid user personal configuration files altering the software build process. The correct workaround, like Alex mentions is to read an external local.properties file with an ndk.dir variable pointing at the proper path, and source this variable to build a full path to ndk-build instead of relying on the environment's PATH. The local.properties file can be kept out of source control and be customised for each checkout.

Community
  • 1
  • 1
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
  • did you just set that? try to logout/login or reboot – zapl Nov 15 '15 at 22:06
  • 1
    Unless you are starting Android Studio from the command line, it is unlikely to inherit anything in your .bashrc – Chris Stratton Nov 15 '15 at 22:12
  • @ChrisStratton starting Android Studio from the command line with ``/Users/gradha/Applications/Android\ Studio.app/Contents/MacOS/studio`` doesn't change the result, it seems the IDE explicitly spawns a shell ignoring all user configuration files. – Grzegorz Adam Hankiewicz Nov 15 '15 at 22:27
  • Android studio has historically been notoriously broken where cooperation with the ndk is concerned. You should test if you can run ndk-build from the command line; if not, your problem is that you have not added this to the path or your ndk installation is broken. But if you can run it from the command line, the problem is that android studio is still broken. – Chris Stratton Nov 15 '15 at 22:31
  • gosh, environment variables seem to be one of the most difficult things when you have to use osx http://apple.stackexchange.com/questions/119711/why-mac-os-x-dont-source-bashrc / http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x – zapl Nov 15 '15 at 22:33

1 Answers1

3

There is nothing we can do with the broken PATH for Android Studio, but is is easy to resolve the problem of ndk-build - once and forever.

Add the following block to the top of build.gradle file for the module (app or library):

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkBuild = properties.getProperty('ndk.dir') + '/ndk-build'
import org.apache.tools.ant.taskdefs.condition.Os
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
    ndkBuild += '.cmd'
}

And here is the gradle task:

task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
    commandLine "$ndkBuild" …
}
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • 2
    Another variant at http://stackoverflow.com/questions/26960891/path-variable-not-recognized-in-android-studio-ndk-build-command-is-not-recogni/26961349#26961349. – Grzegorz Adam Hankiewicz Nov 16 '15 at 14:35
  • Thanks for this link and for the explanation. Actually, unlike circa '14, modern Android Studio expects you to have this **ndk.dir** setting in **local.properties**, and even provides nice GUI to fill this value. – Alex Cohn Nov 16 '15 at 14:47