Grzegorz Adam Hankiewicz
Grzegorz Adam Hankiewicz

Reputation: 7661

Why is Android Studio ignoring my .bashrc file on OSX?

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.

Upvotes: 4

Views: 1358

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

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" …
}

Upvotes: 3

Related Questions