问题描述
我正在尝试在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!