How to change the generated filename for App Bundles with Gradle?
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)
}
}
How To change Android App Bundles name (app.aab) to reflect App version and build type
I have come up with the solution of how to achieve this with Gradle.
First, we have to create in App build.gradle
file a Gradle task that will rename the original app.aab
on copy. This method is described here.
Then for conveniance, we will add another method that will delete old app.aab
file.
android{
.....
}
dependencies{
.....
}
.....
task renameBundle(type: Copy) {
from "$buildDir/outputs/bundle/release"
into "$buildDir/outputs/bundle/release"
rename 'app.aab', "${android.defaultConfig.versionName}.aab"
}
task deleteOriginalBundleFile(type: Delete) {
delete fileTree("$buildDir/outputs/bundle/release").matching {
include "app.aab"
}
}
In this example the output file name will be something like 1.5.11.aab
Then we can combine those tasks together into publishRelease
task which will be used for publishing the App:
task publishRelease(type: GradleBuild) {
tasks = ['clean', 'assembleRelease', 'bundleRelease', 'renameBundle', 'deleteOriginalBundleFile']
}
How to Rename and Generate all APK & Bundle from Gradle with Product flavour and APK Splits
As per This answer, you can go with Option - Two
with minor changes as mention below only works for APK
, not the Bundle / AAB files
splits {
abi {
enable true
reset()
include 'arm64-v8a', 'x86', 'x86_64'
universalApk false
}
}
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
// New one or Updated one
output.outputFileName = "${variant.getFlavorName()}-${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}-${output.getFilter(com.android.build.OutputFile.ABI)}.apk"
// Old one
// output.outputFileName = "${variant.buildType.name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}.apk"
}
}
Also, remove the line from each Flavor's block
// To Change the APK and Bundle Name
archivesBaseName = "${name}-v${versionCode}_${versionName}-${new Date().format('ddMMMyyyy_HH-mm')}"
By this, you get the output file name like this
For aFlvour
- Release
aFlavor-release-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk
aFlavor-release-v5_1.0.5-16Jan2020_21-26-x86_64.apk
aFlavor-release-v5_1.0.5-16Jan2020_21-26-x86.apk
- Debug
aFlavor-debug-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk
aFlavor-debug-v5_1.0.5-16Jan2020_21-26-x86_64.apk
aFlavor-debug-v5_1.0.5-16Jan2020_21-26-x86.apk
For bFlavor
Similar name as above just change the prefix aFlavor
with bFlavor
like
bFlavor-release-v5_1.0.5-16Jan2020_21-26-arm64-v8a.apk
For cFlavor
Similar name as above just change the prefix aFlavor
with cFlavor
and, versionCode
and versionName
as respected
cFlavor-release-v3_1.0.3-16Jan2020_21-26-arm64-v8a.apk
How to set versionName in APK filename using gradle?
This solved my problem: using applicationVariants.all
instead of applicationVariants.each
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
}
Update:
So it seems this does not work with 0.14+ versions of android studio gradle plugin.
This does the trick (Reference from this question
) :
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
output.outputFile.name.replace(".apk", "-${variant.versionName}.apk"))
}
}
}
app-release.apk how to change this default generated apk name
Yes we can change that but with some more attention
Now add this in your build.gradle in your project while make sure you have checked the build variant of your project like release or Debug
so here I have set my build variant as release
but you may select as Debug as well.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig getSigningConfig()
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('yyyyMMddHHmmss')
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace("-release", "-" + formattedDate)
//for Debug use output.outputFile = new File(output.outputFile.parent,
// output.outputFile.name.replace("-debug", "-" + formattedDate)
)
}
}
}
}
You may Do it With different Approach Like this
defaultConfig {
applicationId "com.myapp.status"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
setProperty("archivesBaseName", "COMU-$versionName")
}
Using Set property method in build.gradle
and Don't forget to sync the gradle before running the projects
Hope It will solve your problem :)
A New approach to handle this added recently by google update
You may now rename your build according to flavor or Variant output
//Below source is from developer android documentation
For more details follow the above documentation link
Using the Variant API to manipulate variant outputs is broken with the new plugin. It still works for simple tasks, such as changing the APK name during build time, as shown below:
// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${variant.name}-${variant.versionName}.apk"
}
}
Renaming .aab bundle
This is nicely answered by David Medenjak
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)
}
//@credit to David Medenjak for this block of code
}
Is there need of above code
What I have observed in the latest version of the android studio 3.3.1
The rename of .aab bundle is done by the previous code there don't require any task rename at all.
Hope it will help you guys. :)
Related Topics
How to Change Action Bar Actions Dynamically
Add an Image from Url into Custom Infowindow Google Maps V2
Android:Change Button Text and Background Color
Can't Handle Both Click and Touch Events Simultaneously
Taking a "Screenshot" of a Specific Layout in Android
5.1 Crash - a Taskdescription's Primary Color Should Be Opaque
How to Load HTML String in a Webview
Android - How to Set an Alarm to a Specific Date
Chaining Requests in Retrofit + Rxjava
Android 5.0: How to Change Recent Apps Title Color
Android Accessibility Service Detect Notification
Fragmentactivity Cannot Be Resolved to a Type
Syncing Android Studio Project with Gradle Files
Android:Dither="True" Does Not Dither, What's Wrong