Increase version code and version name on Android using Gradle script
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:
- Changes app version inside package.json file.
- Creates commit of changes from step 1.
- Creates git tag.
Welcome, same versioning tool for Android: bumper
prerequirements
- git as source control system
- use semantic version for version name (tl;dr; — should match x.y.z pattern). Good article about Android Versioning.
setup
- Add on top of
app/build.gradle
:
apply from: 'https://gist.githubusercontent.com/IlyaEremin/8821fbf0069e8e60dfeaeefc19afaca2/raw/ce54be4ea6f19b9609f303cd75714d6ec8d916e5/app_version.gradle'
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
- 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!