在 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()[]
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()[]
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
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.welhzh.android.myapplication"
minSdkVersion
targetSdkVersion
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 - --pretty=%H%n%ci)
commit_full_hash=$(echo "$commit" | head -) # c7618bf23a71637c54b5e51c37cbace3f6ff4899
# commit_full_hash=$(git log - --pretty=%H) # same as above
# commit_full_date=$(echo "$commit" | head - | tail -) # -- :: +
commit_short_hash=$(git log - --pretty=%h) # c7618bf commit_date=$(git log - --pretty=%ci >/dev/null | cut -d" " -f ) # --
commit_time=$(git log - --pretty="%ci" >/dev/null | cut -d" " -f ) # :: branch_full=$(git symbolic-ref HEAD >/dev/null) # refs/heads/master
branch_simple=$(git symbolic-ref HEAD >/dev/null | cut -d"/" -f ) # 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 --------\n" # 当然,你可以将这些东西写到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

gradle 及 git 环境下利用hook及gradle脚本自动添加versioncode和versionname的方法的更多相关文章

  1. Windows环境下利用github快速配置git环境

    在windows环境下利用github客户端我们可以直接拥有可视化的界面来管理工程,当然你也可以选择你喜欢的命令行工具来做.今天我分享一个比较快速的方式来配置git环境. 先去下载github的win ...

  2. 痞子衡嵌入式:在IAR开发环境下将关键函数重定向到RAM中执行的三种方法

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在IAR开发环境下将关键函数重定向到RAM中执行的三种方法. 嵌入式项目里应用程序代码正常是放在 Flash 中执行的,但有时候也需要将 ...

  3. 痞子衡嵌入式:在MDK开发环境下将关键函数重定向到RAM中执行的几种方法

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在MDK开发环境下将关键函数重定向到RAM中执行的几种方法. 这个关键函数重定向到 RAM 中执行系列文章,痞子衡已经写过 <IA ...

  4. 一步步教你搭建VS环境下用C#写WebDriver脚本

    一步步教你搭建VS环境下用C#写WebDriver脚本http://www.automationqa.com/forum.php?mod=viewthread&tid=3529&fro ...

  5. 20181225-Linux Shell Bash环境下自动化创建ssh互信脚本

    20181225-Linux Shell Bash环境下自动化创建ssh互信脚本 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba/ 个人网站 http: ...

  6. Window环境下,PHP调用Python脚本

    参考 php调用python脚本*** php 调用 python脚本的方法 解决办法:php提供了许多调用其他脚本或程序的方法,比如exec/system/popen/proc_open/passt ...

  7. windows环境下利用Gitblit搭建Git服务器并实现自动部署Web站点目录

    Git服务搭建多见于linux环境,但windows主机也不少,目前网上文章诸多不全,且以讹传讹,不甚清楚.下面介绍windows环境下的自动部署和发布. 所需环境及资源:Java环境.Gitblit ...

  8. 在ctex环境下利用Metapost作图

    使用Metapost作图,是LaTeX的好搭档.下面介绍如何在ctex环境下的使用Metapost作图. 首先新建一个test.mp的Metapost文件. 在文件开始需要声明如下代码: prolog ...

  9. 断网环境下利用pip安装Python离线安装包

    这几天搞Windows离线断网环境下安装Python包,配置环境,各种坑!做个记录,供以后查询吧. # 生产环境 windows 7 # python 2.7.9 # pip 1.5.2 友情提示:当 ...

随机推荐

  1. Volley HTTP库系列教程(3)自定义RequestQueue和编写单例RequestQueue示例

    Setting Up a RequestQueue Previous  Next This lesson teaches you to Set Up a Network and Cache Use a ...

  2. wget 批量下载目录文件

    wget -r -p -k -np http://源目录     ./本地目标目录

  3. asp.net页面过滤所有换行符和多余空格

    不知道大家注意到了没有,Google和Baidu网页的HTML源代码是混合在一起的.HTML代码混合在一起,出发点是为了减小网页体积,从而加快网页加载速度. 写个函数把网页HTML源代码的换行符和空格 ...

  4. 【温故知新】C#委托delegate

    在c#的学习过程中,学到委托与事件总会迷糊一段时间,迷糊过后自然而就似懂非懂了~,所以最近我打算把以前所学的迷糊过的知识总结,温故知新,总结记录下来. 首先,我们来看一下msdn对委托的定义: del ...

  5. Spring+Hibernate配置多数据源

    配置说明 在实际应用中,经常会用到读写分离,这里就这种情况进行Spring+Hibernate的多数据源配置.此处的配置只是让读的方法操作一个数据库,写的方法操作另外一个数据库. 注:我这里的配置JD ...

  6. 如何扩展VCL的hint

    默认的Hint窗口展现如下: 这种情况下可以操作有窗口的背景颜色,字体样式 Application.Color 有的时候仅仅是文字满足不了我们的需求,比例如下格式: 这个时候就应该执行以下步骤: 1. ...

  7. [ionic开源项目教程] - 第5讲 如何在项目中使用全局配置

    第5讲 如何在项目中使用全局配置? Q:ionic开发,说纯粹一点,用的就是html+css+js,那么无疑跟web开发的方式是类似的.在这里给大家分享一个小技巧,如何在项目中使用全局配置? A:我的 ...

  8. UVa 1225 Digit Counting

    题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...

  9. AIX 第7章 指令记录

    要点: AIX文件系统的访问路径 AIX文件系统目录树 创建AIX文件系统 文件系统的卸载和删除 文件系统的自动挂载 文件系统的容量管理 文件系统的一致性管理 文件系统的卸载失败 文件系统的快照管理 ...

  10. java-No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable

    功能:读配置文件 java菜鸟:导入工程在报名处就开始报错,第一次遇到 import org.apache.commons.lang3.StringUtils; import org.apache.c ...