Change apk name with Gradle - android

I have an Android project which uses Gradle for build process. Now I want to add two extra build types staging and production, so my build.gradle contains:
android {
buildTypes {
release {
runProguard false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
staging {
signingConfig signingConfigs.staging
applicationVariants.all { variant ->
appendVersionNameVersionCode(variant, defaultConfig)
}
}
production {
signingConfig signingConfigs.production
}
}
}
and my appndVersionNameVersionCode looks like:
def appendVersionNameVersionCode(variant, defaultConfig) {
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)
}
If I execute task assembleStaging then I get proper name of my apk, but when I execute assembleProduction then I get changed names of my apk (like in staging case). For example:
MyApp-defaultFlavor-production-9.9.9-999.apk
MyApp-defaultFlavor-production-9.9.9-999.apk
It looks like in production build type is executed appendVersionNameVersionCode. How can I avoid it?

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)
}
}

Lecho's solution doesn't work for Android Gradle Plugin 0.14.3+ because of removal of deprecated APIS:
http://tools.android.com/tech-docs/new-build-system
Almost 1.0: removed deprecated properties/methods
...
Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest (use the variant output)
The following works for me:
def appendVersionNameVersionCode(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.outputFile = new File(file.parent, fileName)
}
def file = output.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}

For newer android studio Gradle
AppName is your app name you want so replace it
variantName will be default Selected variant or flavor
Date will Today's date, So need to do any changes just paste it
applicationVariants.all { variant ->
variant.outputs.all {
def variantName = variant.name
def versionName = variant.versionName
def formattedDate = new Date().format('dd-MM-YYYY')
outputFileName = "AppName_${variantName}_D_${formattedDate}_V_${versionName}.apk"
}
}
Output:
AppName_release_D_26-04-2021_V_1.2.apk

If you use Kotlin DSL, then here is how you can change the APK name:
applicationVariants.all {
outputs.all {
this as com.android.build.gradle.internal.api.ApkVariantOutputImpl
val buildName = buildType.name
val timestamp = SimpleDateFormat("yyyyMMdd").format(Date())
val apkName = timestamp + "_AppName_" + defaultConfig.versionName + "." + defaultConfig.versionCode + "_" + buildName + ".apk"
outputFileName = apkName
}
}
If you don't want to use the timestamp, remove it. Otherwise, don't forget these lines on top of the file to import java classes:
import java.util.Date
import java.text.SimpleDateFormat
You can also compare the build name in order to specify a config for different builds, like so:
name == "debug"
/* or */
name.contains("release")

I have use little generic version of it. build and install fine.
for example your projectName is "Salad" then for staging apk name will be "Salad-staging-dd-MM-YY" you can also change for debug and release apk. hope my solution will be better.
Change in projectName/app/build.gradle
buildTypes {
debug{
}
staging{
debuggable true
signingConfig signingConfigs.debug
applicationVariants.all { variant ->
variant.outputs.each { output ->
def date = new Date();
def formattedDate = date.format('dd-MM-yyyy')
def appName = getProjectDir().getParentFile().name
output.outputFile = new File(output.outputFile.parent,
output.outputFile.name.replace(getProjectDir().name +"-staging", "$appName-staging-" + formattedDate)
//for Debug use output.outputFile = new File(output.outputFile.parent,
// output.outputFile.name.replace("-debug", "-" + formattedDate)
)
}
}
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
to change Android app name ref

Here is what I have done:
def appName
if (project.hasProperty("applicationName")) {
appName = applicationName
} else {
appName = parent.name
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace("app-release.apk", appName + "_V" + versionCode + ".apk"))
}
}

i successfully create apk in android studio 4.1v or above.
application level gradle
Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.11'
ext.play_services_version = '16.0.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//..............and another module level gradle within........//
buildTypes{
release {
def versionCode = "4.1.0"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
applicationVariants.all { variant ->
variant.outputs.all() { output ->
variant.productFlavors.each { flavor ->
def appName = "MyAPP" + versionCode + ".apk"
outputFileName = new File("./build/",
appName.replace(".apk", "_${flavor.name}.apk")
)
}
}
}
}
debug {
// ext.enableCrashlytics = false
// ext.alwaysUpdateBuildId = false
}
}

Im using gradle 7.5
my version of the filename consists in :
applicationVariants.all { variant ->
println("Generating variant: " + variant.getName())
variant.outputs.all { output ->
def variantName = variant.name
def versionName = variant.versionName
def formattedDate = new Date().format('dd_MM_YY')
output.versionCodeOverride = defaultConfig.versionCode * 10 + variant.productFlavors.get(0).abiVersionCode
//output.outputFileName = "${defaultConfig.applicationId}.${variantName}_v${versionName}_${defaultConfig.versionCode}.apk"
output.outputFileName = "${variantName}_v${versionName}_${defaultConfig.versionCode}_${formattedDate}.apk"
}
}

Related

flutter set version name in apk filename

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

Gradle Generated APK name always has "app" and not my app name

The following build script generates three APK's but doesn't seem to include the app's name.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
appendVersionNameVersionCode(variant, defaultConfig)
}
}
}
def appendVersionNameVersionCode(variant, defaultConfig) {
variant.outputs.each { output ->
if (output.zipAlign) {
def file = output.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.outputFile = new File(file.parent, fileName)
}
def file = output.packageApplication.outputFile
def fileName = file.name.replace(".apk", "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk")
output.packageApplication.outputFile = new File(file.parent, fileName)
}
}
This generates:
app-development-release-null-1.apk
app-live-release-null-1.apk
How can I remove the "null" and replace "app" with my app's actual name?
Update
I fixed the null by adding in a versionName to the top of the gradle file. For some reason it was missing but still can't seem to fix the "app" string and replace it with my app's name.
settings.gradle:
rootProject.name = 'appName'
Here is my ways, this should give you some hint.
I added application name in root build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// other class path.
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
ext {
androidApplicationId = 'com.sample.myproject'
androidVersionCode = 1
androidVersionName = "1.0"
androidAppName= 'myproject'
}
}
Then in app project build gradle, add def baseName:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
applicationId globalConfiguration.getAt("androidApplicationId")
versionCode globalConfiguration.getAt("androidVersionCode")
versionName globalConfiguration.getAt("androidVersionName")
def baseName = globalConfiguration.getAt("androidAppName")
project.ext.set("archivesBaseName", baseName + "-" + defaultConfig.versionName);
}
...
You will get generated apk with app name like this:
myproject-1.0-debug.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)
}
}
}

Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl

After updating to AS 1.0 RC 1 and plugin 0.14.4 I am having problems with the renaming part of my build.gradle:
applicationVariants.all { variant ->
def file = variant.outputFile
variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
throws now:
Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated#67e7625f.
and also I cannot jump to the class ApplicationVariantImpl to look how the property might have been renamed.
Anyone knows workarounds for this?
try this
applicationVariants.all { variant ->
variant.outputs.each { output ->
def file = output.outputFile
output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
More comprehensively:
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
}
This can occur for few reasons:
1.) First as was said before by #Khalidov, try
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = ...
}
}
2.) Second try update all other plugins.
For example I got this problem for Spoon, that resolved by update Spoon up to:
classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.14.1'
Or where there's only one variant:
def apk = outputs[0].outputFile
Instead of
def apk = variant.outputFile
Make sure you run the latest gradle version (not the plugin, gradle it self).
Check your gradle-wrapper.properties. Are you running gradle 2.1?
More info on compatibility: http://tools.android.com/tech-docs/new-build-system/version-compatibility
I managed to solve as follows:
old:
buildTypes {
libertação {
runProguard false // esta linha tem que ser mudado
proguardFiles getDefaultProguardFile ( 'android.txt proguard-' ), 'proguard-rules.pro'
}
}
new:
buildTypes {
libertação {
minifyEnabled false // nova versão
proguardFiles getDefaultProguardFile ( 'android.txt proguard-' ), 'proguard-rules.pro'
}
}
edited in file buil.gradle of your project as described in this post by ruan65
Error:(26, 0) Gradle DSL method not found: 'runProguard()'
and after edit too this line:
applicationVariants . all { variant ->
variant . outputs . each { output ->
def file = output . outputFile
output . outputFile = new File ( file . parent , file . name . replace ( ".apk" , "-" + defaultConfig . versionName + ".apk" ))
}
}
as it was said up there.
That settled me!

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