For the sake of simplicity, one should better use the base plugin.
This Kotlin script reads String from gradle/libs.versions.toml:
[versions]
app_versionName = "1.0.0"
And applies it to all the output packages, no matter if APK or AAB ...
base {
val versionName: String = libs.versions.app.versionName.get()
archivesName = "SomeApp_$versionName"
}
android { ... }
Which basically means, that it is not required to rename anything.
This also works fine with build-types and product-flavors, by default.
One can avoid some moving, when changing the value of buildDir.
However, one can as well move and rename files, in case this should be required. I've wrote an Exec task in Groovy for cross-platform CLI execution, no matter what the commandLine is. My RenameTask can detect Linux, Mac & Windows, as well as release & debug.
Property archivesBaseName needs to be defined in defaultConfig:
android {
defaultConfig {
setProperty("archivesBaseName", "SomeApp_" + "1.0.0")
}
}
RenameTask extends Exec performs the renaming (not to be confused with type: Rename):
import javax.inject.Inject
/**
* App Bundle RenameTask
* @author Martin Zeitler
**/
class RenameTask extends Exec {
private String buildType
@Inject RenameTask(String value) {this.setBuildType(value)}
@Input String getBuildType() {return this.buildType}
void setBuildType(String value) {this.buildType = value}
@Override
@TaskAction
void exec() {
def baseName = getProject().getProperty('archivesBaseName')
def basePath = getProject().getProjectDir().getAbsolutePath()
def bundlePath = "${basePath}/build/outputs/bundle/${this.getBuildType()}"
def srcFile = "${bundlePath}/${baseName}-${this.getBuildType()}.aab"
def dstFile = "${bundlePath}/${baseName}.aab"
def os = org.gradle.internal.os.OperatingSystem.current()
if (os.isUnix() || os.isLinux() || os.isMacOsX()) {
commandLine "mv -v ${srcFile} ${dstFile}".split(" ")
} else if (os.isWindows()) {
commandLine "ren ${srcFile} ${dstFile}".split(" ")
} else {
throw new GradleException("Cannot move AAB with ${os.getName()}.")
}
super.exec()
}
}
And it finalizes two other tasks:
// it defines tasks :renameBundleRelease & :renameBundleDebug
task renameBundleRelease(type: RenameTask, constructorArgs: ['release'])
task renameBundleDebug(type: RenameTask, constructorArgs: ['debug'])
// it sets finalizedBy for :bundleRelease & :bundleDebug
tasks.whenTaskAdded { task ->
switch (task.name) {
case 'bundleRelease': task.finalizedBy renameBundleRelease; break
case 'bundleDebug': task.finalizedBy renameBundleDebug; break
}
}
However, in most cases the base plugin might already suffice.
One can set archivesName - but one cannot move files around.
base {
archivesName = "someapp_${libs.versions.app.version.name.get()}"
}