JHH
JHH

Reputation: 9295

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?

Upvotes: 3

Views: 1708

Answers (1)

lance-java
lance-java

Reputation: 27984

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

Upvotes: 3

Related Questions