Skip to content Skip to sidebar Skip to footer

How To Change Android App Bundles Name (app.aab) To Reflect App Version And Build Type

While I'm building an APK I can change APK name in build.gradle script, like that: android.applicationVariants.all { variant -> if (variant.buildType.name != 'debug') {

Solution 1:

I have come up with the solution of how to achieve this with Gradle.

First, we have to create in App build.gradle file a Gradle task that will rename the original app.aab on copy. This method is described here. Then for conveniance, we will add another method that will delete old app.aab file.

android{ 
.....
}
dependencies{
.....
}
.....

task renameBundle(type: Copy) {
    from"$buildDir/outputs/bundle/release"
    into "$buildDir/outputs/bundle/release"

    rename 'app.aab', "${android.defaultConfig.versionName}.aab"
}

task deleteOriginalBundleFile(type: Delete) {
    deletefileTree("$buildDir/outputs/bundle/release").matching {
        include "app.aab"
    }
}

In this example the output file name will be something like 1.5.11.aab Then we can combine those tasks together into publishRelease task which will be used for publishing the App:

task publishRelease(type: GradleBuild) {
    tasks = ['clean', 'assembleRelease', 'bundleRelease', 'renameBundle', 'deleteOriginalBundleFile']
}

Solution 2:

android {
    ...

    this.project.afterEvaluate { project ->
        project.tasks.each { task ->
            if (task.toString().contains("packageReleaseBundle")) {
                task.doLast {
                    copy {
                        from "$buildDir/outputs/bundle/release"
                        into "${projectDir}/../../../../publish/android/"

                        rename "app.aab", "${android.defaultConfig.versionName}.aab"
                    }
                }
            }
        }
    }  
}

Post a Comment for "How To Change Android App Bundles Name (app.aab) To Reflect App Version And Build Type"