应用程序变体 do Last for App Bundle

use*_*801 6 android bundle app-bundle android-app-bundle

我在 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)
                }
            }
    }
Run Code Online (Sandbox Code Playgroud)

现在,当我从 android studio 执行 Build Apk 时,在创建 apk后会执行 Variant.assemble.doLast 。但是如果我执行Build Bundle ,则不会调用相同的 doLast 。

为什么没有为 BuildBundle 调用 doLast 的任何信息?

Haz*_*war 0

bundle因此,为了回答您的问题,当您构建(或 AAB)时,此代码不会运行,因为您正在使用variant.assemble.doLast(这已被弃用,以支持顺便说variant.assembleProvider.configure一句),并且仅在assemble运行(或 APK)任务时运行。

假设您想在bundle(AAB) 和assemble(APK) 任务上运行该代码,您可以做的一件事是通过执行类似以下操作来监视任务列表(示例代码是 Kotlin DSL,而不是 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
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这将查找任何构建任务,然后允许您在构建后运行代码。

您应该注意,如果您想按照上面的代码片段使用outputFile,它只会返回APK. 即使您正在构建捆绑包,它还不支持/返回文件路径AAB(我不确定这是故意的还是 AGP 中的错误)。