Skip to content

shubham230523/AutoVersionGradle

Repository files navigation

Auto Version Gradle Plugin

A Gradle plugin that automatically generates Android version code and version name based on git commits count, date, and Semantic Versioning

Features

  • Automatic Version Code Generation: Generate version codes based on git commit count or auto-increment
  • Semantic Versioning Support: Configure version names with major, minor, patch, and build numbers
  • APK/AAB Renaming: Customize output file names with version information
  • CI/CD Friendly: Works seamlessly with GitHub Actions, Jenkins, and other CI systems
  • Android Gradle Plugin Compatibility: Supports modern Android Gradle Plugin versions

Contents

  1. Installation
  2. How to use
  3. Version Name Configuration
  4. Version Code Configuration
  5. File output options

Installation

Add the auto-version-gradle plugin to your build script and use the property advancedVersioning.versionName and advancedVersioning.versionCode where you need:

Auto Version Gradle Minimum AGP Version
4.0.0 9.0.0
3.5.0 8.1.0
3.0.0 8.1.0
2.0.2 8.0.0
2.0.0 7.0.0
1.7.3 3.0.0

Using the plugins DSL:

plugins {
    id("me.gradleadvanceversion.auto-version-gradle") version "4.0.0"
}

How to use

advancedVersioning {
  nameOptions { }
  codeOptions { }
  outputOptions { }
}

def appVersionName = advancedVersioning.versionName
def appVersionCode = advancedVersioning.versionCode

Version Name Configuration

You can customize version name in your build.gradle file as follows:

advancedVersioning {
  nameOptions {
    versionMajor 1
    versionMinor 3
    versionPatch 6
    versionBuild 8
  }
}

The above configuration will output 1.3.6.8

There is no need to specify all params because they will be handled automatically. For example:

advancedVersioning {
  nameOptions {
    versionMajor 1
    versionBuild 8
  }
}

Will output 1.0.0.8 and

advancedVersioning {
  nameOptions {
    versionMajor 1
    versionMinor 3
  }
}

Will output 1.3

Version Code Configuration

To customize version code in your build.gradle file:

Groovy:

advancedVersioning {
  codeOptions {
    versionCodeType 'GIT_COMMIT_COUNT'
  }
}

Kotlin:

import me.gradleadvanceversion.gradle.auto-version-gradle.gradleextensions.VersionCodeType.*

advancedVersioning {
  codeOptions {
    versionCodeType(GIT_COMMIT_COUNT)
  }
}

versionCodeType can be one of the following values:

  • GIT_COMMIT_COUNT — total number of commits in the current branch
  • AUTO_INCREMENT_STEP — file-based auto-increment (e.g. 26)
  • AUTO_INCREMENT_DATE — date-based auto-increment (YYMMddHHmm format)

GIT_COMMIT_COUNT

If you are using CIs like Jenkins, CircleCI or GitHub Actions, and you want to use GIT_COMMIT_COUNT, you should clone your repository with full history or unshallow an already existing one.

For GitHub Actions, use fetch-depth: 0:

steps:
- name: Checkout
  uses: actions/checkout@v4
  with:
    fetch-depth: 0

AUTO_INCREMENT_STEP

Stores AI_VERSION_CODE in a version.properties file in the build.gradle directory. You can change the dependsOnTasks property to specify which tasks should increase the version code (default is every task that contains 'release' in its name).

You can set a step different from 1:

advancedVersioning {
  codeOptions {
    versionCodeType 'AUTO_INCREMENT_STEP'
    versionCodeStep 2 // default is 1
  }
}

Migration Support

If you are migrating from a different versioning system, you can use lastLegacyCode to add an offset to the generated version code:

advancedVersioning {
  codeOptions {
    versionCodeType 'GIT_COMMIT_COUNT'
    lastLegacyCode 987650
  }
}

The final version code will be lastLegacyCode + generatedCode.

File Output Options

You can rename the output APK/AAB file by enabling the renameOutput option:

advancedVersioning {
  outputOptions {
    renameOutput true
  }
}

If your app name is MyApp with 2.7 version name, and you are in debug mode, the output APK file name will be: MyApp-2.7-debug.apk

Custom Output Name Format

You can customize the output name by using these parameters:

  • ${appName}: name of main module
  • ${projectName}: name of root project
  • ${flavorName}: flavor name
  • ${buildType}: build type
  • ${versionName}: version name
  • ${versionCode}: version code

Groovy:

advancedVersioning {
  outputOptions {
    renameOutput true
    nameFormat '${appName}-${buildType}-${versionName}'
  }
}

Kotlin:

advancedVersioning {
  outputOptions {
    renameOutput(true)
    nameFormat("\${appName}-\${buildType}-\${versionName}-\${versionCode}")
  }
}

Custom String Examples

Groovy:

advancedVersioning {
  outputOptions {
    renameOutput true
    nameFormat '${appName}-google-play-${versionName}'
  }
}

Kotlin:

advancedVersioning {
  outputOptions {
    renameOutput(true)
    nameFormat("\${appName}-google-play-\${versionName}")
  }
}

If your app name is MyApp with 4.6.1 version name, the output APK file name will be: MyApp-google-play-4.6.1.apk

Requirements

  • Java: 17+
  • Gradle: 8.0+
  • Android Gradle Plugin: 7.0+ (recommended 9.0+)

About

A Gradle plugin that automatically generates Android version code and version name based on git commits count, date, and Semantic Versioning

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages