Created
August 2, 2017 17:34
-
-
Save jasonwyatt/7af5cb179533e003388f424b63e5c188 to your computer and use it in GitHub Desktop.
Get versionName and versionCode from APK file.
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 getAaptPath = { -> | |
def sdkDir = android.sdkDirectory | |
def buildToolsDir = new File(sdkDir, "build-tools") | |
def versions = buildToolsDir.list() | |
def latestBuildToolsDir = new File(buildToolsDir, versions[-1]) | |
return "${new File(latestBuildToolsDir, "aapt").absoluteFile}" | |
} |
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 getVersionInfo = { File apkFile -> | |
def stdout = new ByteArrayOutputStream() | |
exec { | |
commandLine getAaptPath(), "dump", "badging", apkFile.absoluteFile | |
standardOutput = stdout | |
} | |
return new VersionInfo(apkFile, stdout.toString()) | |
} |
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
import java.util.regex.Pattern | |
class VersionInfo { | |
private static Pattern VERSION_NAME_PATTERN = Pattern.compile("versionName='([0-9]+(\\.[0-9]+)+)'", Pattern.MULTILINE) | |
private static Pattern VERSION_CODE_PATTERN = Pattern.compile("versionCode='([0-9]+)'", Pattern.MULTILINE) | |
public File apkFile; | |
public String versionName; | |
public int versionCode; | |
public VersionInfo(File apkFile, String aaptOutput) { | |
def nameMatcher = VERSION_NAME_PATTERN.matcher(aaptOutput) | |
def codeMatcher = VERSION_CODE_PATTERN.matcher(aaptOutput) | |
nameMatcher.find() | |
codeMatcher.find() | |
this.apkFile = apkFile; | |
this.versionName = nameMatcher.group(1) | |
this.versionCode = Integer.parseInt(codeMatcher.group(1), 10) | |
} | |
@Override | |
String toString() { | |
return "VersionInfo(\"${apkFile}\", versionName=\"${versionName}\", versionCode=\"${versionCode}\")" | |
} | |
} | |
def getAaptPath = { -> | |
def sdkDir = android.sdkDirectory | |
def buildToolsDir = new File(sdkDir, "build-tools") | |
def versions = buildToolsDir.list() | |
def latestBuildToolsDir = new File(buildToolsDir, versions[-1]) | |
return "${new File(latestBuildToolsDir, "aapt").absoluteFile}" | |
} | |
def getVersionInfo = { File apkFile -> | |
def stdout = new ByteArrayOutputStream() | |
exec { | |
commandLine getAaptPath(), "dump", "badging", apkFile.absoluteFile | |
standardOutput = stdout | |
} | |
return new VersionInfo(apkFile, stdout.toString()) | |
} |
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
class VersionInfo { | |
public static Pattern VERSION_NAME_PATTERN = Pattern.compile("versionName='([0-9]+(\\.[0-9]+)+)'", Pattern.MULTILINE) | |
public static Pattern VERSION_CODE_PATTERN = Pattern.compile("versionCode='([0-9]+)'", Pattern.MULTILINE) | |
public File apkFile; | |
public String versionName; | |
public int versionCode; | |
public VersionInfo(File apkFile, String versionName, int versionCode) { | |
this.apkFile = apkFile; | |
this.versionName = versionName | |
this.versionCode = versionCode | |
} | |
@Override | |
String toString() { | |
return "VersionInfo(\"${apkFile}\", versionName=\"${versionName}\", versionCode=\"${versionCode}\")" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment