Embed git commit information in version number Versionname when building Android with Gradle

Source: Internet
Author: User

Why did you do this?

?? The version number does not quickly navigate to the corresponding code during the version iteration of the application development, causing trouble when the problem is traced back to the corresponding version of the code.
?? If the code is managed by git, information such as GIT's commit ID can be quickly targeted to the response code. If you can automatically embed the commit ID into the versionname when building the application, it will be helpful to follow up the traceability.
?? The following is an introduction to the main Gradle building tools.

A workable approach
    1. You can call the git command via Gradle to get the current warehouse status and stitch it into versionname.
      such as by git describe --always acquiring origin/develop.2.0.1017-2-g0327583 a shape (with tag presence) or 0327583 (no tag exists)
      or by git rev-parse HEAD acquiring a shape like 90312cd9157587d11779ed7be776e3220050b308 , or by git rev-parse --short HEAD acquiring a short 90312cd9 .
    2. Method 1 is convenient to use, but depends on the build environment that has git and is fully configured, and the fact that the command-line compile OK may appear on Windows, but fails in Android studio. So the method described here is to assemble the Git commit ID information into versionname in the code warehouse relative path.
Step description extract git commit ID

Gradle script to extract git commit ID

gradle.allprojects {    ext.getGitHeadRefsSuffix = {        try {            // .git/HEAD描述当前目录所指向的分支信息,内容示例:"ref: refs/heads/master\n"            def headFile = new File(‘.git/HEAD‘)            if (headFile.exists()) {                String[] strings = headFile.getText(‘UTF-8‘).split(" ");                if (strings.size() > 1) {                    String refFilePath = ‘.git/‘ + strings[1];                                        // 根据HEAD读取当前指向的hash值,路径示例为:".git/refs/heads/master"                    def refFile = new File(refFilePath.replace("\n", ""));                    // 索引文件内容为hash值+"\n",                    // 示例:"90312cd9157587d11779ed7be776e3220050b308\n"                    return "_" + refFile.getText(‘UTF-8‘).substring(0, 7)                }            }        } catch (Exception e) {            e.printStackTrace()        }        return ""    }}

Save the above in a common common.gradle script for later use.

Import in Project

Import Build.gradle in Project

apply from: ‘common.gradle‘
Module reference

Referencing defined methods in module as needed

android {    ....    defaultConfig {        ....        versionName "2.0_" + getGitHeadRefsSuffix()        ....    }    ....}
Inspection

Build the version information that confirms the app after installation, such as:

....versionName=2.0_14e1535....

If you need to track the corresponding version of the code, you can directly in the GIT repository

git checkout 14e1535 [-b $branchname]



Reprint Link: http://www.jianshu.com/p/582939dfd73e

Embed git commit information in version number Versionname when building Android with Gradle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.