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

标签 android gradle android-gradle-plugin gradle-plugin

我的 android 版本设置为在 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 ...
}

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

  • 版本.txt:0.7
  • 版本代码.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/

相关文章:

java - Toast 未显示 - 用 genymotion 模拟

java - 单击 ListView 项目时在 Android Activity 之间切换

android - 如何在每个 Activity 或 fragment 的底部显示 exoplayer 的迷你 Controller ?

android - Jenkins : ionic 安卓构建失败

android - 从build.gradle读取元数据

android - 无法解析 : junit:junit:4. 12

android - 使布局不可点击

Gradle:无法存储任务输入属性。无法序列化值为 'myInput' 的属性 '[null]'

android - Gradle SwingBuilder: “Toolkit not found: apple.awt.CToolkit”错误

android - 异常 : Unsupported Android Plugin version: 4. 2.1。当 flutter 运行时--release