git.exe is not found by gradle



我的Android Studio项目使用gradle:中的git

branchName = ByteArrayOutputStream().use { outputStream ->
exec {
commandLine("git branch --show-current")
standardOutput = outputStream
}
outputStream.toString()
}

在Linux和Mac上,这很好,但在Windows上,它说:

无法启动"git">

,我必须用以下内容替换它:

commandLine("cmd", "/c", "git branch --show-current")

这违背了我的目的,因为我希望它能在不同的平台和机器上工作。如果我添加它,它会在Linux和Mac上崩溃。我该怎么办有什么建议吗?

您可能可以通过这种方式进行操作系统检查

import org.apache.tools.ant.taskdefs.condition.Os
// ...
branchName = ByteArrayOutputStream().use { outputStream ->
exec {
if (Os.isFamily(Os.FAMILY_WINDOWS)) commandLine("cmd", "/c", "git branch --show-current")
else commandLine("git branch --show-current")
standardOutput = outputStream
}
outputStream.toString()
}

如果你真的想只在linux中实现它,你可以尝试一下WSL-适用于Linux的Windows子系统。以下是使用此软件运行Android Studio的附加说明。

它可以工作,但需要一些配置,但通常情况下,它对您的案例来说有点过头了,我认为@deecue建议使用小if-查看文档是正确的:https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html

你可以用一种更愉快、更可重复使用的方式来做——作为一种提取的方法:

import org.apache.tools.ant.taskdefs.condition.Os
// ...
fun runCommand(command: String, windowsDir: String = "/c") {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine("cmd", windowsDir, command)
} else {
commandLine(command)
}
}
// ...
branchName = ByteArrayOutputStream().use { outputStream ->
exec {
runCommand("git branch --show-current")
standardOutput = outputStream
}
outputStream.toString()
}

相关内容

最新更新