之前一直用windows电脑开发,采用git rev-list --count HEAD
获取版本号作为VersionCode来进行编译打包,近期更换mac电脑,gradle报错。
解决路径
static def getGitVersion() {
return 'git rev-list --count HEAD'.execute().text.trim()
}
def obtainVersionCode = getGitVersion().toInteger()
gradle报NumberFormatException,getGitVersion()是空字符。
cmd运行改为'git rev-list --count HEAD'.execute().err.text.trim()
并打印出来
fatal: Not a git repository (or any of the parent directories):
project的app module下不存在.git文件,于是切换到项目根目录执行cmd
def getGitVersion() {
def cmd = "git -C ${project.rootDir} rev-list --count HEAD"
println "git version cmd:" + cmd
def version = cmd.execute().err.text.trim()
println "git version:"+version
return version
}
def obtainVersionCode = getGitVersion().toInteger()
问题得到解决