Gradle脚本自动转换并在Android中包含提交哈希

时间:2015-02-13 11:35:24

标签: android git android-studio gradle versioning

我需要编写一个gradle脚本来在每次提交时自动对我的应用程序进行版本控制。我还需要在测试人员的应用程序中包含提交哈希作为参考。

我很担心自动版本控制通常如何工作。有人可以解释自动转换过程吗?

6 个答案:

答案 0 :(得分:106)

我遇到了类似的问题,但不想修改versionName以包含git哈希。我们希望将其保留为1.2.2之类的东西,但仍然可以在UI中显示git哈希。

我修改了the other answer here中的代码,使用buildConfigField任务生成可以在Java代码中引用的BuildConfig.GitHash值。

在模块的build.gradle文件的android部分上方添加:

def getGitHash = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

然后将以下行添加到build.gradle的defaultConfig部分的android部分,即versionName下面:

buildConfigField "String", "GitHash", "\"${getGitHash()}\""

这会在自动生成的BuildConfig.java文件中生成以下行:

// Fields from default config.
public static final String GitHash = "e61af97";

现在,您可以使用BuildConfig.GitHash在您的Java代码中获取git哈希。

答案 1 :(得分:11)

一个理想的解决方案可能是从项目的git状态中获取版本。这样,版本控制不依赖于您记住增加变量,或更改gradle或配置文件中的任何文本。另一个优点是版本名称和代码可追溯到一个特定的代码状态。

您可以在http://ryanharter.com/blog/2013/07/30/automatic-versioning-with-git-and-gradle/

中找到一个描述性示例

想法是使用getVersionName函数获取git信息,并在gradle脚本中使用该函数:

/*
 * Gets the version name from the latest Git tag
 */
def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

当然,您需要使用命令行git(因为将执行命令git describe --tags以生成信息)。

另一种方法(也基于从git获取版本信息)可以将该逻辑外部化为gradle插件 - 例如: - https://github.com/moallemi/gradle-advanced-build-version - https://github.com/infusionsoft/gradle-build-version-plugin - https://github.com/nemerosa/versioning

要使用的将取决于您要应用的版本控制策略。

答案 2 :(得分:4)

我创建了一个Gradle插件来为您执行此操作。项目和完整说明位于https://github.com/lessthanoptimal/gversion-plugin

要使用它,请将以下内容添加到build.gradle文件

plugins {
  id "com.peterabeles.gversion" version "1.2.4"
}

gversion {
  srcDir = "src/main/java/"
  classPackage = "com.your.package"
  className = "MyVersion"                   // optional. If not specified GVersion is used
  dateFormat   = "yyyy-MM-dd'T'HH:mm:ss'Z'" // optional. This is the default
  timeZone     = "UTC"                      // optional. UTC is default
}

现在你只需要运行gradle任务' createVersionFile'创建文件。您可能需要考虑将以下行添加到gradle项目project.compileJava.dependsOn(createVersionFile)这将导致它每次Gradle构建项目时生成文件。有关Android说明,请参阅上面的网站。

这是文件的样子

/**
 * Automatically generated file containing build version information.
 */
public class MyVersion {
    public static final String MAVEN_GROUP = "com.your";
    public static final String MAVEN_NAME = "project_name";
    public static final String VERSION = "1.0-SNAPSHOT";
    public static final int GIT_REVISION = 56;
    public static final String GIT_SHA = "a0e41dd1a068d184009227083fa6ae276ef1846a";
    public static final String BUILD_DATE = "2018-04-11T12:19:03Z";
    public static final long BUILD_UNIX_TIME = 1523449143116L;
}

您可能还想将版本文件添加到.gitignore中,因为它是自动生成的,而您不想在git中使用它。

答案 3 :(得分:2)

另外值得一看grgit - Groovy/Gradle Git,这有助于在Gradle脚本中简化信息提取,包括Git commit-hashes。

答案 4 :(得分:0)

将以下代码添加到您的build.gradle

def gitCommitHash = 'git rev-parse --verify --short HEAD'.execute().text.trim()
defaultConfig{
    ... otherConfigs
    buildConfigField("String", "GIT_HASH", "\"${gitCommitHash}\"")
}

现在您可以通过BuildConfig.GIT_HASH

获取git hash了

玩得开心

答案 5 :(得分:0)

试试这个我已经成功尝试过 Xamarine 应用程序的完整示例。 https://github.com/manojalwisnz/Mobile-App-Auto-Versioning