Android 配置gradle实现VersionCode自增实例
作者:Ikulm 时间:2021-07-03 12:39:23
我是因为构建多渠道包的时候有这个需求,平常工作多个渠道包频繁的打包,总会忘记versioncode提高一下,从而打包出来的apk无法覆盖原先的安装包,这里只介绍了自增VersionCode,网上还有动态设置versionName,有需要也可以看看其他人的实现。
以下介绍下配置Gradle自动增加versionCode的值
一、项目的根目录新建一个文件version.properties
二、文件内容写上初始的versioncode
三、主gradle配置
def getVersionCode() {
def versionFile = file('version.properties')
if (versionFile.canRead()) {
def Properties versionProps = new Properties()
versionProps.load(new FileInputStream(versionFile))
def versionCode = versionProps['VERSION_CODE'].toInteger()
def runTasks = gradle.startParameter.taskNames //仅在assembleRelease任务是增加版本号,其他渠道包在此分别配置
if ('assembleInsectRelease' in runTasks) {
versionProps['VERSION_CODE'] = (++versionCode).toString()
versionProps.store(versionFile.newWriter(), null)
}
return versionCode
} else {
throw new GradleException("Could not find version.properties!")
}
}
android {
....
}
只在release模式下才自增,保证测试的时候versionCode不增加
四、调用
def currentVersionCode = getVersionCode()
defaultConfig {
applicationId 'com.chongzzz.texas'
minSdkVersion 15
targetSdkVersion 25
multiDexEnabled true
versionCode currentVersionCode
versionName "1.0.1.21"
}
补充知识:Android 动态替换 Manifest.xml中的值
根据情况替换 Manifest.xml 中的值
android 中有很多个渠道,每个渠道都有 debug 和 release 版本, 如果Manifest.xml 中每个版本有不同的值
一般的可以用 manifestPlaceholders = [APP_KEY : '对应的值'] 来替换,在 debug和 release 的时候也可以,但是多渠道的时候可能就会有些问题了
解决方式
1、简单
两个渠道
flavorDimensions 'defaultDimension'
productFlavors {
alibaba { }
tencent { }
}
每个渠道的版本的不同的值
ext {
alibabaDebug = 'alibabaDebug'
alibabaRelease = 'alibabaRelease'
tencentDebug = 'tencentDebug'
tencentRelease = 'tencentRelease'
}
替换值
afterEvaluate {
project.android.applicationVariants.all { variant ->
// 最简单的方法
variant.mergedFlavor.manifestPlaceholders = [APP_KEY: project."${variant.name}"]
}
}
2、自定义任务,和上面的其实差不多
两个渠道
flavorDimensions 'defaultDimension'
productFlavors {
alibaba { }
tencent { }
}
每个渠道的版本的不同的值
ext {
alibabaDebug = 'alibabaDebug'
alibabaRelease = 'alibabaRelease'
tencentDebug = 'tencentDebug'
tencentRelease = 'tencentRelease'
}
替换值
afterEvaluate {
project.android.applicationVariants.all { variant ->
// 自定义任务
tasks.create("${variant.name}ManifestPlaceHolder", ManifestPlaceHolder) {
current = variant
}
variant.outputs.first().processManifest.dependsOn "${variant.name}ManifestPlaceHolder" }
}
class ManifestPlaceHolder extends DefaultTask {
@Input
def current
@TaskAction
def changeManifestPlaceHolder() {
current.mergedFlavor.manifestPlaceholders = [APP_KEY: project."${current.name}"]
}
}
来源:https://blog.csdn.net/Ikulm/article/details/79312174
标签:Android,gradle,VersionCode,自增
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
servlet异步请求的实现
2023-07-14 17:11:38
![](https://img.aspxhome.com/file/2023/4/57724_0s.png)
Spring Native项目实战(体验79毫秒启动springboot应用)
2022-03-14 22:18:42
![](https://img.aspxhome.com/file/2023/1/78361_0s.png)
Java Socket通信(一)之客户端程序 发送和接收数据
2023-07-05 00:34:48
java.text.DecimalFormat用法详解
2022-09-30 03:02:27
![](https://img.aspxhome.com/file/2023/9/61249_0s.jpg)
C#中Invoke的用法讲解
2023-10-26 03:24:36
Java静态代理和动态代理的深入讲解
2023-04-12 03:07:33
![](https://img.aspxhome.com/file/2023/0/101780_0s.png)
如何在MyBatis中实现DataSource
2021-11-29 13:49:48
![](https://img.aspxhome.com/file/2023/0/68560_0s.png)
实现一个基于Servlet的hello world程序详解步骤
2022-06-21 00:22:13
![](https://img.aspxhome.com/file/2023/9/75009_0s.png)
java 中clone()的使用方法
2023-03-31 12:11:37
C# DataTable.Select()根据条件筛选数据问题
2021-10-14 16:53:34
完整的医院就诊挂号系统基于Spring MVC + Spring + MyBatis实现
2022-06-03 18:07:05
![](https://img.aspxhome.com/file/2023/0/61400_0s.png)
java实现斗地主游戏
2022-02-18 14:06:26
![](https://img.aspxhome.com/file/2023/7/100657_0s.jpg)
XFire构建web service客户端的五种方式
2021-08-30 21:56:16
Java fastdfs客户端实现上传下载文件
2023-11-26 09:45:41
![](https://img.aspxhome.com/file/2023/0/59760_0s.png)
Java解析json报文实例解析
2023-07-18 21:33:58
如何清空文件夹里面的所有文件和文件夹
2022-09-17 04:11:06
Java实现简单GUI登录和注册界面
2022-09-02 06:36:21
![](https://img.aspxhome.com/file/2023/2/68202_0s.jpg)
springboot读取文件,打成jar包后访问不到的解决
2023-07-14 13:56:35
Java魔法堂之调用外部程序的方法
2023-11-09 07:14:16
![](https://img.aspxhome.com/file/2023/9/59209_0s.png)
win7中C#的winForm编程使用savefiledialog不能弹出保存窗体的解决方法
2022-09-18 07:27:57