
AGP 9.0 Modify APK Packaging Name
AGP 9.0 marks the old version `applicationVariants` API as deprecated, this article provides the new version's syntax.
162 views
AGP 9.0 deprecated the old applicationVariants API, so migration to the new Variant API is required.
Old Solution (Deprecated in AGP 9.0)
Previously, you could customize the output APK name via applicationVariants:
android {
applicationVariants.all {
outputs.all {
val currentDate = SimpleDateFormat("yyyy.MM.dd").format(Date())
val outputFileName =
"${applicationId}_${buildType.name}_v${versionName}_${currentDate}.apk"
(this as com.android.build.gradle.internal.api.BaseVariantOutputImpl).outputFileName =
outputFileName
}
}
}
New Solution (androidComponents + onVariants)
The official recommendation is to use the new androidComponents API:
androidComponents {
onVariants { variant ->
variant.outputs.forEach { output ->
val currentDate = SimpleDateFormat("yyyy.MM.dd").format(Date())
val newName =
"${variant.applicationId.get()}-${variant.name}-${output.versionName.get()}-${currentDate}.apk"
output.outputFileName.set(newName)
}
}
}
Notes
- The above example uses Kotlin DSL (
build.gradle.kts). applicationVariantsis deprecated in AGP 9.0; it is recommended to migrate as soon as possible to avoid future upgrade errors.- When using
SimpleDateFormat, you need to importjava.text.SimpleDateFormatandjava.util.Dateat the top of the script.