如何使用 gradle 在 APK 文件名中设置 versionName?

编程入门 行业动态 更新时间:2024-10-23 12:29:20
本文介绍了如何使用 gradle 在 APK 文件名中设置 versionName?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 gradle 自动生成的 APK 文件名中设置特定的版本号.

I'm trying to set a specific version number in the gradle auto-generated APK filename.

现在 gradle 生成 myapp-release.apk 但我希望它看起来像 myapp-release-1.0.apk.

Now gradle generates myapp-release.apk but I want it to look something like myapp-release-1.0.apk.

我试过重命名看起来很乱的选项.有没有简单的方法可以做到这一点?

I have tried renaming options that seems messy. Is there a simple way to do this?

buildTypes { release { signingConfig signingConfigs.release applicationVariants.each { variant -> def file = variant.outputFile variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk")) } }

我已经尝试了上面的代码,但没有成功.有什么建议?(使用 gradle 1.6)

I have tried the code above with no luck. Any suggestions? (using gradle 1.6)

推荐答案

这解决了我的问题:使用 applicationVariants.all 而不是 applicationVariants.each

This solved my problem: using applicationVariants.all instead of applicationVariants.each

buildTypes { release { signingConfig signingConfigs.release applicationVariants.all { variant -> def file = variant.outputFile variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk")) } } }

更新:

所以这似乎不适用于 0.14+ 版本的 android studio gradle 插件.

Update:

So it seems this does not work with 0.14+ versions of android studio gradle plugin.

这可以解决问题(参考来自这个 问题) :

This does the trick (Reference from this question ) :

android { applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File( output.outputFile.parent, output.outputFile.name.replace(".apk", "-${variant.versionName}.apk")) } } }

更多推荐

如何使用 gradle 在 APK 文件名中设置 versionName?

本文发布于:2023-10-31 19:01:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件名   如何使用   APK   gradle   versionName

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!