0

更新到 Android Studio Bumblebee 后 | 2021.1.1 (Mac OS),gradle exec 插件无法工作。

这是一个带有 exec 的 gradle 任务示例:

task testExec(type: Exec) {
    commandLine 'npm', '--version'
}

在 Android Studio 中运行此任务时,出现此错误:

Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'npm''
    at org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:241)
    at org.gradle.process.internal.DefaultExecHandle.setEndStateInfo(DefaultExecHandle.java:218)
    at org.gradle.process.internal.DefaultExecHandle.failed(DefaultExecHandle.java:369)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:87)
    at org.gradle.internal.operations.CurrentBuildOperationPreservingRunnable.run(CurrentBuildOperationPreservingRunnable.java:42)
    ... 3 more
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'npm'
    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.startProcess(ExecHandleRunner.java:98)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:71)
    ... 4 more
Caused by: java.io.IOException: Cannot run program "npm" (in directory "/Users/pavel/AndroidStudioProjects/tempexec/app"): error=2, No such file or directory
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
    ... 7 more
Caused by: java.io.IOException: error=2, No such file or directory
    ... 8 more

但是当从控制台 - 运行它时./gradlew app:testExec,一切正常:

> Task :app:testExec
8.3.1
4

1 回答 1

0

找出问题的原因。

新的 Android Studio$PATH出于某种原因替换了变量。启动时commandLine 'bash', '-c', 'echo $PATH',它会打印:/usr/bin:/bin:/usr/sbin:/sbin. 但npm位于/usr/local/bin/npm.

现在的临时解决方案是/usr/local/bin/像这样添加到 PATH 中:

commandLine 'bash', '-c', 'PATH=$PATH:/usr/local/bin/; npm --version'

但是,不幸的是,这个解决方案扼杀了跨平台。

于 2022-01-28T22:32:32.233 回答