利用gradle加入构建版本
2024-08-31 10:15:18
在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的文件。用于保存构建版本。
完毕之后,构建一下试试吧。
最新文章
- Intellij IDEA的一些东西
- RabbitHub开源情况及计划
- jdk版本及编译版本导致服务器部署UnsupportedClassVersionError错误
- JavaScript中设置元素class的三种方法小结
- 在RedHat/CentOS下安装Docker(不升级内核)
- can't run as root without the -u switch
- Spark 大数据平台 Introduction part 2 coding
- Generic repository pattern and Unit of work with Entity framework
- tap/click on search button on softkeyboard
- Crash的旅行计划
- js数组&;&;字符串&;&;定时器2
- javascript 里找元素操作元素
- vue项目基本流程
- Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较
- mayavi与X11的一些坑总结
- python----特殊闭包
- intest
- JAVA8的LocalDateTime使用心得和工具类
- yii 验证码功能的实现
- Calibre 3.4版中,为epub书籍嵌入中文字体
热门文章
- rest_framework-权限-总结完结篇
- 11.string容器
- BZOJ 3130 二分+网络流
- ASP.NET Identity 角色管理(Roles)
- vmware workstation虚拟机克隆后不能上网(桥接模式下)
- PostgreSQL Replication之第八章 与pgbouncer一起工作(1)
- iOS-RAC学习笔记(一)——RACStream
- Bootstrap modal.js 源码分析
- Threading and Tasks in Chrome
- caffe for python (官方翻译)