Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle cannot find git.exe on Windows

My Android Studio project uses git from gradle (to include the git SHA-1 in the built artifacts):

def getGitVersion = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

On Linux and Mac this works just fine, but on Windows I get the following:

    Caused by: org.gradle.api.GradleScriptException: A problem occurred evaluating project ':app'.
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:92)
        ...
    ... 75 more
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'git''
    at org.gradle.process.internal.DefaultExecHandle.execExceptionFor(DefaultExecHandle.java:232)
        ...
    ... 3 more
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'git'
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
        ...
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:70)
    ... 4 more

I'm using Git Bash, and I've added the path to git.exe to my System and User environment variables. If I start a terminal inside Android Studio, it also has git.exe on its path. But when AS launches gradle to build my project, apparently Gradle cannot find git.exe.

I can't find any background service or daemon for gradle, thinking that restarting the process after modifying the environment variables might be the problem. So what could be causing my build.gradle to fail executing git? Does Gradle have its own shell with a custom environment?

like image 269
JHH Avatar asked Nov 25 '25 10:11

JHH


1 Answers

Gradle daemon has a clean/empty environment and doesn't inherit the caller's environment.

I do this

ext {
   gitExe = 'c:/GIT-2.7.0/bin/git.exe'
}
task clone {
   doLast {
      exec {
         commandLine "cmd /c $gitExe clone https://my-git/project.git".split(' ')
      }
   } 
} 
like image 94
lance-java Avatar answered Nov 27 '25 02:11

lance-java



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!