git Gradle 脚本自动版本并在 Android 中包含提交哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28498688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:45:57  来源:igfitidea点击:

Gradle script to autoversion and include the commit hash in Android

androidgitandroid-studiogradleversioning

提问by Shatazone

I need to write a gradle script to auto version my application on every commit. I need to also include the commit hash as a reference in the application for testers.

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

I am confused how auto versioning usually work. Can someone explain the autoversioning process?

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

回答by Paul

I encountered a similar problem, but did not want to modify the versionName to include the git hash. We wanted to keep that as something like 1.2.2, but still have the possibility of displaying the git hash in the UI.

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

I modified the code from the other answer hereto use the buildConfigField task to generate a BuildConfig.GitHash value that can be referenced in the Java code.

我修改了此处其他答案中的代码,以使用 buildConfigField 任务生成可在 Java 代码中引用的 BuildConfig.GitHash 值。

Add this above the androidsection of your module's build.gradle file:

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

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

Then add the following line to the defaultConfigsection of the androidsection of the build.gradle, i.e. below versionName:

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

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

This generates the following line in the auto-generated BuildConfig.java file:

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

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

Now you can get the git hash in your Java code with BuildConfig.GitHash.

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

回答by Adrián Moreno

One ideal solution could be grabbing the version from the git status of the project. This way the versioning does not rely on you remembering to increase a variable, or change any text in the gradle or config files. Another advantage is the traceability of the version name and code to one specific code status.

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

You can find one descriptive example in http://ryanharter.com/blog/2013/07/30/automatic-versioning-with-git-and-gradle/

您可以在http://ryanharter.com/blog/2013/07/30/automatic-versioning-with-git-and-gradle/ 中找到一个描述性示例

The idea is getting the git info with the getVersionName function, and use that function in the gradle script:

这个想法是使用 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()
}

Of course, you need to have command line git available (as the command git describe --tagswill be executed to generate the information).

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

Another approach (based also in getting the version info from git) could be externalizing that logic to a gradle plugin - such as: - https://github.com/moallemi/gradle-advanced-build-version- https://github.com/infusionsoft/gradle-build-version-plugin- https://github.com/nemerosa/versioning

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

The one to use will depend on which kind of versioning policy you want to apply.

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

回答by lessthanoptimal

I created a Gradle plugin to do this for you. The project and full instructions are at https://github.com/lessthanoptimal/gversion-plugin

我创建了一个 Gradle 插件来为你做这件事。项目和完整说明位于https://github.com/lessthanoptimal/gversion-plugin

To use it add the following to your build.gradle file

要使用它,请将以下内容添加到您的 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
}

Now you just need to run the gradle task 'createVersionFile' to create the file. You might want to consider adding the following line to your gradle project project.compileJava.dependsOn(createVersionFile)This will cause it to generate the file every time Gradle builds the project. See the website above for Android instructions.

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

Here's what the file looks like

这是文件的样子

/**
 * 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;
}

You might also want to add the version file to your .gitignore since it is autogenerated and you don't want it in git.

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

回答by javabrett

Also worth looking at grgit - Groovy/Gradle Git, which can help simplify extraction of information, including Git commit-hashes, in a Gradle script.

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

回答by Charlie Lee

add following code to your build.gradle

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

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

now you can get git hash by BuildConfig.GIT_HASH

现在你可以得到 git hash BuildConfig.GIT_HASH

have fun

玩得开心