Skip to content

Instantly share code, notes, and snippets.

@skyisle
Last active December 23, 2015 14:39
Show Gist options
  • Save skyisle/6650658 to your computer and use it in GitHub Desktop.
Save skyisle/6650658 to your computer and use it in GitHub Desktop.
get versionCode from manifest
def getVersionCodeFromManifest() {
def manifestContent = file(android.sourceSets.main.manifest.srcFile).getText()
def matcher = (~"versionCode=\"(\\d+)\"").matcher(manifestContent)
matcher.find()
return Integer.parseInt(matcher.group(1))
}
def getVersionCodeFromManifestLong() {
def manifestFile = android.sourceSets.main.manifest.srcFile
def xpath = XPathFactory.newInstance().newXPath()
xpath.setNamespaceContext(new NamespaceContext() {
final String nsPrefix = "android";
final String nsURI = "http://schemas.android.com/apk/res/android"
public String getNamespaceURI(String prefix) {
if (nsPrefix.equals(prefix)) {
return nsURI;
}
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String namespaceURI) {
if (nsURI.equals(namespaceURI)) {
return nsPrefix;
}
return null;
}
public Iterator getPrefixes(String namespaceURI) {
if (nsURI.equals(namespaceURI)) {
ArrayList<String> list = new ArrayList<String>();
list.add(nsPrefix);
return list.iterator();
}
return null;
}
})
return Integer.parseInt(xpath.evaluate("/manifest/@android:versionCode",
new InputSource(new FileInputStream(manifestFile))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment