I\'ve setup my Android Studio Project to work with Github. Here is my Manifext
apply plugin: \'com.android.application\'
def gitVersion() {
def counter
I found the problem. Android studio performs command line operations in the project folder by default. I had made my project such that the git repo lived inside my app directory instead of in my project directory, so any git commands on the project level would error, since it didn't know about the git repo one directory down.
My solution was to move my git repo out of the app directory and into the project directory. Once I did that, the script worked perfectly.
Try this:
def gitCommitCount = Integer.parseInt(['sh', '-c', 'git rev-list HEAD | wc -l | tr -d " "'].execute([], project.rootDir).text.trim())
defaultConfig {
...
versionCode gitCommitCount
...
}
This method will help you get your revision number:
int extractVersionCode() {
def stdout = new ByteArrayOutputStream()
exec {
workingDir projectDir
executable 'git'
args 'rev-list', '--count', 'HEAD'
standardOutput = stdout
}
return stdout.toString().trim().toInteger()
}
Usage:
versionCode extractVersionCode()
I wrote and tested that script for use on OSX Mavericks. What kind of OS platform are you using to make your build? Also, have you tried it without the ".text" ? You may have to play around with different combinations on that line to see what will work.
Just specify the working directory for execute()
:
.execute([], project.rootDir)