gpt4 book ai didi

android - 来自带有 gradle 插件 3 的文件的版本名称和代码

转载 作者:搜寻专家 更新时间:2023-11-01 07:44:29 25 4
gpt4 key购买 nike

我的 android build设置为使用 Release模式文件中的 versionNameversionCode。在不创建发布构建以保持增量构建工作时,它们被设置为静态值。

gradle文件的相关部分是:

android {
defaultConfig {
versionCode 1
versionName "1.0"
// SNIP...
}

applicationVariants.all { variant ->
if (variant.buildType.name == "release") {
variant.versionCode = file('version-code.txt').text as int
variant.versionName = file('version.txt').text
}
}

// SNIP ...
}

版本文件的示例内容可以是:

  • version.txt:0.7
  • version-code.txt:7

这是通过遵循 Use static build config values with your debug build 完成的保持增量构建工作的推荐指南部分。

For example, using dynamic version codes, version names, resources, or any other build logic that changes the manifest file requires a full APK build every time you want to run a change—even though the actual change might otherwise require only a hot swap. If your build configuration requires such dynamic properties, then isolate them to your release build variants and keep the values static for your debug builds, as shown in the build.gradle file below.

但是,我们发现自从升级到 gradle 插件的第 3 版后,这个问题就不再有效了。 Modifying variant outputs at build time may not work gradle 插件 3.0.0 迁移指南的部分说:

Using the Variant API to manipulate variant outputs is broken with the new plugin. It still works for simple tasks, such as changing the APK name during build time, as shown below:

However, more complicated tasks that involve accessing outputFile objects no longer work. That's because variant-specific tasks are no longer created during the configuration stage. This results in the plugin not knowing all of its outputs up front, but it also means faster configuration times.

迁移指南中似乎没有推荐任何替代方案。还有其他方法可以实现吗?

更新

感谢@nhoxbypass 的回答,将我的 gradle 文件更改为包含以下内容让一切重新开始:

applicationVariants.all { variant ->
if (variant.buildType.name == "release") {
variant.outputs.all { output ->
output.setVersionNameOverride(file('version.txt').text)
output.setVersionCodeOverride(file('version-code.txt').text as int)
}
}
}

最佳答案

migration guide 仍然适用于 简单的任务,例如在构建期间更改 APK 名称(至少适用于我的项目)。但是,涉及访问 outputFile 对象的更复杂的任务不再有效。

但是如果您需要尝试一种解决方法,在 3.0 版本之前已经存在一个解决方法,如果有人正在寻找解决方案,您可以使用:

output.setVersionCodeOverride(Integer.parseInt(buildTimeSmall()))

参见:Unable to change project versionCode for different build types

关于android - 来自带有 gradle 插件 3 的文件的版本名称和代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47553086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com