How to use productFlavors for android aab bundles

杀马特。学长 韩版系。学妹 提交于 2021-02-16 13:08:23

问题


I try to build different Android app bundles via productFlavors. To keep and test the files I need a fixed file name.

For APK's I have the following working code:

    applicationVariants.all { variant ->
    if (variant.buildType.name.equals("release")) {
        variant.outputs.all { output ->
            outputFileName = "${applicationId}-${versionCode}-${variant.flavorName}.apk"
        }
    }

    if (variant.getBuildType().isMinifyEnabled()) {
        variant.assemble.doLast {
            copy {
                from variant.mappingFile
                into variant.outputs[0].outputFile.parent
                rename { String fileName ->
                    "${applicationId}-${versionCode}-${variant.flavorName}-mapping.txt"
                }
            }
        }
    }
}

But this don't work for bundles. I try to get it working with this code:

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) {
            println android.defaultConfig.versionName
            def applicationId = android.defaultConfig.applicationId
            def versionCode = android.defaultConfig.versionCode

            def path = "${buildDir}/outputs/bundle/${flavor}/"
            from(path)
            include "app.aab"
            destinationDir file("${buildDir}/outputs/renamedBundle/")
            rename "app.aab", "${applicationId}-${versionCode}-${flavor}.aab"
        }

        task.finalizedBy(renameTaskName)
    }
}

But the version code is always the default version code. My build.gradle looks like this:

project.ext {
VERSION_CODE_INSTANT = 1150
VERSION_CODE_PLAY = 11500
VERSION_NAME = "1.1.5"
}

android {

defaultConfig {
    applicationId "com.abc.test"
    resValue "string", "app_name", "Test"
    versionName VERSION_NAME
    versionCode VERSION_CODE_PLAY
    project.ext.set("archivesBaseName", "app");
}

productFlavors {
    instant {
        dimension 'type'
        versionCode VERSION_CODE_INSTANT
    }
    play {
        dimension 'type'
        versionCode VERSION_CODE_PLAY
    }
}
}

I also try to set project.ext.set("archivesBaseName", "app"); per flavour but this always generate the name of the play flavour. The Manifests inside the app bundles contains the correct versionCodes. How can I get the correct versionCode from the currently compiling flavour at the copy task?


回答1:


Did you try to replace def versionCode = android.defaultConfig.versionCode by def versionCode = flavor.versionCode?

I think it meets your need.



来源:https://stackoverflow.com/questions/55282416/how-to-use-productflavors-for-android-aab-bundles

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