flutter set version name in apk filename - android

I tried many options in android/app/build.gradle
but none seems to be working.
for example I tried with below code in default config block
archivesBaseName = "AppName-${versionName}-${new Date().format('yyMMdd')}"
all example seems to be not related to flutter.

You can try this in your gradle file
buildTypes {
release {
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.all {
def appName = "your_app_name_"
def buildType = variant.variantData.variantConfiguration.buildType.name
def newName
if (buildType == 'debug'){
newName = "app-${variant.getFlavorName()}-debug.apk"
} else {
newName = "${appName}${defaultConfig.versionName}_${variant.getFlavorName()}.apk"
}
outputFileName = newName
}
}
}
}

This code works with regular builds and flavors.
Add it in the build.gradle (the source is https://stackoverflow.com/a/58392819/7198006)
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"
}
}

You can open pubspec.yaml and search on version and edit it
Version: 1.0.0+1

Related

Apk rename with the Gradle plugin v4.10.1

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

Android: How to change specific name of the generated apk file in Android Studio?

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.

Generate apk name as package name in Android Studio

Android studio will generate default apk name as app-(release|debug).apk.
How to generate apk file name same as package name of the app like com.example-debug.apk.
You can do it without using another tasks, setting the archivesBaseName.
For example:
defaultConfig {
....
project.ext.set("archivesBaseName", "MyName-" + defaultConfig.versionName);
}
Output:
MyName-1.0.12-release.apk
In your case:
project.ext.set("archivesBaseName", "com.example" );
Try putting this in your module's build.gradle
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
def appId = android.defaultConfig.applicationId
def fileName = appId + "-" variant.buildType.name +".apk"
output.outputFile = new File(file.parent, fileName)
}
}
you can see this link.
or Illogical option to rename your release|debug.apk with name what you want in file browser.
this code may be useful for you:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def formattedDate = new Date().format('yyyyMMddHHmmss')
def newName = output.outputFile.name
newName = newName.replace("app-", "$rootProject.ext.appName-") //"MyAppName" -> I set my app variables in the root project
newName = newName.replace("-release", "-release" + formattedDate)
//noinspection GroovyAssignabilityCheck
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
debug {
}
}
enjoy your code:)
Create a file named customname.gradle in the top level directory of the project.Put this code into it.
android.applicationVariants.all { variant ->;
def appName
//Check if an applicationName property is supplied; if not use the name of the parent project.
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
variant.outputs.each { output ->;
def newApkName
//If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such.
if (output.zipAlign) {
newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk"
} else {
newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk"
}
output.outputFile = new File(output.outputFile.parent, newApkName)
}}
Then in your app module's gradle add this code
apply from: "../customname.gradle"
This may help you. This code will create app name like applicationId-release.apk or applicationId-debug.apk in which applicationId can be your package name.
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def newName = output.outputFile.name
newName = newName.replace("app-", applicationId)
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}

Get application version from Manifest in Android Studio build.gradle

Is there a way to access the current application version during a build using Android Studio? I'm trying to include the build version string in the filename of the apk.
I'm using the following to change the filename based on the date for a nightly build, but would like to have another flavor for a release build that includes the version name.
productFlavors {
nightly {
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
def date = new Date();
def formattedDate = date.format('yyyy-MM-dd')
output.outputFile = new File(
file.parent,
"App-nightly-" + formattedDate + ".apk"
)
}
}
}
}
Via https://stackoverflow.com/a/19406109/1139908, if you are not defining your version numbers in Gradle, you can access them using the Manifest Parser:
import com.android.builder.core.DefaultManifestParser // At the top of build.gradle
def manifestParser = new com.android.builder.core.DefaultManifestParser()
String versionName = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
Also worth noting is that (per https://stackoverflow.com/a/22126638/1139908) using applicationVariants.all can have unexpected behavior for your default debug build. In my final solution, my buildTypes section looks like this:
buildTypes {
applicationVariants.all { variant ->
variant.outputs.each { output ->
def String fileName;
if(variant.name == android.buildTypes.release.name) {
def manifestParser = new DefaultManifestParser()
def String versionName = manifestParser.getVersionName((File) android.sourceSets.main.manifest.srcFile)
fileName = "App-release-v${versionName}.apk"
} else { //etc }
def File file = output.outputFile
output.outputFile = new File(
file.parent,
fileName
)
}
}
release {
//etc
}
}

Build release apk with customize name format in Android Studio

I want the .apk to be built with the following name format (with timestamp).
How can I set it?
format : {app_name}{yyyymmddhis}.apk
Now, it is fixed with the name {app_name}-{release}.apk
in the build.gradle file, you should change/add buildTypes like this:
buildTypes {
release {
signingConfig signingConfigs.release
applicationVariants.all { variant ->
def file = variant.outputFile
def date = new Date();
def formattedDate = date.format('yyyyMMddHHmmss')
variant.outputFile = new File(
file.parent,
file.name.replace("-release", "-" + formattedDate)
)
}
}
}
====== EDIT with Android Studio 1.0 ======
If you are using Android Studio 1.0, you will get an error like this:
Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated#67e7625f.
You should change the build.Types part to this:
buildTypes {
release {
signingConfig signingConfigs.releaseConfig
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)
)
}
}
}
}
As a side note, I would really like to know where people get the objects structures of gradle build objects.
To construct this I've used some trial and errors, a bit of Googling, and this (which is out of date)
android {
...
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def flavor = "default";
if (variant.productFlavors.size() > 0)
flavor = variant.productFlavors.get(0);
def initials = "DefaultFlavor";
if (flavor.name == "flavor1")
initials = "F1";
else if (flavor.name == "flavor2")
initials = "F2";
def build = "Debug";
if (variant.buildType.name == "release")
build = "Release"
def finalName = variant.versionCode + "-" + initials + "-" + build + "-v" + flavor.versionName + "-MyAppName.apk";
output.outputFile = new File(output.outputFile.parent, finalName)
}
}
}
...
}
Ok, So as a side note, if you would like to determine the names of your libs project here is a quick script, make sure you put it in each of your library gradle build files.
android {
...
defaultConfig {
minSdkVersion 10
targetSdkVersion 16
versionCode 1
versionName "1.0.4"
project.version = versionName
}
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "${archivesBaseName}-v${version}.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
...
}
You should change something like this. Doing dynamically.
project.archivesBaseName = {app_name}{yyyymmddhis};
But I have read that this is going to be deprecated.
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Deprecated dynamic property: "archivesBaseName" on "root project 'myapp'", value: "AnotherName".

Categories

Resources