在java的程序中,貌似都没有这个构建版本的概念。用的诸如eclipse。 idea和android studio的IDE也没有直接提供构建版本的选项。只是我却想在android程序的版本其中加入一个构建版本,当然,这个仅仅是个人的蛋疼需求。尽管在stackoverflow上看到其它人也有这样的需求,但相信人不多。

这里分享一下我的解决方法。

首先在app的build.gradle中的android方法里面,把defaultConfig的代码替换成下面代码:

    def verName = "1.0.0"
def suffix = "(RC)" defaultConfig {
applicationId "cn.irains.access.v2"
minSdkVersion 14
targetSdkVersion 20
versionCode 5
versionName verName + suffix
} def versionPropsFile = file('version.properties') if (versionPropsFile.canRead()) {
def Properties versionProps = new Properties() versionProps.load(new FileInputStream(versionPropsFile)) def name = versionProps['VERSION_NAME'].toInteger() def runTasks = gradle.startParameter.taskNames
if ('b' in runTasks || 'build' in runTasks ) {
name++
} versionProps['VERSION_NAME']=name.toString()
versionProps.store(versionPropsFile.newWriter(), null)
defaultConfig {
versionName verName + "." + name + suffix
}
} android.applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}

上面的android.applicationVariant.all就是指定了新的apk的文件名称。前面的一大堆代码都是构建版本的逻辑。

由于是构建版本,所以先读取任务,仅在任务为b(build的缩写)或build的情况下,版本才加一。

然后我们还须要在build.gradle的同级文件夹下建立一个叫version.properties的文件。用于保存构建版本。

完毕之后,构建一下试试吧。

最新文章

  1. Intellij IDEA的一些东西
  2. RabbitHub开源情况及计划
  3. jdk版本及编译版本导致服务器部署UnsupportedClassVersionError错误
  4. JavaScript中设置元素class的三种方法小结
  5. 在RedHat/CentOS下安装Docker(不升级内核)
  6. can't run as root without the -u switch
  7. Spark 大数据平台 Introduction part 2 coding
  8. Generic repository pattern and Unit of work with Entity framework
  9. tap/click on search button on softkeyboard
  10. Crash的旅行计划
  11. js数组&&字符串&&定时器2
  12. javascript 里找元素操作元素
  13. vue项目基本流程
  14. Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较
  15. mayavi与X11的一些坑总结
  16. python----特殊闭包
  17. intest
  18. JAVA8的LocalDateTime使用心得和工具类
  19. yii 验证码功能的实现
  20. Calibre 3.4版中,为epub书籍嵌入中文字体

热门文章

  1. rest_framework-权限-总结完结篇
  2. 11.string容器
  3. BZOJ 3130 二分+网络流
  4. ASP.NET Identity 角色管理(Roles)
  5. vmware workstation虚拟机克隆后不能上网(桥接模式下)
  6. PostgreSQL Replication之第八章 与pgbouncer一起工作(1)
  7. iOS-RAC学习笔记(一)——RACStream
  8. Bootstrap modal.js 源码分析
  9. Threading and Tasks in Chrome
  10. caffe for python (官方翻译)