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"))
}
}
}
How to change apk name by using gradle like this format?
Update: Please check Anrimian's answer below which is much simpler and shorter.
Try this:
gradle.properties
applicationName = MyApp
build.gradle
android {
...
defaultConfig {
versionCode 111
...
}
buildTypes {
release {
...
applicationVariants.all { variant ->
renameAPK(variant, defaultConfig, 'R')
}
}
debug {
...
applicationVariants.all { variant ->
renameAPK(variant, defaultConfig, 'T')
}
}
}
}
def renameAPK(variant, defaultConfig, buildType) {
variant.outputs.each { output ->
def formattedDate = new Date().format('yyMMdd')
def file = output.packageApplication.outputFile
def fileName = applicationName + "_V" + defaultConfig.versionCode + "_" + formattedDate + "_" + buildType + ".apk"
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}
Reference:
https://stackoverflow.com/a/30332234/206292
https://stackoverflow.com/a/27104634/206292
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)
}
}
Apk rename with the Gradle plugin v4.10.1
Try this:
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def builtType = variant.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
def flavor = variant.flavorName
outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
}
}
This outputs the following apk name : app-release-myFlavor-0.0.1-1.apk.
How do I append the build.gradle versionName to the release build APK file
If you want to change your apk file name at release build you can do like as follows.
For Example: ( app/build.gradle )
android {
applicationVariants.all { variant ->
if (variant.buildType.name.equals("release")) {
variant.outputs.each { output ->
if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
def apk_name = "my_application"
def versionName = defaultConfig.versionName
//you can also add version code and date if you like
//def applicationId = defaultConfig.applicationId
//def versionCode = defaultConfig.versionCode
//def date = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())
def newName = "${apk_name}_${versionName}.apk"
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
}
}
Change apk name with Gradle
As CommonsWare wrote in his comment, you should call appendVersionNameVersionCode
only for staging variants. You can easily do that, just slightly modify your appendVersionNameVersionCode
method, for example:
def appendVersionNameVersionCode(variant, defaultConfig) {
//check if staging variant
if(variant.name == android.buildTypes.staging.name){
if(variant.zipAlign) {
def file = variant.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.outputFile = new File(file.parent, fileName)
}
def file = variant.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
variant.packageApplication.outputFile = new File(file.parent, fileName)
}
}
Related Topics
How to Integrate Opencv into Qt Creator Android Project
Set Inputtype for an Edittext Programmatically
Getexternalstoragepublicdirectory Deprecated in Android Q
How to Keep/Exclude a Particular Package Path When Using Proguard
Android: Using Findviewbyid() with a String/In a Loop
Copy Database from Assets Folder in Unrooted Device
Change the Color of a Checked Menu Item in a Navigation Drawer
Datepicker Dialog Without Calendar Visualization in Lollipop [Spinner Mode]
How to Load External Webpage in Webview
Android Webview, How to Handle Redirects in App Instead of Opening a Browser
Filtering Listview with Custom (Object) Adapter
Why Do I Get "Unresolved Reference" Error for My View's Name/Id When I Type It in Kotlin
How to Prevent a Service from Being Killed by Os
Difference Between Oncreateview and Onviewcreated in Fragment
Alphabetindexer with Custom Adapter Managed by Loadermanager