# How to generate or set version number for Android apps

**URL:** <https://discuss.bitrise.io/t/how-to-generate-or-set-version-number-for-android-apps/3837>\
**Category:** Android\
**Created:** [January 22, 2018, 7:30pm UTC](https://discuss.bitrise.io/t/how-to-generate-or-set-version-number-for-android-apps/3837 "2018-01-22T19:30:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![koral](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/koral/32/357_2.png) [@koral](https://discuss.bitrise.io/u/koral)\
**Post date:** [January 22, 2018, 7:30pm UTC](https://discuss.bitrise.io/t/how-to-generate-or-set-version-number-for-android-apps/3837/1 "2018-01-22T19:30:19Z")

</div>

Here are two sources of version number (code and/or name) generation for Android projects.

**Commit timestamp**  
Buildscript (`build.gradle` snippet):

```auto
buildscript {
	repositories {
		jcenter()
	}
	dependencies {
		classpath 'org.ajoberstar:gradle-git:1.7.2'
	}
}

import org.ajoberstar.grgit.Grgit

def commitTimestamp = Grgit.open(currentDir: projectDir).head().time

android {
    defaultConfig {
        versionCode commitTimestamp
        versionName "1.2.$commitTimestamp" //optional
    }
}

```

`versionCode` will always be equal to the timestamp (in seconds) of the last commit on current branch.

Pros:

- works everywhere (both locally and on [bitrise.io](http://bitrise.io)) out of the box, no need for additional steps nor environment variables setup
- the same (commited) source code will always have the same version number
- newer code will have higher version number

Cons:

- may not be suitable for some branching strategies (e.g. if new major version is ready but not released while older one is still maintained and receives new commits)

**Bitrise build number**

```auto
def rawBuildNumber = System.getenv("BITRISE_BUILD_NUMBER") ?: "0"
def buildNumber = Integer.parseInt(rawBuildNumber)

android {
    defaultConfig {
        versionCode buildNumber
        versionName "1.2.$buildNumber" //optional
    }
}

```

Pros:

- newer builds will always have higher version number

Cons:

- works out of the box only on [bitrise.io](http://bitrise.io)

Commit timestamp should be suitable for most of the cases. Build number may be better if you are using dynamic dependency versions/snapshots and/or content downloaded at build time from outside the repository.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 23, 2018, 4:33pm UTC](https://discuss.bitrise.io/t/how-to-generate-or-set-version-number-for-android-apps/3837/2 "2018-01-23T16:33:23Z")

</div>

Thanks for the detailed #how-to @koral ! 🙂

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [February 8, 2018, 3:53pm UTC](https://discuss.bitrise.io/t/how-to-generate-or-set-version-number-for-android-apps/3837/3 "2018-02-08T15:53:47Z")

</div>

Just wanted to mention that this #how-to is super useful, linked it to so many users by now… 😉 Thanks again @koral!
