Android StudioGradle

Generate signed APK file with Specific name Android Studio

Generate Signed APK file

In Android Studio, when clicked menu Build-> Generate Signed APK file, the dialogs displayed for generating APK file, doesn’t allow you to edit the APK file name. It just applies default app name to build file, unlike ADT Eclipse.

Solution to Generate Signed APK file:

We have to write Groovy code in Gradle build file to customize name, Generate singed APK file with Specific name.

Here i’m just giving an example how we can write it.
For eg: If your app name is helloWorld, If you generate APK build file, it will creates helloWorld-release.apk file. But if you want to generate Apk file with appending version name, and change filename like “HelloWorld-1.0v.apk”. You should use setProperty in defaultConfig.

In app project, open build.gradle file, you should have defaultConfig as below

defaultConfig {
    applicationId "com.sample.helloworld"
    minSdkVersion 14
    targetSdkVersion 14
    versionCode 1
    versionName '1.0'
//this below line for Customize APk File name. setProperty("archivesBaseName", "HelloWorld-" + versionName + "v") }


That’s it. Now Goto Build-> Generate a Signed APK… just follow the dialog, click Finish, and you will see APK file created with your specified name.

 

** Below steps doesn’t not work any more.

in this build.gradle file, define a method just above the dependencies, as below

def appendVersionName(variant, defaultConfig) {
    variant.outputs.each { output ->
        if (output.zipAlign) {
            def file = output.outputFile
            def fileName = file.name.replace("helloWorld-release.apk", "HelloWorld-" + defaultConfig.versionName  + "v.apk")
            output.outputFile = new File(file.parent, fileName)
        }
        def file = output.packageApplication.outputFile
        def fileName = file.name.replace("helloWorld-release.apk", "HelloWorld-" + defaultConfig.versionName + "v.apk")
        output.packageApplication.outputFile = new File(file.parent, fileName)
    }
}
dependencies {
    compile files('libs/android-support-v13.jar')
    compile 'com.android.support:appcompat-v7:21.0.2'
}

then search for buildTypes, and call the appendVersionName method as shown below,

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
applicationVariants.all { variant ->
                appendVersionName(variant, defaultConfig)
            }

        }
    }

 

Hope it helps somebody…

Cheers.. 🙂

ref link & credits: http://stackoverflow.com/a/22126638/341443

You may also interested in

5 thoughts on “Generate signed APK file with Specific name Android Studio

    1. @Ray, Yes I agree. I wish i could help on this. I’ll try to create a sample app and attach to this post soon, may be this weekend. Thanks

  1. hello there. i did the same but my file name neither versionName nor verisonCode become “null”
    like 20151029_hello_null.apk . why it become null ? what should i do ? Can you help? thank you

Leave a Reply

Your email address will not be published. Required fields are marked *