Search code examples
gitandroid-studiogradlegit-bashgradle-kotlin-dsl

git.exe is not found by gradle


My Android Studio project uses git from gradle :

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

On Linux and Mac, this works just fine, but on Windows it says:

Could not start 'git'

and I have to replace it with this :

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

this defeats the purpose for me since I want this to work on different platforms and machines. If I add it it'll instead break on Linux and Mac. any suggestions on what should I do ?


Solution

  • You can probably do an OS checking this way

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