动态versioncode,在更新到AGP 3.2.0之后,BuildConfig.VersionCode不能反映实际的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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值