[android] " app-release.apk" how to change this default generated apk name

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)enter image description here

This question is related to android android-studio

The answer is


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


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.


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

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


Not renaming it, but perhaps generating the name correctly in the first place would help? Change apk name with Gradle


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

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


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


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


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

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

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:

  1. all is used now instead of each to iterate over the variant outputs.
  2. outputFileName property is used instead of mutating a file reference.