使用git describe和Gradle进行Android构建的自动版本控制
我进行了广泛的搜索,但是可能是由于Android Studio和Gradle的新颖性.我尚未找到有关如何执行此操作的任何说明.我基本上想做的完全是
I have searched extensively, but likely due to the newness of Android Studio and Gradle. I haven't found any description of how to do this. I want to do basically exactly what is described in this post, but with Android Studio, Gradle and Windows rather than Eclipse and Linux.
获得最近成功的结果的一种更正确,更精益的方法是使用 Groovy JGit绑定实现git集成.由于它使用JGit,因此甚至不需要安装git就可以正常工作.
A more proper and lean way to achieve the result which gained traction lately would be to use gradle git integration via Groovy JGit bindings. As it uses JGit it doesn't even require git to be installed to work.
这是一个基本示例,显示了类似的解决方案(但在gitVersionName字符串中包含一些其他信息):
Here's a basic example showing a similar (but with some additional information in gitVersionName string) solution:
buildscript {
dependencies {
classpath 'org.ajoberstar:grgit:1.4.+'
}
}
ext {
git = org.ajoberstar.grgit.Grgit.open()
gitVersionCode = git.tag.list().size()
gitVersionName = "${git.describe()}"
}
android {
defaultConfig {
versionCode gitVersionCode
versionName gitVersionName
}
}
[...]
您可以在 Grgit API文档中看到
As you can see in Grgit API documentation the describe operation provides additional information other than most recent tag reachable in history:
查找可从HEAD访问的最新标签.如果标签指向提交,则仅显示标签.否则,它会在标记名称后加上标记对象后附加的提交次数以及最近提交的缩写对象名称.
Find the most recent tag that is reachable from HEAD. If the tag points to the commit, then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.
无论如何,它不会告诉您状态是否肮脏.可以通过查看回购协议的清除状态轻松添加此信息,然后如果不干净,则附加一个字符串.
Anyhow, it won't tell if the state is dirty or not. This information can be easily added by looking at the clean status of the repo, and appending a string if it's not clean.