安卓安装包版本解析工具类
package com.hikvision.cms.modules.app.util;
import java.io.File;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.xmlpull.v1.XmlPullParser;
import android.content.res.AXmlResourceParser;
import android.util.TypedValue;
import com.hikvision.cms.modules.app.bo.ApkInfo;
public class AppUtil {
private static final String VERSION_CODE = "versionCode";
private static final String VERSION_NAME = "versionName";
private static final String PACKAGE = "package";
private static final float RADIX_MULTS[] = { 0.00390625F, 3.051758E-005F,
1.192093E-007F, 4.656613E-010F };
private static final String DIMENSION_UNITS[] = { "px", "dip", "sp", "pt",
"in", "mm", "", "" };
private static final String FRACTION_UNITS[] = { "%", "%p", "", "", "", "",
"", "" };
/**
* 解析Apk文件
*
* @param apkFile
* apk文件
* @return apk版本信息文件
*/
public static ApkInfo apk(File apkFile) throws Exception {
ApkInfo apkInfo = new ApkInfo();
ZipFile zipFile;
zipFile = new ZipFile(apkFile);
ZipEntry zipEntry = zipFile.getEntry(("AndroidManifest.xml"));
AXmlResourceParser parser = new AXmlResourceParser();
parser.open(zipFile.getInputStream(zipEntry));
boolean flag = true;
while (flag) {
int type = parser.next();
if (type == XmlPullParser.START_TAG) {
int count = parser.getAttributeCount();
for (int i = 0; i < count; i++) {
String name = parser.getAttributeName(i);
String value = getAttributeValue(parser, i);
if (value.contains("MAIN")) {
flag = false;
break;
} else if (PACKAGE.equals(name)) {
apkInfo.setPackagee(value);
} else if (VERSION_CODE.equals(name)) {
apkInfo.setVersionCode(value);
} else if (VERSION_NAME.equals(name)) {
apkInfo.setVersionName(value);
}
}// end for
}
}// end while
return apkInfo;
}
/**
* 获取属性值
*
* @param parser
* 解析对象
* @param index
* 属性索引
* @return String 属性值
*/
private static String getAttributeValue(AXmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x%08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data != 0 ? "true" : "false";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data))
+ DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data))
+ FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT
&& type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#%08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
return String.format("<0x%X, type 0x%02X>", data, type);
}
private static String getPackage(int id) {
if (id >>> 24 == 1) {
return "android:";
}
return "";
}
private static float complexToFloat(int complex) {
return (float) (complex & 0xFFFFFF00) * RADIX_MULTS[(complex >> 4) & 3];
}
}
版本信息记录类
/**
* <p>
* apk版本信息对象
* </p>
* @version V1.0
*/
public class ApkInfo {
// 内部版本号
private String versionCode;
// 外部版本号
private String versionName;
// 包名
private String packagee;
public ApkInfo() {
}
public String getVersionCode() {
return versionCode;
}
public void setVersionCode(String versionCode) {
this.versionCode = versionCode;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getPackagee() {
return packagee;
}
public void setPackagee(String packagee) {
this.packagee = packagee;
}
}
用到安卓压缩包解析工具包下载链接如下: