美文网首页Gradle构建
Gradle-App关联git提交次数和分支

Gradle-App关联git提交次数和分支

作者: 未见哥哥 | 来源:发表于2019-09-27 19:56 被阅读0次

打包分支是 Mater 分支,所以 APP 的版本号就可以设置为当前分支的提交次数,并且为了方便,需要将分支名显示在设置页面上,如下图所示:

build

设置 APP 版本号为 git 提交的次数

  • git 命令
git rev-list --count HEAD
  • java 实现 git 命令的执行
public static int getGitReVersion() {
    int gitReversion = -1;
    try {
        StringBuffer buffer = new StringBuffer();
        Process exec = Runtime.getRuntime().exec("git rev-list --count HEAD");
        InputStream inputStream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        char[] chars = new char[1024];
        int len;
        while ((len = reader.read(chars)) != -1) {
            buffer.append(chars, 0, len);
        }
        reader.close();
        inputStream.close();
        //注意:这里读取出来的数据需要 trim() 不然会有一个 \n 的换行符
        gitReversion = Integer.valueOf(buffer.toString().trim());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return gitReversion;
}
  • groovy 脚本

给 versionCode 设置为 git 的提交次数

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.git"
        minSdkVersion 21
        targetSdkVersion 28
        //设置版本号为 getReversion() 
        versionCode getReversion()
        versionName "1.0.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
}
def getReversion() {
    def buildnum = -1
    try {
        //底层使用的是 Runtime.getRuntime().exec()去执行
        def getGit = 'git rev-list --count HEAD'.execute()
        def gitVersion = getGit.text.trim().toInteger()
        buildnum = buildnum + gitVersion
    } catch (e) {
        e.printStackTrace()
    }
    return buildnum
}

获取当前打包分支的分支名

  • git 命令
git symbolic-ref --short -q HEAD
  • java 实现
public static String getBranchName() {
    String branchName = null;
    try {
        StringBuffer buffer = new StringBuffer();
        Process exec = Runtime.getRuntime().exec("git symbolic-ref --short -q HEAD")
        InputStream inputStream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        char[] chars = new char[1024];
        int len;
        while ((len = reader.read(chars)) != -1) {
            buffer.append(chars, 0, len);
        }
        reader.close();
        inputStream.close();
        branchName = buffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return branchName;
}
  • groovy 脚本
String getBranchName() {
    def branchName = null;
    try {
        /**
         * .execute()
         * .getText()
         * 以上两个方法都是扩展方法
         */
        def inputstream = "git symbolic-ref --short -q HEAD".execute()
        //这里记得 trim() 不然会有换行符
        branchName = inputstream.getText().trim()
    } catch (IOException e) {
        e.printStackTrace();
    }
    println "getBranchName():" + branchName
    return branchName;
}

给 BuildConfig 设置一个属性参数获取分支名

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        buildConfigField "String", "BUILD_BRANCE", "\"" + getBranchName() + "\""
    }
    debug {
        //如果不单独加上引号会出现这种问题,到时运行失败
        //public static final String BUILD_BRANCE = master;
        //注意这里必须要单独加上双引号
        //public static final String BUILD_BRANCE = "master";
        buildConfigField "String", "BUILD_BRANCE", "\"" + getBranchName() + "\""
    }
}

设置之后就可以在 BuildConfig 中看到 BUILD_BRANCE 属性置了。

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.git";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 37;
  public static final String VERSION_NAME = "1.0.0";
  // Fields from build type: debug
  public static final String BUILD_BRANCE = "master";
}

相关文章

  • Gradle-App关联git提交次数和分支

    打包分支是 Mater 分支,所以 APP 的版本号就可以设置为当前分支的提交次数,并且为了方便,需要将分支名显示...

  • 本地项目关联远程Git仓库

    步骤 本地项目初始化 git 关联远程仓库 提交暂存 提交到分支 推送 移除 git 常见问题 如何配置邮箱和用户...

  • Git远程分支

    Git远程分支  Git的分支分为:本地分支,本地远程分支和远程分支。 对分支的操作基本包括:创建、关联和删除。 ...

  • git 命令总结

    创建 分支 git 重新关联远程地址 查看修改 查看提交历史 创建ssh key 远程仓库 标签管理 修改提交me...

  • 【Gradle Task开发】FIR上传脚本(cURL方式、关联

    说明: 1、x:version参数使用了git rev-list --count HEAD获取当前分支提交次数,方...

  • git合并分支和提交步骤

    git 合并分支和提交的步骤 Git 问题 :一个 master, 多个新功能分支, 怎样有序地合并和提交(ht...

  • git提示Can't update(master has no

    原因:本地分支和远程分支没有关联,需要关联远程分支解决方案:git push --set-upstream ori...

  • git管理工具

    git分支命令 git提交命令

  • git使用tips

    创建本地分支:git checkout -b 分支名创建本地分支并和远程分支关联:git checkout -b ...

  • 创建私有库常见git命令

    利用cocoapods一键创建私有库 创建之后关联远程仓库 查看远程分支 关联本地分支和远程分支git branc...

网友评论

    本文标题:Gradle-App关联git提交次数和分支

    本文链接:https://www.haomeiwen.com/subject/xuvauctx.html