How to change the generated filename for App Bundles with Gradle?

北慕城南 提交于 2019-11-26 23:13:55

问题


So to change the generated APK filename inside gradle android I could do something like:

applicationVariants.output.all {
    outputFileName = "the_file_name_that_i_want.apk"
}

Is there a similar thing for the generated App Bundle file? How can I change the generated App Bundle filename?


回答1:


As a more generic way to Martin Zeitlers answer the following will listen for added tasks, then insert rename tasks for any bundle* task that gets added.

Just add it to the bottom of your build.gradle file.

Note: It will add more tasks than necessary, but those tasks will be skipped since they don't match any folder. e.g. > Task :app:renameBundleDevelopmentDebugResourcesAab NO-SOURCE

tasks.whenTaskAdded { task ->
    if (task.name.startsWith("bundle")) {
        def renameTaskName = "rename${task.name.capitalize()}Aab"
        def flavor = task.name.substring("bundle".length()).uncapitalize()
        tasks.create(renameTaskName, Copy) {
            def path = "${buildDir}/outputs/bundle/${flavor}/"
            from(path)
            include "app.aab"
            destinationDir file("${buildDir}/outputs/renamedBundle/")
            rename "app.aab", "${flavor}.aab"
        }

        task.finalizedBy(renameTaskName)
    }
}



回答2:


You could use something like this:

defaultConfig {
  applicationId "com.test.app"
  versionCode 1
  versionName "1.0"
  setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
}



回答3:


Meanwhile I've found a better way to do it ...

// it sets task.finalizedBy for :bundle
tasks.whenTaskAdded { task ->
    switch (task.name) {
        case 'bundleDebug':
        case 'bundleRelease':
            task.finalizedBy renameBundle
            break
    }
}

using an Exec task (still wondering how the syntax of an ant.move task would look alike):

// it finalizes task :bundle
task renameBundle (type: Exec) {

    description "it renames an Android App Bundle"

    def bundlePath = rootProject.getProjectDir().getAbsolutePath() + "/${project.name}/build/outputs/bundle/release/"
    def baseName = getProperty('archivesBaseName')
    def srcFile = "${baseName}.aab"
    def dstFile = "${baseName}_renamed.aab"

    def stdout = new ByteArrayOutputStream()
    def stderr = new ByteArrayOutputStream()

    commandLine "mv", "-v", "${srcFile}", "${dstFile}"
    workingDir = bundlePath
    ignoreExitValue true
    standardOutput stdout
    errorOutput stderr

    doLast {
        if (execResult.getExitValue() == 0) {
            println ":${project.name}:${name} > ${stdout.toString()}"
        } else {
            println ":${project.name}:${name} > ${stderr.toString()}"
        }
    }
}

This leaves nothing behind - and one can move the file wherever one wants.



来源:https://stackoverflow.com/questions/52508720/how-to-change-the-generated-filename-for-app-bundles-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!