Last active
December 23, 2015 14:39
-
-
Save skyisle/6650658 to your computer and use it in GitHub Desktop.
get versionCode from manifest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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