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?
Did you try to replace def versionCode = android.defaultConfig.versionCode by def versionCode = flavor.versionCode?
I think it meets your need.
Related
I was building single code for multiple apk's by using below gradle code:
flavorDimensions "version"
productFlavors {
Free {
dimension "version"
applicationId "com.exampleFree.app"
}
Paid {
dimension "version"
applicationId "com.examplePaid.app"
}
}
Now when i build, it creates archive app as below name:
app-Free-debug.apk
When I include below code in gradle,
setProperty("archivesBaseName","")
It now creates as below APK archive name
-Free-debug.apk
I need my APK file name as below
Free-debug.apk
I was so close but how to remove that hypen (-) which is append in prefix ?
Here you can use android migration like this.
android {
//........
flavorDimensions "version"
productFlavors {
Free {
dimension "version"
applicationId "com.exampleFree.app"
}
Paid {
dimension "version"
applicationId "com.examplePaid.app"
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def appId = variant.applicationId// com.exampleFree.app OR com.examplePaid.app
def versionName = variant.versionName
def versionCode = variant.versionCode // e.g 1.0
def flavorName = variant.flavorName // e. g. Free
def buildType = variant.buildType.name // e. g. debug
def variantName = variant.name // e. g. FreeDebug
//customize your app name by using variables
outputFileName = "${variantName}.apk"
}
}}
Apk name FreeDebug.apk
Proof
I´ve updated my Android Studio today to the 3.3 version which came with Gradle plugin version 4.10.1.
Previously, my build.gradle was renaming my apk´s with this code to the following structure:
app-{buildType[release|debug]}-{flavor[prod|stage]}-{versionName[1.2.4]-{versionCode[43]}.apk
app-release-prod-1.1.4-45.apk.
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = output.outputFile.name.replace(".apk", "-${variant.versionName}-${variant.versionCode}.apk").replace("-unsigned", "")
}
}
But I got this error after updating.
WARNING: API 'variantOutput.getPackageApplication()' is obsolete and has been replaced with 'variant.getPackageApplicationProvider()'.
It will be removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
To determine what is calling variantOutput.getPackageApplication(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace.
Affected Modules: app
The problem is at output.outputFile.name since you can't access output data on this plugin version.
So far I´ve tried this approach without success.
applicationVariants.all { variant ->
variant.flavors*.name.all { flavor ->
outputFileName = "${flavor}-${variant.buildType.name}-${variant.versionName}-${variant.versionCode}.apk".replace("-unsigned", "")
}
}
Any idea?
=======================================================
UPDATE
I took a retake on this matter, I´ve tried the following snippet, but I'm having issues retrieving the flavor of that variant.
android.applicationVariants.all { variant ->
def flavor = variant.flavorName
variant.outputs.all { output ->
def builtType = variant.buildType.name
def versionName = variant.versionName
def versionCode = variant.versionCode
outputFileName = "app-${flavor}-${builtType}-${versionName}-${versionCode}.apk"
}
}
outputs: app--release-1.0.4-88.apk
Thanks
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.
Using setProperty method you can rename your .apk name.
You can do this.
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.expertBrains.abc"
minSdkVersion 17
targetSdkVersion 28
versionCode 75
versionName "2.6.15"
multiDexEnabled true
setProperty("archivesBaseName", "(" + versionName + ") "+ new Date().format( 'yyyy-MM-dd HH:mm' ))
}
}
You can do it like this:
defaultConfig {
...
project.ext.set("archivesBaseName", applicationId + "_V" + versionName + "("+versionCode+")_" + new Date().format('dd-MM mm'));
}
As mentioned in the comments, the right way was to use ${variant.getFlavorName()}.apk or variant.baseName.
Can you try below.
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = outputFileName.replace(".apk", "-${variant.versionName}-${variant.versionCode}.apk").replace("-unsigned", "")
}
}
I hope this could help. I can't say this is the best way but it works.
productFlavour{
uat {
versionName "2.8.74"
buildConfigField("String", "ENVIRONMENT", '"uat"')
setProperty("archivesBaseName", "iotg-uat-v" + versionName)
}
staging {
versionName "2.9.4"
buildConfigField("String", "ENVIRONMENT", '"staging"')
setProperty("archivesBaseName", "iotg-staging-v" + versionName)
}
}
applicationVariants.all { variant ->
variant.outputs.all {
def appName = "AppName"
def buildType = variant.variantData.variantConfiguration.buildType.name
def newName = "${appName}${defaultConfig.versionName}_${buildType}.apk"
outputFileName = newName
}
}
Below code will generate apk file name as
AppName1.2.0_buildType.apk
By default IDE genarate a apk like app-debug.apk or app-release.apk file but I need to generate specific name of the Apk of the App.
For Example:
My application name is iPlanter so i need to generate iPlanter-debug.apk or iPlanter-release.apk instead of app-debug.apk or app-release.apk respectively.
Thanks,
Just add
archivesBaseName = "NAME_YOU_WANT"
in the android{} part of your gradle file.
You'll get "NAME_YOU_WANT-release.apk" as name of the generated file.
Step 1:
Go to root of the main project, under app , right click on app and refactor the app into specific name (example iPlanter) and press ok
Step 2:
Go to Project Setting file which is setting.gradle file
setting.gradle file contains
include ':app'
Now need to replace app by specific name.
For Example
app replace by iPlanter in include ':app'
it looks like below
include ':iPlanter'
then Sync project, after that run your application.
Finally, App generate an apk like iPlanter-debug.apk or iPlanter-release.apk file.
You just have to add following one line of code in app level gradle.
For name only
archivesBaseName = "NAME_YOU_WANT"
defaultConfig {
applicationId "com.PACKAGENAME"
minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)
targetSdkVersion Integer.parseInt(TARGET_SDK)
versionCode 11
versionName "2.3"
multiDexEnabled true
archivesBaseName = "NAME_YOU_WANT"
}
Name with version
archivesBaseName = "NAME_YOU_WANT" + versionName
defaultConfig {
applicationId "com.PACKAGENAME"
minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)
targetSdkVersion Integer.parseInt(TARGET_SDK)
versionCode 11
versionName "2.3"
multiDexEnabled true
archivesBaseName = "NAME_YOU_WANT" + versionName
}
You can use this for app name with current date and version
android {
def version = "2.4";
def milestone = "1";
def build = "0";
def name = getDate()+"APP NAME WHAT YOU WANT"+"v"+version
signingConfigs {
config {
….
}
}
compileSdkVersion Integer.parseInt(COMPILE_SDK)
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
applicationId "com.PACKAGENAME"
minSdkVersion Integer.parseInt(MIN_SDK_LIBRARY)
targetSdkVersion Integer.parseInt(TARGET_SDK)
versionCode 11
versionName "2.3"
multiDexEnabled true
}
buildTypes {
debug {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def apk = output.outputFile;
def newName;
newName = apk.name.replace("-" + variant.buildType.name, "")
.replace(project.name, name);
newName = newName.replace("-", "-" + version + "-" + milestone +
"-" + build + "-");
output.outputFile = new File(apk.parentFile, newName);
}
}
}
This will help you. This code will create app name like iPlanter-release.apk or iPlanter-debug.apk
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
project.ext { appName = 'iPlanter' }
def newName = output.outputFile.name
newName = newName.replace("app-", "$project.ext.appName-")
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
Update:
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "iPlanter_${variant.versionName}(${variant.versionCode}).apk"
}
}
It set name like
iPlanter_0.0.1(25).apk
For Android Studio 3, this works for me:
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File("AppName-" + variant.versionName + ".apk");
}
}
Try this code:
defaultConfig{
applicationVariants.all { variant ->
changeAPKName(variant, defaultConfig)
}
}
def changeAPKName(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
output.packageApplication.outputFile = new File(file.parent, "Your APK NAME")
}
def file = output.packageApplication.outputFile
output.packageApplication.outputFile = new File(file.parent, "Your APK NAME")
}
}
For Android Studio 3.1 this works for me:
android {
...............
...............
applicationVariants.all { variant ->
changeAPKName(variant, defaultConfig)
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
.................................
.................................
}
and
def changeAPKName(variant, defaultConfig) {
variant.outputs.all { output ->
outputFileName = new File("xxxxx" + variant.versionName +".apk")
}
}
On my PC, it suffices to rename (through refactor) app to the desired name yourName. After that, include ':app' in setting.grandle file is changed to include ':yourName'. However, in my case I need to close/reopen Android Studio because of sync error. As a result, obtain apk something like yourName-debug.apk and yourName-release.apk.
I'd like to change "app-release.apk" file name to like following when I build an app by using gradle.
[format]
(appname of package name)_V(version code)_(yyMMdd)_(R|T)
[explain]
(appname of package name) : example) com.example.myApp -> myApp
(version code) : build version code 2.2.3 -> 223
(yyMMdd) : build date 2015.11.18 -> 151118
(R|T) : if app is release, "R" but debug is "T".
If I generate an apk file in release, result is : myApp_V223_151118_R.apk.
How to make a file name like this in gradle?
This may be the shortest way:
defaultConfig {
...
applicationId "com.blahblah.example"
versionCode 1
versionName "1.0"
setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
}
buildType: like so
buildTypes {
debug {
...
versionNameSuffix "-T"
}
release {
...
versionNameSuffix "-R"
}
}
Keep in mind, Android Studio adds versionNameSuffix by build type name by default, so you may not need this.
Upd. In new versions of Android Studio you can it write little shorter(thanks for szx comment):
defaultConfig {
...
archivesBaseName = "$applicationId-v$versionCode($versionName)"
}
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
2019 / 2020 - How to change APK name For Gradle 3.3, 3.4, 3.5, 4.0 and above
android {
......
applicationVariants.all { variant ->
variant.outputs.all {
def flavor = variant.name
def versionName = variant.versionName
outputFileName = "prefix_${flavor}_${versionName}.apk"
}
}
}
The result would be like this,
prefix_release_1.0.1.apk
I'd like to apply different VersionCode to make apk file.
For debug only fix it to 1, and for release whatever number specified in defaultConfig.
Below code gives mypackage-release-1.apk file as assembleRelease artifact, which is not expected. I expected mypackage-release-10111.apk for that.
why the line debug { defaultConfig.versionCode=1 } affects assembleRelease artifact?
defaultConfig {
versionCode 10111
versionName '2.5.4'
minSdkVersion 10
targetSdkVersion 21
}
signingConfigs {
debug {
project.ext.loadSign = false
defaultConfig.versionCode = 1 // Why this value applied to assembleRelease?
}
release {
project.ext.loadSign = true
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionCode + ".apk"))
}
}
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
}
}
Here's an updated version:
android {
defaultConfig { ... }
applicationVariants.all { variant ->
if (variant.name == 'debug') {
variant.outputs.each { output ->
output.versionCodeOverride = 1
}
}
}
}
Late on the party...
The entire gradle file evaluated before any task execution, so you are basically changing the default versionCode while declaring debug configs. There is no direct way to reset versionCode from buildType, but the link on the other answer do the trick by declaring a task on build variants.
android {
...
defaultConfig {
...
}
buildTypes {
...
}
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def versionCode = flavor.versionCode
if (variant.buildType.isDebuggable()) {
versionCode += 1
}
flavor.versionCode = versionCode
}
}
The easiest solution is moving versionCode and versionName variables from defaultConfig to debug and release respectively.
android {
...
defaultConfig {
// without versionCode and versionName
...
}
buildTypes {
debug {
defaultConfig.versionCode X
defaultConfig.versionName 'X.Y.Z'
}
release {
defaultConfig.versionCode A
defaultConfig.versionName 'A.B.C'
}
}
...
}
Me too, but I think defaultConfig.versionCode was set when build.gradle be compiling. It's global static variable, and assigned at compiletime, not runtime.
I think we can intercept gradle task execution, and modify defaultConfig.versionCode at runtime.
After goooooooogle, I found this one works for me: https://gist.github.com/keyboardsurfer/a6a5bcf2b62f9aa41ae2
To use with Flavors:
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def name = flavor.getVersionName()
def code = flavor.getVersionCode()
if (variant.buildType.isDebuggable()) {
name += '-d'
code = 1
}
variant.outputs.each { output ->
output.versionNameOverride = name
output.versionCodeOverride = code
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
if (variant.buildType.isDebuggable()) {
output.versionCodeOverride = 26
output.versionNameOverride = "2.2.6"
}
}
}
put it in android{}
So recently I had to deal with the same scenario and all the examples I could find use the applicationVariants property which is ill-documented imo.
So after some digging through the source code a bit, I realized that in the end versionCode and versionName properties from ProductFlavor get merged into the AndroidManifest which got me thinking: couldn't we just inject them by ourselves, cause we have manifestPlaceholders property on ProductFlavor AND on BuildType DSL objects, so I came up with this -- don't hesitate to give feedback and tell me why it's wrong
In build.gradle(app)
android {
...
buildTypes {
debug {
manifestPlaceholder = [versionCode: X, versionName: "X.Y.Z"]
}
release {
manifestPlaceholder = [versionCode: A, versionName: "A.B.C"]
}
}
...
}
In AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="..."
android:versionCode="${versionCode}"
android:versionName="${versionName}">
...
</manifest>