Click here to Skip to main content
14,036,655 members
Click here to Skip to main content
Add your own
alternative version

Stats

14.6K views
1 bookmarked
Posted 9 Apr 2016
Licenced CPOL

Changing the Default apk Name in Android

, 9 Apr 2016
Rate this:
Please Sign up or sign in to vote.
Change that default apk name in generated->output directory of your Android application with a script gradle plugin.

Introduction

If you have worked in Android application development, then you must be aware of the default naming convention of generated apk file in Android. It goes something like app-flavor1-flavor2....-debug.apk. It is ok as long as you are debugging the application, but when we have to share the apk file with testers or analysts, we usually give it a meaningful name. This tip/trick helps in automating that process via script plugins in gralde. Script plugin is not really required for this, it can be done by modifying the modules gradle file as well. This trick also makes the readers aware of script plugins and the beautiful extendibility that gradle offers.

Background

I am assuming you are aware of Android development, product flavors and gradle basics. For a quick recap, you can go through this. The code sample below is for Android studio IDE and must not be used in eclipse IDE.

Using the Code

Plugins in gradle can be classified into two categories - binary plugin and script plugin. Binary plugins are the plugins we see in apply plugin "..." statements in Android gradle files. For example, in Android, we always see "apply plugin 'com.android.application'". Script plugins on the other hand are separate gradle files that can be included in standard gradle files of our Android application. Script plugins can be applied using the syntax "apply from '../newgradle.gradle'".

Below are the contents of a gradle file that I like to call apkbeautifier.gradle. This is kept in the root directory of Android project:

android.applicationVariants.all { variant ->
    ;
    def appName
    //Check if an appName property is supplied;
    //if not use the name of the parent folder.
    if (project.hasProperty("applicationName")) {
        appName = applicationName
    } else {
        appName = parent.name
    }

    variant.outputs.each { output ->
        ;
        def newApkName
        if (output.zipAlign) {
            newApkName = "${appName}-${variant.name}.apk"
            //newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk"
            output.outputFile = new File(output.outputFile.parent, newApkName)
        }
    }
}

This code basically loops through all the build variants of the Android application and gives a custom name to the output file, which in our case is an apk file. This first checks if any property called "appName" is supplied from the gradle.properties of the Android project (like: appName=myApp), if not it just picks up the root directory name. It then uses the apkName variable to give a name to the output apk file using the above defined logic.

So now our script plugin is ready and it is named apkbeautifier.gradle. But we are not done yet. We have not yet applied this plugin anywhere. We need to apply this plugin in the standard gradle file of Android project. In your app module's build.gradle file, just add this statement (among other apply statements):

apply from: "../apkbeautifier.gradle"

The above statement assumes the presence of apkbeautifier file in root project folder, if you prefer to keep your script plugin in any other location, just add the relative path to that file in the above statement.

And you are done. Perform a gradle sync, then run and debug your app as normal. Open the outputs folder in generated build directory of your module and you can see the changed apk name.

Points of Interest

Notice the commented out statement inside if block of the code. There is an additional version name property used to name the apk. You can try that too. But I don't recommend using it if your version name changes too frequently. Android studio caches the apk name and if version changes frequently, the apk name will too and you may run into errors like "apk file myApp-flavor1Flavor2-1.2.3.55895 not found".

If you see this error, just comment out the apply from statement perform a gradle sync, then uncomment it and sync again.

History

  • 1.0 - First version by Kaushal Dhruw

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kaushal Dhruw
Software Developer
India India
Just another software developer trying to connect the dots.

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190424.1 | Last Updated 10 Apr 2016
Article Copyright 2016 by Kaushal Dhruw
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid