如何将productFlavors用于android aab捆绑包 [英] How to use productFlavors for android aab bundles

查看:238
本文介绍了如何将productFlavors用于android aab捆绑包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过productFlavors构建不同的Android应用程序捆绑包.要保留和测试文件,我需要一个固定的文件名.

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

对于APK,我有以下工作代码:

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)
    }
}

但是版本代码始终是默认版本代码.我的build.gradle看起来像这样:

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
    }
}
}

我也尝试设置project.ext.set("archivesBaseName","app");每种口味,但这总是产生游戏口味的名称.应用程序捆绑包中的清单包含正确的versionCodes.如何在复制任务中从当前编译的版本中获取正确的versionCode?

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?

推荐答案

您是否尝试将 def versionCode = android.defaultConfig.versionCode 替换为 def versionCode = flavour.versionCode ?

我认为它可以满足您的需求.

I think it meets your need.

这篇关于如何将productFlavors用于android aab捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆