如何使用 gradle 在 APK 文件名中设置 versionName? [英] How to set versionName in APK filename using gradle?

查看:48
本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆