Application Variant do Last for App Bundle
Asked Answered
G

1

7

I have entered below code in my Gradle.

///start
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            println("Roop: mappingFile:  ${output.outputFile}")
        }
            variant.assemble.doLast {
//            doLast {
                File dir = new File("${project.projectDir}/out1")
                dir.mkdirs()
                println("Roop: copy")
                variant.outputs.each { output ->
                    File file = output.outputFile
                    println("Roop: file:" + file)
                }
            }
    }

Now when i perform Build Apk from android studio, Variant.assemble.doLast gets executed after apk is created. But the same doLast is not called if i perform Build Bundle.

Any info why doLast does not get called for BuildBundle ?

Gynaecocracy answered 16/8, 2019 at 10:12 Comment(2)
Possible duplicate of How to change the generated filename for App Bundles with Gradle?Amiss
@Gabriele did you get the solution? If yes then please share it with meHealy
R
1

So to answer your question, this code doesn't get run when you build a bundle (or AAB) because you're using variant.assemble.doLast (this has been deprecated in favour of variant.assembleProvider.configure btw), and that only runs when an assemble (or APK) task is run.

Assuming you'd like to run that code on both a bundle (AAB) and assemble (APK) task, one thing you can do is monitor the task list by doing something like this (example code is Kotlin DSL, not Groovy):

tasks.whenTaskAdded {
    if ((name.startsWith("bundle") || name.startsWith("assemble"))
        && (name.endsWith("Release") || name.endsWith("Debug"))
    ) {
        doLast {
            android.applicationVariants.all {
                this.outputs.forEach { output ->
                    println("Output file: ${output.outputFile}")
                    // Your code goes here
                }
            }
        }
    }
}

This will look out for any build tasks and then allow you to run your code after it has been built.

You should note that if you want to use the outputFile as per the snippet above, it only returns the path to an APK. It does not yet support/return a path to an AAB file even if you are building a bundle (I'm not sure if this is intentional or a bug within AGP).

Receive answered 2/8, 2023 at 20:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.