9

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") {
      variant.outputs.all {
          outputFileName = "${variant.applicationId}-v${variant.versionName}-${variant.name}.apk"
      }
  }
}

An I'll have something like this com.myapp.package-v1.x.x-release

Is there a way to do something similar with Android App Bundles, it is not convenient to always have app.aab

Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67

2 Answers2

2

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) {
    delete fileTree("$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']
}
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
  • 2
    How do you run publishRelease? How do I make it automatically call when I generate a signed APK into the specified directory? – poetryrocksalot Jul 22 '20 at 07:29
0
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"
                    }
                }
            }
        }
    }  
}
shooga
  • 1
  • 1