Skip to content

tslamic/versioning

Repository files navigation

Build Status codecov

Versioning

A Grade plugin that makes versioning easier:

android {
  defaultConfig {
    versionCode versioning.versionCode() // e.g. 42
    versionName versioning.versionName() // e.g. 1.0.0
  }
}

The only prerequisite is using Git as your VCS.

How to use it

Build script snippet for plugins DSL for Gradle 2.1 and later:

plugins {
  id "me.tadej.versioning" version "0.2.0"
}

Build script snippet for use in older Gradle versions or where dynamic configuration is required:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.me.tadej:versioning:0.2.0"
  }
}

apply plugin: "me.tadej.versioning"

How does it work?

The plugin assumes you use tags to mark your releases. The version name corresponds to the git command:

$ git describe --tags --dirty --always

The version code is simply the number of commits:

$ git rev-list --count HEAD

Examples

Assume you're executing the commands below in sequential order.

$ git commit -a -m "First commit."
Op Result
versioning.versionCode() 1
versioning.versionName() "3db5953"
$ git tag -a 0.1.0 -m "First tag."
Op Result
versioning.versionCode() 1
versioning.versionName() "0.1.0"
$ git commit -a -m "Second commit."
Op Result
versioning.versionCode() 2
versioning.versionName() "0.1.0-1-g3db5953"
$ echo "Hi, mom!" > hi.txt && git add hi.txt
Op Result
versioning.versionCode() 2
versioning.versionName() "0.1.0-1-g3db5953-dirty"
$ git commit -m "Third commit."
Op Result
versioning.versionCode() 3
versioning.versionName() "0.1.0-2-g3cbf60f"
$ git tag 0.1.1
Op Result
versioning.versionCode() 3
versioning.versionName() "0.1.1"

Decorators

You can tweak the version name and/or version code:

versioning {
  decorateVersionCode { code -> code * 1000 }
  decorateVersionName { name -> "$name-SNAPSHOT" }
}

License

Copyright 2018 Tadej Slamic

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.