Skip to content

Instantly share code, notes, and snippets.

@tmtrademarked
Last active January 3, 2017 00:04
Show Gist options
  • Save tmtrademarked/73a0f66e80e53b7c26a2922268ea6d68 to your computer and use it in GitHub Desktop.
Save tmtrademarked/73a0f66e80e53b7c26a2922268ea6d68 to your computer and use it in GitHub Desktop.
Gradle Versioning
// In root-level build.gradle
def getBaseVersionCode = { ->
// Get the version code relative to the current branch. Will typically be slightly ahead of master.
def current = "git rev-parse --abbrev-ref HEAD".execute()
def cmd = "git rev-list " + current.text + " --first-parent --count"
def process = cmd.execute()
return process.text.toInteger()
}
def getBaseVersionName = { ->
return "git describe --tags".execute().text.trim();
}
// Helper function to create the relevant version code.
ext.getVersionCode = { int inflation ->
assert inflation < 1000: "Inflation can only be three digits"
return getBaseVersionCode() * 1000 + inflation;
}
// Helper function to append a suffix if provided
ext.getVersionName = { String suffix ->
if (suffix == null) {
return getBaseVersionName();
}
return getBaseVersionName() + suffix;
}
// In module-level build.gradle:
android {
// Set the version code and name based on variant.
applicationVariants.all { variant ->
def flavor = variant.mergedFlavor
def versionIncrement = 20;
def versionSuffix = "";
def variantName = variant.getName();
if (variantName.contains("alpha")) {
versionIncrement = 10;
versionSuffix = "-alpha"
} else if (variantName.contains("debug")) {
versionIncrement = 0;
versionSuffix = "-debug";
}
flavor.versionName = getVersionName(versionSuffix);
flavor.versionCode = getVersionCode(versionIncrement)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment