Increase version code and version name on Android using Gradle script

Ilya Eremin
2 min readSep 18, 2017

--

intro

In JS world there is nice package manager called npm. It has nice command:

npm version [patch|minor|major]

After I tried it I immediately wanted same tool for Android.

what the hell npm version does

It increases your app version. For example, your app version is 1.0.0. Following table shows how command affect on version number:

npm version patch        ->   1.0.1npm version minor        ->   1.1.0npm version major        ->   2.0.0

This command internally makes 3 things:

  1. Changes app version inside package.json file.
  2. Creates commit of changes from step 1.
  3. Creates git tag.

Welcome, same versioning tool for Android: bumper

prerequirements

setup

  1. Add on top of app/build.gradle:
apply from: 'https://gist.githubusercontent.com/IlyaEremin/8821fbf0069e8e60dfeaeefc19afaca2/raw/ce54be4ea6f19b9609f303cd75714d6ec8d916e5/app_version.gradle'

Sources of script

2. Run

./gradlew bumperInit -PbumperVersionName=1.0.0 -PbumperVersionCode=1

It creates file app/gradle.properties with following content:

appVersionName=1.0.0
appVersionCode=1

If such file exists then adds values to the end of file.

3. Inside app/build.gradle change

android {
...
defaultConfig {
versionCode 1
versionName 1.0.0
...
}
}

to

android {
...
defaultConfig {
versionCode appVersionCode.toInteger()
versionName appVersionName
...
}
}

how to use

  1. Run

./gradlew bumperVersionPatch

  • version code of your app has been increased by 1
  • last number (patch) of version name has been increased (1.0.0 -> 1.0.1)
  • new commit with message “Release 1.0.1” has been created
  • new tag with name “v1.0.1” has been created

There are 2 more commands:

./gradlew bumperVersionMinor./gradlew bumperVersionMajor

to increase minor and major number accordingly.

Thank you for reading, hope it’s useful. Please leave comments with your suggestions. I’m very new in gradle plugin world!

--

--