I am trying to rename the generated .aab
file in format <appname>-<variant>-<buildType>-<version>
, is there any way output bundle file can be renamed similar to apk renaming.
Copying and renaming I have tried, but the problem there is I need to name the bundle as <appname>-<variant>-<buildType>-<version>
. when I create the task as mentioned here How to change the generated filename for App Bundles with Gradle?
getting the variant is using a substring, which gives both variantBuildtype
.
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.
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>
.
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
}
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)
}
}
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With