Auto ‘versionCode’ increment when building production apk’s

Android, Smartphone, Android Developer, Android Studio

Since I adopted Fabric as a way to monitor vital app stats such as ‘Time in App per User‘ and it’s Beta distribution platform to distribute test builds, increasing APK’s versionCode numbers became a tedious task.

I decided to simplify the things by letting Gradle to do auto versionCode increments when producing release APK’s

Our implementation of build number increments will consist of a property file named version.properties located in the root folder of our project.

The property file will contain 2 variables, one defining the version name such as “2.3” and one defining the version code such as 15

VERSION_NAME=2.3
VERSION_CODE=19

In our app module build.gradle file

build.gradle

we will define a function which takes care of retrieving the a bough mentioned values from the properties file and increment the VERSION_CODE if needed.

/**
 * Get's value from 'version.properties' file
 * @param varName the name of the variable which value we wan't to get.
 * @return the variable value.
 */
def getVersionPropertiesValue(def varName)
{
    def propertiesFile = file('version.properties')
 
    if(!propertiesFile.canRead()) {
        throw new GradleException("Could not read " + propertiesFile.name)
    }
 
    Properties properties = new Properties();
    properties.load(new FileInputStream(propertiesFile))
 
    def propertyValue = properties[varName]
    if(varName == 'VERSION_CODE')
    {
        // If we are building release increment the version code
        List gradleTasksNames = gradle.startParameter.getTaskNames();
        for(String taskName : gradleTasksNames)
        {
            if(taskName.contains("Release"))
            {
                propertyValue = propertyValue.toInteger() + 1
                properties[varName] = propertyValue.toString()
                properties.store(propertiesFile.newWriter(), null)
                break
            }
        }
    }
 
    return propertyValue
}

In the defaultConfig section of the gradle build script we will call this function to retrieve values for the versionName and versionCode of our app.

android {
    compileSdkVersion 28
 
    defaultConfig {
        applicationId "com.example.foo"
        versionCode Integer.valueOf(getVersionPropertiesValue('VERSION_CODE'))
        versionName getVersionPropertiesValue('VERSION_NAME')
        minSdkVersion 14
        targetSdkVersion 28
    }
}

Now each time a release build is made, the version code will increment automatically. If we want to change the version name we can do so by changing the value of VERSION_NAME property.

GSM Signal Monitor 1.5 out now!

1-featuredimage
GSM Signal Monitor 1.5 is out now!
This version features new app visual style, additional logging options, bug fixes and Android 5.x and up support.

Version 1.5 Change Log
* New app visual style
* Compatibility with Android 5.x and up
* Fixed bug when clicking over the app notification in the notification area
* GSM Signal monitor will now log (optional) information when the phone is entering roaming state and changes in your mobile data connectivity.
* Fixed bug when no notifications will be made on some devices
* New Google play services version

Get it on Google Play

Alien Swarm 1.4.1 out now!

Alien Swarm version 1.4.1 is out now!

This is the best ‘Alien Swarm’ release to date. Featuring Facebook and Google+ share integration, so you can share your experience with friends and family. Sound and performance improvements, few bug fixes and more. Check it out!

Version 1.4.1 Change Log:
* Facebook and Google+ share improvements.
* Fixed visual bug when the game is running on tablets with Android 2.3.x
* Fixed crash when running on devices with Android 2.3.x
* Removed the ‘-1’ sign when hitting an enemy with a health bar.
* Sound improvements
* Small apk size reduction
* Reduced start up time

google-play-badge

Tic Tac Toe 4.0.1

web-slider

Tic Tac Toe v4.0.1 is here. This release focus is on improving performance, fixing visual glitches and bugs.

Version 4.0.1 Change log:
* Fixed crash occurring when the device is rotated.
* Minor UI improvements.
* Updated ‘Google Play Services’ to version 9.2.1

google-play-badge