How to insert the commit and branch info from git

2019-04-11 12:27发布

问题:

We have lots of developers and lots of devices and lots of fresh and old versions of apk. Sometimes bad behaviour is occurred. I need to detect what version of the apk is installed on the device (what branch, what last commit, what files was changed from last commit, when apk criated).

We use Gradle to make apk from projects.

Is there any method (directive or plugin or script) to place the version information somewhere in the apk? For example, as constant in java class or as text file in resources.

回答1:

You could use next snippet:

task gitInfo << {
   ext.revision = getGitRevParseInfo ( "--short" )
   ext.branch = getGitRevParseInfo ( "--abbrev-ref" )

   println ext.revision
   println ext.branch
}

def getGitRevParseInfo (what) {
   def cmd = "git rev-parse " + what + " HEAD"
   def proc = cmd.execute ()
   proc.text.trim ()
}

So you can use ext for constructing versionName for android app