在更新到AGP 3.2.0之后,BuildConfig.VersionCode不能反映实际的versionCode [英] BuildConfig.VersionCode is not reflecting the actual versionCode after updating to AGP 3.2.0

查看:868
本文介绍了在更新到AGP 3.2.0之后,BuildConfig.VersionCode不能反映实际的versionCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新到AGP(Android Gradle插件)3.2.0后,我们无法直接在mergedFlavor上设置versionCode.如果这样做,我们会收到以下有用警告:

versionCode cannot be set on a mergedFlavor directly.
versionCodeOverride can instead be set for variant outputs using the following syntax:
android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.versionCodeOverride = 40805
        }
    }
}

此更改后,除一件小事情外,其他所有东西都工作正常.自动生成的BuildConfig.VERSION_CODE不能反映output.versionCodeOverride = 40805中的版本代码.

在AGP 3.2.0之前,我们可以通过以下方式动态设置versionCode:

applicationVariants.all { v ->
        v.mergedFlavor.versionCode = 40805 // 40805 is hardcoded as an example but it is archived dynamically.
    }

BuildConfig.VERSION_CODE中反映的版本代码(这非常方便),我想将其与AGP 3.2.0一起存档.

我知道我可以通过为此创建一个自定义生成配置字段(如variant.buildConfigField('int', 'OVERRIDDEN_VERSION_CODE', "${versionCodeOverride}"))来解决此问题,这将生成带有我覆盖的versionCode的BuildConfig.OVERRIDDEN_VERSION_CODE.通过设置versionCodemergedFlavor.versionCode = 40805来存档与使用AGP 3.2.0以下版本时的存档相同的文件,但是我不喜欢这种解决方法.

有什么方法可以使output.versionCodeOverride = 40805反映在自动生成的BuildConfig.VERSION_CODE中?

PS:如果我们直接将versionCode设置为特定的风味,它将按预期工作,但这不是我想知道的:)

更新

发现了一个类似的问题(使用案例得到了很好的描述),考虑到我们的讨论,我可以给出一个更好的答案解决方案

总而言之,问题不是根本没有应用版本代码替代,只是BuildConfig.VERSION_CODE没有选择替代值./p>

此问题已在官方问题跟踪器中标记为预期的行为: https://issuetracker.google. com/issues/37008496

其中一条评论解释了原因,并建议以一种风味而非defaultConfig的方式定义versionCode:

如果我们为每个输出创建一个不同的buildconfig.java,那么我们还需要为每个拆分运行javac/proguard/jacoco/dex,并且我们会失去构建[时间]上的改进.

如果这对您很重要,则不要使用拆分,而要使用口味,但是我们会大大缩短构建时间.

如果您不想更改当前的设置,则可能需要从清单中读取版本代码.您只需要一个上下文:

val versionCode = context.packageManager.getPackageInfo(context.packageName, 0).versionCode

您应该缓存该值,因为获取软件包信息是一项不重要的操作.

版本名称也是如此.

After updating to AGP(Android Gradle Plugin) 3.2.0 we can't set versionCode directly on a mergedFlavor. If we do so we get this useful warning:

versionCode cannot be set on a mergedFlavor directly.
versionCodeOverride can instead be set for variant outputs using the following syntax:
android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.versionCodeOverride = 40805
        }
    }
}

After this change everything works fine besides one little thing. The auto-generated BuildConfig.VERSION_CODE doesn't reflect the version code from output.versionCodeOverride = 40805.

Before AGP 3.2.0 we could set the versionCode dynamically through:

applicationVariants.all { v ->
        v.mergedFlavor.versionCode = 40805 // 40805 is hardcoded as an example but it is archived dynamically.
    }

And the version code reflected in BuildConfig.VERSION_CODE (this is very handy) and I would like to archive the same with AGP 3.2.0.

I know I could workaround this by creating a custom build config field for this like variant.buildConfigField('int', 'OVERRIDDEN_VERSION_CODE', "${versionCodeOverride}") and this will generate the BuildConfig.OVERRIDDEN_VERSION_CODE with the versionCode I override. Archiving the same as I was when using the AGP version bellow 3.2.0 by setting the versionCode through mergedFlavor.versionCode = 40805 but I don't like this kind of workarounds.

Is there any way to have the output.versionCodeOverride = 40805 being reflecting in the auto-generated BuildConfig.VERSION_CODE?

PS: If we set the versionCode directly in the specific flavor it will work as expected but that's not what I want to know :)

UPDATE

Found a similar question(with a well described use case) and considering the discussions we had I could give a better answer here.

解决方案

To recap, the problem isn't the version code override not being applied at all, it's just that BuildConfig.VERSION_CODE does not pick up the override value.

This has been labeled as intended behavior in the official issue tracker: https://issuetracker.google.com/issues/37008496

One of the comments explains why and suggests defining versionCode in a flavor instead of the defaultConfig:

If we made a different buildconfig.java per output then we'd also need to run javac/proguard/jacoco/dex for each split and we'd lose the improvement in build [time].

If this is critical to you, then don't use splits and use flavors instead, but we'll get much slower build time.

If you don't want to alter your curent setup you might want to read the version code from the manifest instead. All you need is a context:

val versionCode = context.packageManager.getPackageInfo(context.packageName, 0).versionCode

You should cache the value as getting package info is a non-trivial operation.

The same applies to version name.

这篇关于在更新到AGP 3.2.0之后,BuildConfig.VersionCode不能反映实际的versionCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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