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