" app-release.apk" how to change this default generated apk name
AndroidAndroid StudioAndroid Problem Overview
Whenever I generate a signed apk in Android Studio, by default it gives the name as app-release.apk...
Can we do any settings so that it should prompt and ask me the name which need to be assigned to the apk(the way it do in eclipse)
What I do is - rename the apk after it generated. This doesn't give any errors but is there any genuine way so that i can do any changes in settings to get a prompt.
Note::
while generating apk android studio is giving me a prompt to select the location(only)
Android Solutions
Solution 1 - Android
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. :)
Solution 2 - Android
You might get the error with the latest android gradle plugin (3.0):
> Cannot set the value of read-only property 'outputFile'
According to the migration guide, we should use the following approach now:
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${applicationName}_${variant.buildType.name}_${defaultConfig.versionName}.apk"
}
}
Note 2 main changes here:
all
is used now instead ofeach
to iterate over the variant outputs.outputFileName
property is used instead of mutating a file reference.
Solution 3 - Android
(EDITED to work with Android Studio 3.0 and Gradle 4)
I was looking for a more complex apk filename renaming option and I wrote this solution that renames the apk with the following data:
- flavor
- build type
- version
- date
You would get an apk like this: myProject_dev_debug_1.3.6_131016_1047.apk.
You can find the whole answer here. Hope it helps!
In the build.gradle:
android {
...
buildTypes {
release {
minifyEnabled true
...
}
debug {
minifyEnabled false
}
}
productFlavors {
prod {
applicationId "com.feraguiba.myproject"
versionCode 3
versionName "1.2.0"
}
dev {
applicationId "com.feraguiba.myproject.dev"
versionCode 15
versionName "1.3.6"
}
}
applicationVariants.all { variant ->
variant.outputs.all { output ->
def project = "myProject"
def SEP = "_"
def flavor = variant.productFlavors[0].name
def buildType = variant.variantData.variantConfiguration.buildType.name
def version = variant.versionName
def date = new Date();
def formattedDate = date.format('ddMMyy_HHmm')
def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
outputFileName = new File(newApkName)
}
}
}
Solution 4 - Android
Here is a much shorter way:
defaultConfig {
...
applicationId "com.blahblah.example"
versionCode 1
versionName "1.0"
setProperty("archivesBaseName", applicationId + "-v" + versionCode + "(" + versionName + ")")
//or so
archivesBaseName = "$applicationId-v$versionCode($versionName)"
}
It gives you name com.blahblah.example-v1(1.0)-debug.apk (in debug mode)
Android Studio add versionNameSuffix by build type name by default, if you want override this, do next:
buildTypes {
debug {
...
versionNameSuffix "-MyNiceDebugModeName"
}
release {
...
}
}
Output in debug mode: com.blahblah.example-v1(1.0)-MyNiceDebugModeName.apk
Solution 5 - Android
I modified @Abhishek Chaubey answer to change the whole file name:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
project.ext { appName = 'MyAppName' }
def formattedDate = new Date().format('yyyyMMddHHmmss')
def newName = output.outputFile.name
newName = newName.replace("app-", "$project.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 {
}
}
This produces a file name like: MyAppName-release20150519121617.apk
Solution 6 - Android
I wrote more universal solution based on @Fer answer.
It also should work with flavor and build type based configuration of applicationId
, versionName
, versionCode
.
In the build.gradle:
android {
...
applicationVariants.all { variant ->
variant.outputs.each { output ->
def appId = variant.applicationId
def versionName = variant.versionName
def versionCode = variant.versionCode
def flavorName = variant.flavorName // e. g. free
def buildType = variant.buildType // e. g. debug
def variantName = variant.name // e. g. freeDebug
def apkName = appId + '_' + variantName + '_' + versionName + '_' + versionCode + '.apk';
output.outputFile = new File(output.outputFile.parentFile, apkName)
}
}
}
Example apk name: com.example.app_freeDebug_1.0_1.apk
For more information about variant
variable see ApkVariant and BaseVariant interfaces definition.
Solution 7 - Android
add android.applicationVariants.all
block like below in you app level gradle
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
lintOptions {
disable 'MissingTranslation'
}
signingConfig signingConfigs.release
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${applicationId}_${versionCode}_${variant.flavorName}_${variant.buildType.name}.apk"
}
}
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '_debug'
}
}
available at 2019/03/25
Solution 8 - Android
Not renaming it, but perhaps generating the name correctly in the first place would help? https://stackoverflow.com/questions/22126299/change-apk-name-with-gradle
Solution 9 - Android
With the flavors and split APK
this is how it works for APK
files, not the Bundle / AAB files
android {
....
productFlavors {
aFlavor {
applicationId "com.a"
versionCode 5
versionName "1.0.5"
signingConfig signingConfigs.signingA
}
bFlavor {
applicationId "com.b"
versionCode 5
versionName "1.0.5"
signingConfig signingConfigs.signingB
}
cFlavor {
applicationId "com.c"
versionCode 3
versionName "1.0.3"
signingConfig signingConfigs.signingC
}
}
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"
}
}
}
Result
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
For more detail review this Que-Ans thread
Solution 10 - Android
First rename your module from app to i.e. SurveyApp
Second add this to your project top-level (root project) gradle. It's working with Gradle 3.0
//rename apk for all sub projects
subprojects {
afterEvaluate { project ->
if (project.hasProperty("android")) {
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "${project.name}-${variant.name}-${variant.versionName}.apk"
}
}
}
}
}
Solution 11 - Android
Add the following code in build.gradle(Module:app)
android {
......
......
......
buildTypes {
release {
......
......
......
/*The is the code fot the template of release name*/
applicationVariants.all { variant ->
variant.outputs.each { output ->
def formattedDate = new Date().format('yyyy-MM-dd HH-mm')
def newName = "Your App Name " + formattedDate
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
}
}
And the release build name will be Your App Name 2018-03-31 12-34
Solution 12 - Android
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"
}
}
AppName_release_D_26-04-2021_V_1.2.apk Output:
Solution 13 - Android
I've realised that a lot of the answers didn't cater for different buildTypes, here is how I handle it.
applicationVariants.all { variant ->
variant.outputs.all {
def appVersionName = "${applicationId}v${versionCode}#${versionName}"
switch (buildType.name) {
case "debug": {
outputFileName = "${appVersionName}-staging.apk"
break
}
case "release": {
outputFileName = "${appVersionName}.apk"
break
}
}
}
}
Attributes like applicationId, versionCode and versionName are already defined in your defaultConfig. You can just easily format into something that gives it a more meaningful and context naming whenever you generate an APK.
This is how it'll churn out.
com.delacrixmorgan-v1.0.0#1.apk
com.delacrixmorgan-v1.0.0#1-staging.apk
Besides that, if you guys want to learn more about Gradle. I've went into length in this Medium article. Supercharging your Android Gradle
Solution 14 - Android
My solution may also be of help to someone.
Tested and Works on IntelliJ 2017.3.2 with Gradle 4.4
Scenario:
I have 2 flavours in my application, and so I wanted each release to be named appropriately according to each flavor.
The code below will be placed into your module gradle build file found in:
{app-root}/app/build.gradle
Gradle code to be added to android{ }
block:
android {
// ...
defaultConfig {
versionCode 10
versionName "1.2.3_build5"
}
buildTypes {
// ...
release {
// ...
applicationVariants.all {
variant.outputs.each { output ->
output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(output.outputFile.name, variant.flavorName + "-" + defaultConfig.versionName + "_v" + defaultConfig.versionCode + ".apk"))
}
}
}
}
productFlavors {
myspicyflavor {
applicationIdSuffix ".MySpicyFlavor"
signingConfig signingConfigs.debug
}
mystandardflavor {
applicationIdSuffix ".MyStandardFlavor"
signingConfig signingConfigs.config
}
}
}
The above provides the following APKs found in {app-root}/app/
:
myspicyflavor-release-1.2.3_build5_v10.apk
mystandardflavor-release-1.2.3_build5_v10.apk
Hope it can be of use to someone.
For more info, see other answers mentioned in the question
Solution 15 - Android
android studio 4.1.1
applicationVariants.all { variant ->
variant.outputs.all { output ->
def reversion = "118"
def date = new java.text.SimpleDateFormat("yyyyMMdd").format(new Date())
def versionName = defaultConfig.versionName
outputFileName = "MyApp_${versionName}_${date}_${reversion}.apk"
}
}
Solution 16 - Android
I think this will be helpful.
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
project.ext { appName = 'MyAppName' }
def formattedDate = new Date().format('yyyyMMddHHmmss')
def newName = output.outputFile.name
newName = newName.replace("app-", "$project.ext.appName-")
newName = newName.replace("-release", "-release" + formattedDate)
output.outputFile = new File(output.outputFile.parent, newName)
}
}
}
}
productFlavors {
flavor1 {
}
flavor2 {
proguardFile 'flavor2-rules.pro'
}
}
Solution 17 - Android
put this code in build.gradle(app)
android {
.......
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "Name_of_App.apk"
}}
}
Solution 18 - Android
Simplest way - in build.gradle(app):
android {
compileSdk 31
project.archivesBaseName = "Scanner"
defaultConfig {
And - you will receive : Scanner-release.apk