Using git commit count for version code for Android

By applying the following in your project gradle and app gradle file, the version code and version name will be auto generated whenever you make a build. You will no longer need to manually update the version code for submitting a new version to the app store.

1. Add this function in the project gradle file.

ext.getVersionCode = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-list', '--first-parent', '--count', 'origin/master'
            standardOutput = stdout
        return Integer.parseInt(stdout.toString().trim())
    catch (ignored) {
        return -1;

2. In the app gradle file, in the defaultConfig, the above function can be used like this

defaultConfig {
    versionCode project.getVersionCode()
    versionName '1.' + project.getVersionCode()

Version Code, it is an integer value which represents the version of the android app, Android OS check for update by using this version code.

Version Name, it is a string value which the user sees on the playstore or the app info on the Android setting. The format convention is (Major).(Minor).(point)

Search within Codexpedia

Custom Search

Search the entire web

Custom Search