Skip to content

Instantly share code, notes, and snippets.

@mar-v-in
Created March 22, 2016 13:28
Show Gist options
  • Save mar-v-in/280cec64b20a45b4974c to your computer and use it in GitHub Desktop.
Save mar-v-in/280cec64b20a45b4974c to your computer and use it in GitHub Desktop.
A small gradle helper script to determine package name, version code and version name for android gradle projects, including flavors.
// Usage: gradle -q -I version.gradle showVersionInfo
class VersionHelper {
String project;
String packageName;
String versionCode;
String versionName;
VersionHelper(project, config) {
this(project, config.applicationId, config.versionCode, config.versionName);
}
VersionHelper(project, packageName, versionCode, versionName) {
this.project = project;
this.packageName = packageName;
this.versionCode = versionCode;
this.versionName = versionName;
}
VersionHelper withManifest(manifest) {
if (!manifest.exists()) {
return this;
}
if (packageName != null && versionCode != null && versionName != null) {
// We already know everything
return this;
}
def rootNode = new XmlParser().parse(manifest);
return withManifestValues(rootNode.attribute("package"),
rootNode.attribute(new groovy.xml.QName("http://schemas.android.com/apk/res/android", "versionCode")),
rootNode.attribute(new groovy.xml.QName("http://schemas.android.com/apk/res/android", "versionName")));
}
VersionHelper withManifestValues(packageName, versionCode, versionName) {
return new VersionHelper(project,
this.packageName == null ? packageName : this.packageName,
this.versionCode == null ? versionCode : this.versionCode,
this.versionName == null ? versionName : this.versionName);
}
VersionHelper withFlavor(flavor) {
return withFlavor(flavor.name, flavor.hasProperty("applicationId") ? flavor.applicationId : null, flavor.versionCode, flavor.versionName);
}
VersionHelper withFlavor(flavorName, packageName, versionCode, versionName) {
return new VersionHelper(project + "#" + flavorName,
packageName == null ? this.packageName : packageName,
versionCode == null ? this.versionCode : versionCode,
versionName == null ? this.versionName : versionName);
}
String toString() {
return "<application xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
(project == null ? "" : "project=\"" + project + "\" ") +
(packageName == null ? "" : "package=\"" + packageName + "\" ") +
(versionCode == null ? "" : "android:versionCode=\"" + versionCode + "\" ") +
(versionName == null ? "" : "android:versionName=\"" + versionName + "\" ") + "/>";
}
}
allprojects {
task showVersionInfo << {
if (project.hasProperty("android")) {
VersionHelper version = new VersionHelper(project.name, android.defaultConfig);
if (android.productFlavors.isEmpty()) {
println version.withManifest(project.file(android.sourceSets.main.manifest.toString()));
} else {
android.productFlavors.each {
VersionHelper flavor = version.withFlavor(it)
.withManifest(project.file(android.sourceSets.getByName(it.name).manifest.toString()))
.withManifest(project.file(android.sourceSets.main.manifest.toString()));
println flavor;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment