net.erdfelt.android.apk.AndroidApk Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apk-parser Show documentation
Show all versions of apk-parser Show documentation
Parser for android app *.apk files
package net.erdfelt.android.apk;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import net.erdfelt.android.apk.io.IO;
import net.erdfelt.android.apk.xml.Attribute;
import net.erdfelt.android.apk.xml.BinaryXmlListener;
import net.erdfelt.android.apk.xml.BinaryXmlParser;
public class AndroidApk {
private String appVersion;
private String appVersionCode;
private String packageName;
private String minSdkVersion;
private String targetSdkVersion;
private String maxSdkVersion;
private class ManifestListener implements BinaryXmlListener {
public void onXmlEntry(String path, String name, Attribute... attrs) {
if ("//".equals(path) && "manifest".equals(name)) {
for (Attribute attrib : attrs) {
if ("package".equals(attrib.getName())) {
packageName = attrib.getValue();
} else if ("versionName".equals(attrib.getName())) {
appVersion = attrib.getValue();
} else if ("versionCode".equals(attrib.getName())) {
appVersionCode = attrib.getValue();
}
}
}
if ("uses-sdk".equals(name)) {
for (Attribute attrib : attrs) {
if ("minSdkVersion".equals(attrib.getName())) {
minSdkVersion = attrib.getValue();
} else if ("targetSdkVersion".equals(attrib.getName())) {
targetSdkVersion = attrib.getValue();
} else if ("maxSdkVersion".equals(attrib.getName())) {
maxSdkVersion = attrib.getValue();
}
}
}
}
}
public AndroidApk(File apkfile) throws ZipException, IOException {
ZipFile zip = new ZipFile(apkfile);
ZipEntry manifestEntry = zip.getEntry("AndroidManifest.xml");
InputStream in = null;
try {
in = zip.getInputStream(manifestEntry);
BinaryXmlParser parser = new BinaryXmlParser();
// parser.addListener(new BinaryXmlDump());
parser.addListener(new ManifestListener());
parser.parse(in);
} finally {
IO.close(in);
try {
if(zip != null) {
zip.close();
}
} catch(IOException ignore) {
/* ignore */
}
}
}
public String getAppVersion() {
return appVersion;
}
public String getAppVersionCode() {
return appVersionCode;
}
public String getPackageName() {
return packageName;
}
public String getMinSdkVersion() {
return minSdkVersion;
}
public String getTargetSdkVersion() {
return targetSdkVersion;
}
public String getMaxSdkVersion() {
return maxSdkVersion;
}
}