Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
gradle找不到git.exe_Git_Android Studio_Gradle_Git Bash_Gradle Kotlin Dsl - Fatal编程技术网

gradle找不到git.exe

gradle找不到git.exe,git,android-studio,gradle,git-bash,gradle-kotlin-dsl,Git,Android Studio,Gradle,Git Bash,Gradle Kotlin Dsl,我的Android Studio项目使用gradle的git: branchName = ByteArrayOutputStream().use { outputStream -> exec { commandLine("git branch --show-current") standardOutput = outputStream

我的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- . 这里是一个额外的运行Android Studio与此

它可以工作,但需要一些配置,但对于您的案例来说,通常会有点过分,我认为@deecue建议使用较小的if-查看文档:

您只需以一种更愉快、更可重用的方式进行操作—作为一种提取方法:

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,但没有帮助。有什么特殊原因吗?我知道它增加了几行代码,但如果您的目标开发环境是Windows、Linux和Mac,我会说这是一个非常巧妙的黑客攻击。有没有其他方法可以获取分支名称并避免使用命令行?我建议为gradle寻找一个本机git插件,也许可以从那里开始工作?是一个gradle插件,使用JGit执行git操作。我还没有尝试过这个,但我希望这激发了你去寻找更多。