Configuring Android Project — Version Name & Code by@dmytrodanylyk

Configuring Android Project — Version Name & Code

Dmytro Danylyk HackerNoon profile picture

Dmytro Danylyk

This article is a part of Configuring Android Project series:

  1. Little Things That Matter
  2. Version Name & Code
  3. Static Code Analysis Tools
  4. Continuous Integration
Everything we have discussed in current article is available in template project.

Version Name & Code

Developers usually use some hard coded values for android versionName & versionCode.

Such approach has several disadvantages:

  • You never know which commit represent a specific version.
  • Whenever you increment versionCode and change versionName you have to modify build.gradle file.

If you are using git as your source control system it can also help you to generate android versionName & versionCode. It’s a common practice to use git tags to indicate release of new version.

Version Name

For versionName we can use git describe command.

a. The command finds the most recent tag that is reachable from a commit.
b. If the tag points to the commit, then only the tag is shown.
c. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.

Example (a-b)

  1. Mark a specific commit with tag 1.0
  2. Checkout this commit
  3. Call git describe -tags
  4. Output: 1.0

As you can see if you call git describe on a HEAD commit with some tag, it will output this tag.

Example (a-c)

  1. Mark a commit with tag 1.0
  2. Add two more commits
  3. Call git describe -tags
  4. Output: 1.0-2-gdca226a

Using git commit hash “1.0–2-gdca226a we can easily find out from which specific commit build was made.


Version Code

For versionCode we can use total number of tags. Because every git tag indicate some version, versionCode for next version will be always greater then previous.


In the example above we have 3 tags. This value will be used for our versionCode.

However we are not going to create a git tag for every intermediate version, so for dev build we can use a timestamp of HEAD commit.


In the example above a timestamp of HEAD commit is equal to 1484407970 (seconds since the UNIX epoch January 1, 1970 00:00:00 UTC). This value will be used for our versionCode. If you want to convert it to a human readable date use site. In our case it’s Sat Jan 14 2017 15:32:50 UTC.

Groovy way to use git

To work with git I suggest to use a library called grgit. Create script-git-version.gradle file with following content:

Apply it to your build.gradle file:

To check if version name and code are generated correctly call gradle task ./gradlew printVersion which gives you similar output:

Finally use gitVersionName, gitVersionCode and gitVersionCodeTime variables in your build.gradle file.

Run project and verify app version.


Benefits of such approach:

  • No need to modify build.gradle file - versionCode and versionName are generated automatically.
  • You can easily find out from which commit build was made.
Note: you can experiment with version name even more: include branch name, timestamp, etc.


Signup or Login to Join the Discussion


Related Stories