Before AGP 7.0.0-alpha15 I could change version code of an app for example like that
android {
defaultConfig {
applicationVariants.all {
if (buildType.name == "debug") {
outputs.forEach { output ->
(output as? com.android.build.gradle.internal.api.ApkVariantOutputImpl)?.versionCodeOverride = 1
}
}
}
}
}
applicationVariants
seems missing after AGP 7.0.0-alpha15, how to change it?
PS: It seems ok in plain gradle, above is Kotlin
Edit
With answer from below I was able to override version code in build:
android {
androidComponents.onVariants { appVariant ->
if (appVariant.buildType == "release") {
appVariant.outputs.forEach {
it.versionCode.set(1)
}
}
}
}
7.2.2 (August 2022) Issue #231037948: AGP 7.2.
The Variant API is going to change to a lazily-evaluated model in AGP 7.0.0, and it seems like Alpha 15 has removed the old APIs for this now. Going forward, you will need to use the androidComponents
DSL, which gives access to variants. I am not sure if you'll be able to rewrite the version code like this, however. Check out beforeVariants
and onVariants
for more info:
android {
androidComponents.beforeVariants { variantBuilder ->
// Callback before variants are built. Can be modified, but doesn't allow access to outputs
}
androidComponents.onVariants { variant ->
// Callback after variants are built. Apparently it's read-only access at this point, but outputs are available here
println(variant.outputs)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With