Gradle power – Automatically generate Android App Version Code and Name

One recommended practice with Android app development is upgrading app version code and name in gradle file for every code change. We have been doing this manually all this time, there is an additional overhead (time) of gradle syncing for every change that happens in build gradle files. This trick automates that process using simple gradle methods and logic. Even if you forget to upgrade the version, this trick will take care of it.

It is not mandatory but I recommend going through the basics of product flavours in Android. We will be upgrading versions of default config and all the flavours. You can Google it up or check out this link.

Android apps version code and name are defined in the module’s build.gradle file. Same goes for all the product flavours. All are defined inside Android block. Here is a sample:

android {
...
    defaultConfig {
        applicationId "app.package.default"
        versionCode 1
        versionName '1.0'
        ...
    }

    productFlavors {
        free {
            applicationId "app.package.free"
            versionCode 101
            versionName '1.101.1'
            ...
        }
        pro {
           applicationId "app.package.pro"
           versionCode 301
           versionName '1.301.3'
           ...
        }
        freemium {
           applicationId "app.package.freemium"
           versionCode 501
           versionName '1.501.5'
           ...
        }
    }
...
}

FYI, version code and name can be overridden in flavours as shown above. In fact, any property of default config can be overriden in flavours. Both use the same DSL object for configuration.

As you can see, we have to manually write “1.0” and 1 in version name and code respectively. And when we change that, we need to wait for gradle sync to finish. Now let’s see how to automate this process.

We are going to create a method that calculates an upgraded number and we will use this method in version name and code. Like this:

versionCode getCustomNumber()
versionName "1.0." + getAnotherOrSameCustomNumber()

This method in defined in project level build.gradle file. You can use any logic in getCustomNumber() and getAnotherOrSameCustomNumber() that returns an upgraded number every time it is called. When you define this, make sure it returns an upgraded number as a lower version code will not upgrade your app. A simple logic based on timestamp is defined below.

def getCustomNumber() {
    def date = new Date()
    def formattedDate = date.format('yyMMddHHmm')
    def code = formattedDate.toInteger()
    return code
}

allprojects {
    version = '1.0.' + getCustomVersionCode()
    repositories {
        jcenter()
        mavenCentral()
    }
}

Defining the base version in allProjects block is important, or else you will see a null version code and name and may result in build failure. Here’s how we can use the above defined method in app level’s build.gradle file (including product flavours).

android {
    ...
    defaultConfig {
        applicationId "app.package.default"
        versionCode getCustomVersionCode()
        versionName '1.0.' + getCustomVersionCode()
        ...
    }

    productFlavors {
        free {
            applicationId "app.package.free"
            versionName '1.101.' + getCustomVersionCode()
            ...
        }
        pro {
            applicationId "app.package.pro"
            versionName '1.301.' + getCustomVersionCode()
            ...
        }
        freemium {
            applicationId "app.package.freemium"
            versionName '1.501.' + getCustomVersionCode()
            ...
        }
        ...
    }
}

You can even use the revision of the source control you use in your project, just need to find the right gradle plugin your source control.

If you remove the base version in allProject block of project level build.gradle file, you will see a “null” in version code in generated manifest. And your app may or may not run. So please avoid it.

Let me know what you think.

-Kaushal D (@drulabs twitter/github)

drulabs@gmail.com

Gradle power – changing default apk name via script plugin

Android studio is the IDE for android application development which uses gradle for build automation. Yet android developers (including me) are not aware of the power gradle packs with it. The level of customization that gradle offers is amazing. This is the first post of “power of gradle in android studio” from my blog, I hope it helps you become a better android developer.

This post is not about gradle basics, it is about using gradle to suit our needs. I recommend taking a look at this presentation for understanding just enough gradle to get started, it is optional though if all you want is to change the default apk name.

There are many ways to change that default apk name that goes something like “app-flavor1-flavor2…-debug.apk“. One way is to create a gradle task, another is to loop through all the build variants etc. In this post we will see how to use a script plugin to change that name.

There are basically two types of plugins in gradle, script plugin and binary plugin. I am sure you have seen binary plugins. Whenever we use apply plugin syntax we are applying binary plugin. for example this is how android plugin is applied in android applications: apply plugin: “com.android.application”. Script plugins are separate gradle files that are included in the application using the syntax: apply from “../newgradlefile.gradle”.

Let’s see how we can create a script plugin for changing the default apk name in android applications. Below is the content of a gradle file that I named apknomenclature.gradle, this file in the root directory of android project:

android.applicationVariants.all { variant ->;
     def appName
     //Check if an applicationName property is supplied;
     //if not use the name of the parent folder name.
     if (project.hasProperty("applicationName")) {
          appName = applicationName
     } else {
          appName = parent.name
     }
    variant.outputs.each { output ->;
         def newApkName
         if (output.zipAlign) {
              newApkName = "${appName}-${variant.name}.apk"
              output.outputFile = new File(output.outputFile.parent, newApkName)
         }
    }
}

This is the content of apknomenclature.gradle file in the root folder of my android application. What this is basically doing is looping through all the build variants of android application and changing the output file. It checks if there is any property called applicationName in gradle.properties file (applicationName=MyCustomAppName). If it is there, it takes that name or else picks the root folder’s name and renames the apk as per logic defined. This change doesn’t change anything except the name of the apk, you can run and debug your application like you did earlier.

The zipAlign if condition is only to change the name of signed and aligned apk not the unaligned one. We don’t need to bother about unaligned apks anyway.

Notice the variable called newApkName, you can add your custom logic to change this variable’s value, which will be reflected in resulting apk name. You can add timestamp or version code of your app, but I wouldn’t recommend using that in the apk name as the name is cached in android studio. So if you change version code very frequently in your app you will notice this error “apk “your_apk_name_timeinmillis_…” doen’t exist…“. So just keep the name in the code above or use something that doesn’t change frequently.

Now that our apknomenclature.gradle file or our script plugin is ready, we need to apply it to app level build gradle file like this “apply from “../apknomenclature.gralde”“.

This will change the apk name in the outputs directory. If you run into any issues, just comment the apply from line of code run the app, then uncomment it and run again.

I know its not much to change the apk name, but its a start. Watch out this space for more.

Let me know what you think. Happy coding!!!

Have questions? Did I miss anything? Shoot below in comments

-Kaushal D (@drulabs twitter/github)

drulabs@gmail.com