Android Studio 中无法识别路径变量(无法识别 ndk-build 命令)

人气:1,032 发布:2022-10-16 标签: path macos android-studio gradle android-ndk

问题描述

我有一个 Android Studio 项目,它无法识别为 ndk-build 设置的 PATH 变量.

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

如果我从终端运行 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.

但是如果我尝试在 Android Studio 中编译项目,我会在 'ndk-build' commmandLine 出现错误(以下是 build.gradle 的摘录:

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


}

虽然问题似乎已经解决,但像这样的 build.gradle 对于开发来说并不是最佳选择.我怎样才能解决这个问题?

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

推荐答案

您可以在根项目的 local.properties 文件中添加 NDK 的路径:

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

ndk.dir=/opt/android/ndk

然后像这样替换ndk-build的调用:

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

820