gpt4 book ai didi

android - 如何对 android aab bundle 使用 ProductFlavors

转载 作者:行者123 更新时间:2023-12-02 12:08:02 24 4
gpt4 key购买 nike

我尝试通过productFlavors 构建不同的Android 应用程序包。为了保存和测试文件,我需要一个固定的文件名。

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

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

但这不适用于 bundle 。我尝试让它与以下代码一起工作:

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 看起来像这样:

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");每个口味,但这总是生成游戏口味的名称。应用程序包内的 list 包含正确的版本代码。如何在复制任务中从当前编译风格中获取正确的版本代码?

最佳答案

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

我认为它满足您的需求。

关于android - 如何对 android aab bundle 使用 ProductFlavors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com