android studio 生成的apk有版本,有时间标识

本文介绍如何在Gradle构建脚本中正确配置Android应用的版本号,避免使用已废弃的API导致的问题,并展示了如何生成包含日期的时间戳版本号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android {
  ...
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                outputFileName =             
   defaultConfig.applicationId.subSequence(defaultConfig.applicationId.lastIndexOf(".") 
   + 1, defaultConfig.applicationId.length()) + "-v" + defaultConfig.versionCode + "-" + 
   releaseTime() + ".apk"
            }
        }
    }
 ...
}
def releaseTime() {
    return new Date().format("yyyyMMdd_HHmmss", TimeZone.getTimeZone("GMT+8:00"))
}

//新版的gradle会有以下报错

WARNING: API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
To determine what is calling variantOutput.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information.
Affected Modules: app

没有什么影响,参考:https://blog.youkuaiyun.com/yuzhiqiang_1993/article/details/86576718

但是生成的版本号会是null,例如:vnull

解决办法,将 defaultConfig 修改为 varian

参考:https://www.cnblogs.com/blogs-of-lxl/p/10306145.html

 

最终版本:

android.applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = defaultConfig.applicationId.subSequence(defaultConfig.applicationId.lastIndexOf(".")
                    + 1, defaultConfig.applicationId.length()) + "-v${variant.versionName}-${releaseTime()}.apk"
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值