【问题标题】:git.exe is not found by gradlegradle 找不到 git.exe
【发布时间】:2021-01-28 19:50:07
【问题描述】:

我的 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 上中断。 关于我应该怎么做的任何建议?

【问题讨论】:

    标签: git android-studio gradle git-bash gradle-kotlin-dsl


    【解决方案1】:

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

    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()
                    }
    

    【讨论】:

    • 有什么特别的原因吗?我知道它添加了几行代码,但如果您的目标开发环境是 Windows、Linux 和 Mac,我会说这是一个非常巧妙的 hack。
    • 有没有其他方法可以获取分支名称并避免使用命令行?
    • 我建议为 gradle 寻找一个本地 git 插件,也许可以从那里开始工作? grgit 是一个使用 JGit 执行 git 操作的 gradle 插件。我还没有尝试过,但我希望这能激发你寻找更多。
    【解决方案2】:

    如果你真的只想用 linux 方式制作,你可以试试 WSL - Windows Subsytem for Linux。这是一个额外的instruction,用于运行 Android Studio。

    它可以工作,但需要进行一些配置,但通常对于您的情况来说这有点矫枉过正,我认为@deecue 建议小,如果 - 查看文档: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()
    }
    

    【讨论】:

    • 我已经这样做了,但不幸的是,它并没有解决问题。
    • 您是否也重新启动了您的 ide/终端?这可能会很疯狂,但也可以尝试重新启动 Windows 以确保该路径是最新的
    • 其实我确实重启了windows但是没用。
    猜你喜欢
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2015-11-06
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多