Monday, July 6, 2015

Change APK File Name in Gradle

By default, the Gradle build in Android Studio names the .apk file app-release.apk. With a few small changes to the app build.gradle file, the .apk name can be changed to <app name>-release-<version>.apk.

The file that needs to be edited is the application build.gradle file. It is located in the app directory of your Android Studio project.

First step is to set project.archivesBaseName to your application name.


android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    project.archivesBaseName = "AppName";

Second step is to add some code to the buildTypes section of the Gradle file

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            applicationVariants.all { variant ->
                variant.outputs.each{ output ->
                    output.outputFile = new File(output.outputFile.parent,output.outputFile.name.replace(".apk","-" + defaultConfig.versionName + ".apk" ))
                }
            }
        }
    }

And that is all it takes to rename the .apk file.