This Gradle plugin leverages Git to manage project versioning, adhering to semantic versioning principles. It provides several customization options, especially useful for development and debugging purposes.
Add the plugin to your build.gradle.kts file:
plugins {
id("io.github.offrange.git-semantic-versioning")
}Note
Customization options like defaultIncrement and channel are intended for development versions where no Git tag has
been set yet. These customizations help in displaying the correct version during development and debugging.
Note
An active Git tag on the latest commit will always override these custom modifications.
For instance, if you've published a beta version 1.0.0-beta.1 and are working on a release candidate, set the channel
to Channel.RC to ensure the version updates to 1.0.0-rc<+build-metadata>.
versioning {
channel = Channel.RC
}If the release channel of the last version (determined by the latest git tag) and the channel property of the
extension is Channel.STABLE , or if the release channel of the last version
is newer than the release channel defined in the extension, the version is incremented by a strategy defined by
the defaultIncrement property
versioning {
defaultIncrement = Inc.MINOR
}The defaultIncrement property is used to define which part of the version should be incremented under certain
conditions. Specifically, this property comes into play when:
-
The current git tag indicates that the project's version is stable, and there has been at least one commit since this tag. If the
channelproperty is also set toChannel.STABLE, it implies that a new version is required. ThedefaultIncrementproperty will determine which version part (e.g., major, minor, or patch) should be incremented. -
The release channel defined by the
channelproperty is set to a "more unstable" version than the release channel of the current/latest git tag version. For example, if the latest git tag is "1.0.0-rc.2" and thechannelparameter is set toChannel.BETA, the version will be incremented to "1.1.0-beta.1" (see the configuration below).
Here is how you can set the defaultIncrement property in your versioning configuration:
versioning {
defaultIncrement = Inc.MINOR
}If you prefer including the full commit hash in the version name, set the useShortHash property to false.
versioning {
useShortHash = false
}For Android projects, a default version code generator is provided. If you need a custom generator, specify it like this:
versioning {
versionCodeGenerator = { version ->
computeUIntVersionCode(version)
}
}To use this plugin for versioning your Android app, follow these steps:
- Apply the plugin in your
build.gradle.ktsorbuild.gradlefile as described in the Getting Started section. - Remove or comment out the
versionNameandversionCodeproperties in thedefaultConfigblock within theapplicationextension. - Apply the generated versioning source set
build/generated/versioning/kotlinto your build.gradle.kts file. - Apply
android versionedBy versioningto the root of your build.gradle.kts file - Apply
build/generated/versioning/kotlinto your source set - Add
io.github.z4kn4fein:semver:2.0.0dependency
android versionedBy versioning
android {
defaultConfig {
applicationId = "com.example.app"
minSdk = libs.versions.android.minSdk.get().toInt()
targetSdk = libs.versions.android.compileSdk.get().toInt()
// versionName = "1.0.0"
// versionCode = 1
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
sourceSets.getByName("main") {
kotlin.srcDir("build/generated/versioning/kotlin")
}
}
dependencies {
implementation("io.github.z4kn4fein:semver:2.0.0")
}The plugin will automatically set the appropriate version name and version code based on the Git tags.