为什么Android Studio会忽略OSX上的.bashrc文件?

时间:2015-11-15 22:00:58

标签: android macos android-studio android-ndk

我已经下载了一个使用Android的ndk的项目。 gradle文件包含以下行:

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

这在运行./gradlew assembleDebug时工作正常。我的~/.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}"

然而,当我尝试从Android工作室内部构建项目时,我得到了

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

这表明PATH变量未在环境中正确设置,甚至ndk-build进程也无法运行。 Touching .bashrc is supposed to make this work。为什么Android Studio不会选择PATH文件中指定的.bashrc环境变量?

编辑PSEUDO ANSWER :Android Studio遵循other development environments like Xcode以避免用户个人配置文件改变软件构建过程。正确的解决方法like Alex mentions是读取外部local.properties文件,其中ndk.dir变量指向正确的路径,并获取此变量以构建ndk-build的完整路径依赖环境的PATHlocal.properties文件可以保持在源代码管理之外,并且可以针对每个结帐进行自定义。

1 个答案:

答案 0 :(得分:3)

对于Android Studio的破坏PATH我们无能为力,但很容易解决 ndk-build 的问题 - 一次又一次。

将以下块添加到模块(应用或库)的 build.gradle 文件的顶部:

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

这是gradle任务:

task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
    commandLine "$ndkBuild" …
}