Auto ‘versionCode’ increment when building production apk’s

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.

Happy Blocks v1.0 open BETA

UPDATE: As of April 26, 2016, Happy Blocks is officially released to the public.

A public BETA version of my latest game ‘Happy Blocks‘ is available for testing. Any feedback will be greatly appreciated.

If you like, you may give it a try and give me your opinion in the comment section below or at support@vmsoft-bg.com.

Please note that this is a BETA version, so it may have a few bugs!

https://play.google.com/apps/testing/paskov.biz.blocksbreakout

ic_launcher

What ‘Happy Blocks’ has to offer:

Beautiful, well balanced, challenging ball and paddle game featuring 2 game play worlds with total of 150 levels, various power ups and power downs.
‘Happy Blocks’ features:
* 2 game modes – ‘Happy World’ and ‘Classic’ with total of 150 levels.
* 6 different game worlds.
* Mini Games
* 8 different power ups and power downs (‘Mega Ball’, ‘Ball Split’, ‘Extend paddle’, ‘Shooting’, ‘Shrink paddle’, ‘Slow ball’, ‘Glue’, ‘Boom’)
* Horizontal and vertical bombs to help you clear the level quickly
* 25 different type of collectibles to help you unlock the mini games.
* 35 achievements for unlocking
* Leaderboards
* Levels where your objective is to kill flying enemies
* Saved games – save your progress on one device and continue playing on another
* “Optimized for your device” graphics
* Keyboard and tilt support.
* Cheering music and sounds

Notes on playing ‘Classic Mode’
Classic mode is unlocked after completing the 120 levels in ‘Happy World’, in this game mode you start from the beginning when you loose all of your lifes. In classic mode new life is gained every 8000 points.

Playing with keyboard:
‘P’ key will pause / resume the game.
‘Space bar’ will release the ball from the paddle.
‘Left’ arrow will move the paddle left.
‘Right’ arrow will move the paddle right.