An easy Gradle plugin that follows semver.org rules to automatically generate the Patch version, Build number and Code version, while Major, Minor and Pre-Release suffix remain under our control.
I saw plenty of plugins that require a long configuration and continuous adjustment just to update those numbers, if so, better without any plugin then! With this plugin we are required to manage only 2 variables.
Inspired from Android Studio Automatic Incremental Gradle Versioning. Customized into a plugin with PreRelease, Auto-Reset and Sub-Modules features.
💬 Easy to apply, it works with any project type with sub modules too.
major: Required, user defined value for breaking changes.
minor: Required, user defined value for new features, but backwards compatible. If you increase Major version, this value must be coherent(=0).
patch: Optional, user defined value (or auto-generated value) for backwards compatible bug fixes only.
preRelease: Optional, user defined value for pre-releases suffix.
incrementBuild: Optional, set to false to disable build number auto-increment during development (default: true).
incrementOn: Optional, custom task name to trigger the increase of the version and save. By default, any assemble*Release, bundle*Release (including flavors) and grabverRelease are already detected.
patch - If not specified by user, increases at each release, but it auto resets back to 0 when Minor or Major version changes or if preRelease is set.
build - Increases at each build.
code - Increases at each release.
Configure pluginManagement in settings.gradle file:
pluginManagement {
repositories {
gradlePluginPortal()
}
plugins {
id "eu.davidea.grabver" version "2.1.0"
}
}Configure build script in each build.gradle file:
plugins {
id "eu.davidea.grabver"
}Apply the plugin in the module you desire, it will create a version.properties file under that module!
plugins {
...
id 'eu.davidea.grabver'
}
versioning {
// Required (number)
major 1
minor 0
// Optional, force custom patch (number)
patch 7
// Optional (any string)
preRelease "RC1"
// Optional, disable build number increment during development (default: true)
incrementBuild false
// Optional, custom task name to trigger the increase of the version and save
incrementOn "<task-name>"
}💬 Note: The file
version.propertiesis auto-generated, but once it's created, you can modify its content as of your convenience. Just remember to add it to your Version Control System (from time to time).
versioning.major
versioning.minor
versioning.patch
versioning.build
versioning.preRelease
versioning.code // auto-incremental integer, needed for all Android Apps
versioning.name // output: "major.minor.patch[-preRelease]"
versioning.fullName // output: "major.minor.patch[-preRelease] #buildNr built on yyyy.MM.dd"
versioning.builtOn // output: " built on yyyy.MM.dd"
versioning.date // or versioning.getDate([format]) - default "yyyy.MM.dd"
// Example: grab the version name
version = versioning.name
⚠️ Note: To trigger the evaluation, user must grab one ofmajorminorpatchbuildcodenamefullNameattribute!
Via command line:
/**
* Debug: increments build number only.
* Code and Patch remain unchanged.
*/
gradle [build | jar | war | explodedWar]
/**
* Release: increments build, patch and code.
* - Code is incremented if a release task is invoked.
* - Patch may be reset if Major or Minor changed or if preRelease is set.
*/
gradle [grabverRelease]
The plugin recognizes Android tasks including product flavors:
gradle [assembleDebug | bundleDebug | assembleFlavorDebug | bundleFlavorDebug]
gradle [assembleRelease | bundleRelease | assembleFlavorRelease | bundleFlavorRelease]
In Android Studio:
- Build > Generate Bundle / APK (assembleDebug | bundleDebug)
- Build > Generate Signed Bundle / APK (assembleRelease | bundleRelease)
- Run the App (assembleDebug | assembleRelease, depending on the build variant)
The plugin evaluates the run tasks in silent mode to automatically skip the version evaluation
and to not print unnecessary logs if no relevant task was detected. A single log line is however produced.
Single tasks such as clean, test, flyway and all third-party plugin tasks will not trigger
the versioning evaluation.
Default tasks defined via defaultTasks are also evaluated together with command line tasks. Example:
| Command | Outcome |
|---|---|
gradle clean test |
Evaluation skipped |
gradle javadoc |
Evaluation skipped |
gradle flywayClean |
Evaluation skipped |
gradle clean build |
Evaluation triggered and new values saved |
gradle clean war grabverRelease |
Evaluation triggered, versioning increased and new values saved |
defaultTasks('build') + gradle |
Evaluation triggered via default tasks |
By default, the build number increments on every build. To avoid constant version.properties changes during
development (useful for Git workflows), disable it:
versioning {
major 1
minor 2
incrementBuild false
}Scenarios (when flag=false):
- Debug builds → Build number unchanged → File not saved
- Release builds → Code/patch/build increment → File saved
- Manual version changes (major/minor/patch/preRelease) → File saved
💡 Tip: Toggle
incrementBuildbased on your workflow needs.
See CHANGELOG.md for all notable changes.
Everybody is welcome to improve existing solution. Many features and bug fixes already come from user requests - don't hesitate to open an issue, even if it may take some time to address.
💬 Note: Unit tests work fine if you open the project with IntelliJ Idea, while with Android Studio they don't. Alternatively, you can simulate a real build script by running
gradlew publishToMavenLocalandgradlew -b build-test.gradle build [<release task>]OR testing within modules_a/bgradlew build [<release task>].
Additional instructions insidebuild-test.gradle.
Copyright 2017-2026 Davidea Solutions Srl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
