问题
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