First I'd like to clarify what a build variant is.
A build variant is a combination of the build type and flavor.
For example, a build type could be release
and a flavor could be something like freeVersion
. So a variant would be releaseFreeVersion
.
Note: the answer that you linked has a side effect of creating an additional copy of the .aab
file rather than only renaming it. This answer also fixes that.
Part 1: This answer assumes you are using product flavors
In this answer I'll assume that you are using product flavors. I'll also assume the format you want is <appname>-<flavor>-<buildType>-<version>
.
First create some supplementary Gradle methods
You can add these toward the bottom of your build.gradle
file (outside of all curly braces).
static String firstMatchingSubstring(String taskName, String[] keys) {
def lcName = taskName.toLowerCase()
for(String key: keys) {
if(lcName.contains(key.toLowerCase())) return key
}
return null
}
/**
*
* @param taskName e.g., bundleMyFlavorRelease or bundleRelease
* @return
*/
String getBuildType(String taskName) {
return firstMatchingSubstring(taskName, getBuildTypeNames())
}
/**
*
* @param taskName e.g., bundleMyFlavorRelease
* @return
*/
String getFlavor(String taskName) {
return firstMatchingSubstring(taskName, getProductFlavorNames())
}
String[] getBuildTypeNames() {
def types = []
android.buildTypes.all { type ->
types.add(type.name)
}
return types
}
String[] getProductFlavorNames() {
def flavors = []
android.productFlavors.all { flavor ->
flavors.add(flavor.name)
}
return flavors
}
Create a task to rename the app bundle
Similar to the accepted answer you linked in your question we'll insert a rename task.
I hard coded the app name but if you want to have different app names you could put them inside the android.defaultConfig
block and then reference the name below.
tasks.whenTaskAdded { task ->
def name = task.name
//Skip some unnecessary tasks
if (name.startsWith("bundle")
&& !name.contains("Classes")
&& !name.contains("Resources")
&& name != "bundle") {
def renameTaskName = "rename${task.name.capitalize()}Aab"
def version = "${android.defaultConfig.versionName}-${android.defaultConfig.versionCode}"
def flavor = getFlavor(name)
def type = getBuildType(name)
if(flavor == null || type == null) return
def outputName = "AppName-$flavor-$type-$version"
tasks.create(renameTaskName) {
def path = "${rootDir}/app/${flavor}/${type}/"
def originalFile = "$path/app-${flavor}-${type}.aab"
doLast {
if (file("$originalFile").exists()) {
ant.move file: "$originalFile",
tofile: "$path/${outputName}.aab"
}
}
}
task.finalizedBy(renameTaskName)
}
}
Part 2: This answer assumes you are not using product flavors
If you are not using product flavors then something more simple like the accepted answer you linked in your question with some modifications. This would produce a name like <appname>-<buildType>-<version>.aab
.
tasks.whenTaskAdded { task ->
def name = task.name
//Skip some unnecessary tasks
if (name.startsWith("bundle")
&& !name.contains("Classes")
&& !name.contains("Resources")
&& name != "bundle") {
def renameTaskName = "rename${task.name.capitalize()}Aab"
def type = task.name.substring("bundle".length()).uncapitalize()
def version = "${android.defaultConfig.versionName}-${android.defaultConfig.versionCode}"
def outputName = "AppName-$type-$version"
tasks.create(renameTaskName) {
def path = "${rootDir}/app/${type}/"
def originalFile = "$path/app-${type}.aab"
doLast {
if (file("$originalFile").exists()) {
ant.move file: "$originalFile",
tofile: "$path/${outputName}.aab"
}
}
}
task.finalizedBy(renameTaskName)
}
}