Changing the APK name whenever AGP changes is tiresome work so I'm gonna jot down all the gradle versions and their way to change the APK name in it.
Fella android devs feel free to add more in the comments section. ;)
Gradle 8.1.4 kts
The following snippets work in KMP as well.
android {
...
applicationVariants.all {
this.outputs
.map { it as com.android.build.gradle.internal.api.ApkVariantOutputImpl }
.forEach { output ->
val variant = this.buildType.name
var apkName =
this.flavorName[0].uppercase() + this.flavorName.substring(1) + "_" + this.versionName
if (variant.isNotEmpty()) apkName += "_$variant"
apkName += ".apk"
println("ApkName=$apkName ${this.buildType.name}")
output.outputFileName = apkName
}
}
...
Gradle 8.1+ kts
The following snippets work in KMM as well.
android {
...
productFlavors {
create("free") {
dimension = "app"
val appName = "Free App 2.0"
manifestPlaceholders["appName"] = appName
applicationIdSuffix = ".demo"
versionName = "1.0.0"
versionNameSuffix = ".3"
versionCode = (versionName + versionNameSuffix).replace(".", "").toInt()
val apkName = "${appName}_$versionName$versionNameSuffix($versionCode).apk"
// change app name block below
buildOutputs.all {
val variantOutputImpl = this as com.android.build.gradle.internal.api.BaseVariantOutputImpl
variantOutputImpl.outputFileName = apkName
}
}
}
}