Keywords: Android Studio | APK naming | Gradle configuration
Abstract: This article provides a detailed guide on how to change the default APK file names generated in Android Studio. It covers multiple methods, including using the variant API, setting archivesBaseName, and handling AAB files, with code examples and best practices for different Gradle versions.
When generating signed APKs in Android Studio, the default file name is often app-release.apk. This article explores various methods to customize this name to suit project requirements or version control.
Using the Variant API
With Gradle plugin 3.0 and above, you can use the variant API to dynamically set the APK name. Modify your build.gradle file as follows:
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
}This code iterates over all variants and sets the output file name based on the variant name and version name.
Setting archivesBaseName
Another approach is to set the archivesBaseName property in the defaultConfig block. For example:
defaultConfig {
applicationId "com.example.app"
versionCode 1
versionName "1.0"
archivesBaseName = "$applicationId-v$versionCode"
}This will result in APK names like com.example.app-v1-debug.apk for debug builds.
Advanced Naming with Date and Flavor
For more complex naming, you can incorporate date, build type, and product flavor. Here's an example:
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def project = "MyProject"
def flavor = variant.productFlavors[0].name
def buildType = variant.buildType.name
def version = variant.versionName
def date = new Date().format('yyyyMMdd')
outputFileName = "$project-$flavor-$buildType-$version-$date.apk"
}
}This generates names such as MyProject-dev-debug-1.3.6-20231001.apk.
Considerations for Gradle Versions
Note that with older Gradle versions (before 3.0), you might need to use each() instead of all(), and modify output.outputFile directly. Always check the Android Gradle plugin documentation for updates.
Handling AAB Files
For Android App Bundles (AAB), similar techniques can be applied, but ensure to use the correct task names. Refer to the latest Android Studio documentation for best practices.
In conclusion, customizing APK names in Android Studio is straightforward with Gradle configurations. Choose the method that best fits your workflow and Gradle version.