Noice2D
Noice2D

Reputation: 84

How to disable default gradle buildType suffix (-release, -debug)

I migrated a 3rd-party tool's gradle.build configs, so it uses android gradle plugin 3.5.3 and gradle 5.4.1.

The build goes all smoothly, but when I'm trying to make an .aab archive, things got broken because the toolchain expects the output .aab file to be named MyApplicationId.aab, but the new gradle defaults to output MyApplicationId-release.aab, with the buildType suffix which wasn't there.

I tried to search for a solution, but documentations about product flavors are mostly about adding suffix. How do I prevent the default "-release" suffix to be added? There wasn't any product flavor blocks in the toolchain's gradle config files.

Upvotes: 3

Views: 1280

Answers (1)

Noice2D
Noice2D

Reputation: 84

I realzed that I have to create custom tasks after reading other questions and answers: How to change the generated filename for App Bundles with Gradle?

Renaming applicationVariants.outputs' outputFileName does not work because those are for .apks.

I'm using Gradle 5.4.1 so my Copy task syntax reference is here.

I don't quite understand where the "app.aab" name string came from, so I defined my own aabFile name string to match my toolchain's output.

I don't care about the source file so it's not deleted by another delete task.

Also my toolchain seems to be removing unknown variables surrounded by "${}" so I had to work around ${buildDir} and ${flavor} by omitting the brackets and using concatenation for proper delimiting.

    tasks.whenTaskAdded { task ->
    if (task.name.startsWith("bundle")) { // e.g: buildRelease
        def renameTaskName = "rename${task.name.capitalize()}Aab" // renameBundleReleaseAab
        def flavorSuffix = task.name.substring("bundle".length()).uncapitalize() // "release"
        tasks.create(renameTaskName, Copy) {
            def path = "$buildDir/outputs/bundle/" + "$flavorSuffix/"
            def aabFile = "${android.defaultConfig.applicationId}-" + "$flavorSuffix" + ".aab"
            from(path) {
                include aabFile
                rename aabFile, "${android.defaultConfig.applicationId}.aab"
            }
            into path
        }

        task.finalizedBy(renameTaskName)
    }
}

As the original answer said: This will add more tasks than necessary, but those tasks will be skipped since they don't match any folder. e.g.

Task :app:renameBundleReleaseResourcesAab NO-SOURCE

Upvotes: 1

Related Questions