zoukankan      html  css  js  c++  java
  • gradle 及 git 环境下利用hook及gradle脚本自动添加versioncode和versionname的方法

    在 app/build.gradle 文件里添加几行代码:

    def gitCommitShortHash = 'git log -1 --pretty=%h'.execute([], project.rootDir).text.trim()
    def gitCommitDate = 'git log -1 --pretty=%ci'.execute([], project.rootDir).text.trim().split()[0]
    def gitShortShaDate = gitCommitShortHash + "_" + gitCommitDate
    
    // Auto-incrementing commit count based on counting commits to HEAD (Build #543)
    def gitCommitCount = Integer.parseInt('git rev-list HEAD --count'.execute([], project.rootDir).text.trim())
    
    // I want to use git tags as my version names (1.2.2)
    def gitCurrentTag = 'git describe --tags --abbrev=0'.execute([], project.rootDir).text.trim()

    加完之后整个build.gradle文件像这个样子(注意其中的 versionCode gitCommitCount  和 versionName gitShortShaDate):

    apply plugin: 'com.android.application'
    
    def gitCommitShortHash = 'git log -1 --pretty=%h'.execute([], project.rootDir).text.trim()
    def gitCommitDate = 'git log -1 --pretty=%ci'.execute([], project.rootDir).text.trim().split()[0]
    def gitShortShaDate = gitCommitShortHash + "_" + gitCommitDate
    
    // Auto-incrementing commit count based on counting commits to HEAD (Build #543)
    def gitCommitCount = Integer.parseInt('git rev-list HEAD --count'.execute([], project.rootDir).text.trim())
    
    // I want to use git tags as my version names (1.2.2)
    def gitCurrentTag = 'git describe --tags --abbrev=0'.execute([], project.rootDir).text.trim()
    
    android {
        compileSdkVersion 24
        buildToolsVersion "24.0.3"
        defaultConfig {
            applicationId "com.welhzh.android.myapplication"
            minSdkVersion 11
            targetSdkVersion 24
            versionCode gitCommitCount
            versionName gitShortShaDate
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:24.2.1'
        testCompile 'junit:junit:4.12'
    }

    附:

    git的 post-commit 类型 hook,post-commit 类型hook创建方法:1、这项目的主目录里创建一个hooks目录,在该目录里放入一个文件 post-commit,改属性:  chmod a+x post-commit,

    然后将 .git/hooks 目录删除,将自己创建的hooks链接到 .git/ 目录里,进入 .git 目录执行:ln  -s  ../.hooks/  hooks,为啥要这么做,因为.git/ 目录下的所有文件是没法版本管理的,所以创建到工程目录以便加入版本管理。以后所有hook都放在该目录,且该目录不要忽略(不要放进.gitignore)。

    该 post-commit 目录的内容:

    #!/bin/bash
    commit=$(git log -1 --pretty=%H%n%ci)
    commit_full_hash=$(echo "$commit" | head -1)       # c7618bf23a71637c54b5e51c37cbace3f6ff4899
    # commit_full_hash=$(git log -1 --pretty=%H)     # same as above
    # commit_full_date=$(echo "$commit" | head -2 | tail -1)    # 2010-12-28 05:16:23 +0300
    commit_short_hash=$(git log -1 --pretty=%h)      # c7618bf
    
    commit_date=$(git log -1 --pretty=%ci 2>/dev/null | cut -d" " -f 1)     # 2016-09-12
    commit_time=$(git log -1 --pretty="%ci" 2>/dev/null | cut -d" " -f 2)   # 15:12:21
    
    branch_full=$(git symbolic-ref HEAD 2>/dev/null)  # refs/heads/master
    branch_simple=$(git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3)   # master
    # branch_simple=$(git symbolic-ref --short -q HEAD)
    # branch_simple=$(git name-rev --name-only HEAD)
    # branch_simple=$(git symbolic-ref --short -q HEAD)
    
    echo "print by hzh --------"
    echo "$commit_full_hash"
    echo "$commit_short_hash"
    echo "$commit_date"
    echo "$branch_full"
    echo "$branch_simple"
    echo "print by hzh end --------
    "
    
    # 当然,你可以将这些东西写到version文件里,然后编译的时候读取它,以下只是示例
    versionfilename=$(git config hooks.versionfilename)
    if [[ -z $versionfilename ]]
    then
      versionfilename="version"
    fi
    
    # Version number
    #echo # Generated using git post-commit hook > $versionfilename
    echo -n "$commit_time" > $versionfilename
    echo -n " " >> $versionfilename
    echo -n "$commit_date" >> $versionfilename
    echo -n " " >> $versionfilename
    echo -n "$commit_short_hash" >> $versionfilename
    echo -n " " >> $versionfilename
    echo -n "$branch_simple" >> $versionfilename
  • 相关阅读:
    shell数组
    Apache HTTP Server 与 Tomcat 的三种连接方式介绍
    实现Java动态类载入机制
    Tomcat 阀
    MYSQL 常用命令
    MYSQL字符数字转换
    主题:MySQL数据库操作实战
    日本手机三大代理商的UA
    Java解析XML文档——dom解析xml (转载)
    MS sql server和mysql中update多条数据的例子
  • 原文地址:https://www.cnblogs.com/welhzh/p/5983211.html
Copyright © 2011-2022 走看看