【问题标题】:Gradle script to autoversion and include the commit hash in AndroidGradle 脚本自动版本并在 Android 中包含提交哈希
【发布时间】:2015-04-14 10:35:17
【问题描述】:

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

我很困惑自动版本控制通常是如何工作的。有人能解释一下自动版本化过程吗?

【问题讨论】:

    标签: android git android-studio gradle versioning


    【解决方案1】:

    一个理想的解决方案是从项目的 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()
    }
    

    kts:

    val gitDescribe: String by lazy {
        val stdout = ByteArrayOutputStream()
        rootProject.exec {
            commandLine("git", "describe", "--tags")
            standardOutput = stdout
        }
        stdout.toString().trim()
        /* 'g' doesn't belong to the commit id and stays for 'git'
           v0.1.9-1-g3a259e0 -> v0.1.9-1-3a259e0
           if you like this to be removed then */
        //.replace("-g", "-") 
    }
    

    当然,你需要有命令行 git 可用(因为将执行命令git describe --tags 来生成信息)。

    另一种方法(也基于从 git 获取版本信息)可以将该逻辑外部化到 gradle 插件 - 例如:

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

    【讨论】:

    • 非常好的主意,为了舒适,我将添加 kotlin 对应项
    【解决方案2】:

    将以下代码添加到您的 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

    玩得开心

    【讨论】:

    • 欣赏这个答案的简洁性。谢谢:)
    【解决方案3】:

    我创建了一个 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 中使用它。

    【讨论】:

    • 太棒了!我使用反射来避免视觉警告和错误,但这只是我的偏好。如果它有开箱即用的 git short rev 以及资源文件生成而不是类,我也会喜欢它。
    【解决方案4】:

    还值得一看 grgit - Groovy/Gradle Git,它可以帮助简化 Gradle 脚本中信息的提取,包括 Git 提交哈希。

    【讨论】:

    • 这很有帮助,但是这个插件在单存储库中使用时存在内存问题,并且传递 jgit 依赖项与另一个项目冲突。
    【解决方案5】:

    我遇到了类似的问题,但不想修改 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的android部分的defaultConfig部分,即versionName下面:

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

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

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

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

    【讨论】:

    • 嗨,我收到了这个错误:CreateProcess error=2,系统找不到指定的文件,任何导致我的代码降级都在 GitLabs 上
    • @Haroon:检查以确保 git 的路径在您的 PATH 环境变量中。通过打开一个新的命令提示符/终端并输入“git --version”来检查这一点
    • 感谢@Paul 你写的正是我所做的,只是让 git 可用于环境变量
    • 这样做的问题是,在提交 git 后,整个应用程序需要重新构建。
    猜你喜欢
    • 2019-03-30
    • 2017-06-21
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 2018-02-11
    • 2012-10-07
    • 1970-01-01
    相关资源
    最近更新 更多