Member-only story

How I Change Default Generated APK File Name

Adi Andrea
2 min readNov 15, 2024
Photo by Mike Newbry on Unsplash

When we build and generate new APK file using Android Studio or gradle command line, it has default file names such as app-dev-debug.apk or app-prod-release.apk . Sometimes we need to rename it for better historical documentation, like including the version number and application name.

Assume your build configuration has buildTypes and productFlafors setup.

buildTypes {
getByName("release") {
...
}
getByName("debug") {
...
}
}

flavorDimensions.add("mode")
productFlavors {
create("prod") {
...
}
create("dev") {
...
}
}

Update your build.gradle.kts file by adding this code on android block.

applicationVariants.all {
val variant = this
variant.outputs
.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
.filter {
val names = it.name.split("-")
it.name.lowercase().contains(names[0], true) && it.name.lowercase().contains(names[1], true)
}
.forEach { output ->
val outputFileName = "awesomeapp_${variant.flavorName}_${variant.buildType.name}_${variant.versionName}.apk"
output.outputFileName = outputFileName
}
}
  • Here we get all our application variants and then iterate through their output names.

--

--

Adi Andrea
Adi Andrea

Written by Adi Andrea

Converting my learning journey into stories

No responses yet