Skip to content

Instantly share code, notes, and snippets.

@tunjos
Created July 13, 2016 19:39
Show Gist options
  • Select an option

  • Save tunjos/9d833a2cde1832ad0b7882a62cd812d2 to your computer and use it in GitHub Desktop.

Select an option

Save tunjos/9d833a2cde1832ad0b7882a62cd812d2 to your computer and use it in GitHub Desktop.

Revisions

  1. tunjos created this gist Jul 13, 2016.
    4 changes: 4 additions & 0 deletions app_build.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    defaultConfig {
    versionCode buildVersionCode()
    versionName version
    }
    3 changes: 3 additions & 0 deletions build.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    // Top-level build file where you can add configuration options common to all sub-projects/modules.

    apply from: 'versioning.gradle'
    1 change: 1 addition & 0 deletions gradle.properties
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    version=0.1.0-SNAPSHOT
    35 changes: 35 additions & 0 deletions versioning.gradle
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    ext {
    /**
    * Builds an Android version code from the version of the project.
    * This is designed to handle the -SNAPSHOT and -RC format.
    *
    * I.e. during development the version ends with -SNAPSHOT. As the code stabilizes and release nears
    * one or many Release Candidates are tagged. These all end with "-RC1", "-RC2" etc.
    * And the final release is without any suffix.
    * @return
    */
    buildVersionCode = {
    //The rules is as follows:
    //-SNAPSHOT counts as 0
    //-RC* counts as the RC number, i.e. 1 to 98
    //final release counts as 99.
    //Thus you can only have 98 Release Candidates, which ought to be enough for everyone

    def candidate = "99"
    def (major, minor, patch) = version.toLowerCase().replaceAll('-', '').tokenize('.')
    if (patch.endsWith("snapshot")) {
    candidate = "0"
    patch = patch.replaceAll("[^0-9]","")
    } else {
    def rc
    (patch, rc) = patch.tokenize("rc")
    if (rc) {
    candidate = rc
    }
    }

    (major, minor, patch, candidate) = [major, minor, patch, candidate].collect{it.toInteger()}

    (major * 1000000) + (minor * 10000) + (patch * 100) + candidate;
    }
    }