sheldon_blogs

Android: android studio配置生成自定义apk名称

1.Android Studio 3.0之前:

 在build.gradled 的 android {} 内添加如下代码:

复制代码
android.applicationVariants.all { variant ->  //如果是Library,例如aar包则使用 android.libraryVariants.all 且下面后缀判断修改为 .arr
        variant.outputs.each { output ->
 def fileName = "demo_${defaultConfig.versionCode}-${defaultConfig.versionName}-${releaseTime()}.apk" def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { //这里修改apk文件名 output.outputFile = new File(outputFile.parent, fileName)  
       
}
    }
}
复制代码

 在 android {} 之外声明 releaseTime() 方法:

//获取编译时间
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

 

2.Android Studio 3.0之后:

 outputFile变为只读,需把each修改为all,然后通过outputFileName修改生成apk的名称,此外AS 3.0后打包完,除了apk包文件,还会多一个 output.json 参数文件。

    新版修改如下:

复制代码
    //自定义apk名称含版本号信息
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->  //旧版为:each
            def fileName = "demo_${defaultConfig.versionCode}-${defaultConfig.versionName}-${releaseTime()}.apk"
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                //这里修改apk文件名
                outputFileName = fileName  //旧版为:output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }
复制代码

 同样在 android {} 之外声明 releaseTime() 方法:

//获取编译时间
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

 

-end-

 

posted on   sheldon_blogs  阅读(939)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示