参考文档:http://www.cnblogs.com/ITtangtang/p/3881288.html
获取Android版本信息,就需要解析AndroidManifest.xml文件。代码如下:
public class ApkUtil {
public static final String VERSION_CODE = "versionCode";
public static final String VERSION_NAME = "versionName";
public static final String SDK_VERSION = "sdkVersion";
public static final String TARGET_SDK_VERSION = "targetSdkVersion";
public static final String USES_PERMISSION = "uses-permission";
public static final String APPLICATION_LABEL = "application-label";
public static final String APPLICATION_ICON = "application-icon";
public static final String USES_FEATURE = "uses-feature";
public static final String USES_IMPLIED_FEATURE = "uses-implied-feature";
public static final String SUPPORTS_SCREENS = "supports-screens";
public static final String SUPPORTS_ANY_DENSITY = "supports-any-density";
public static final String DENSITIES = "densities";
public static final String PACKAGE = "package";
public static final String APPLICATION = "application:";
public static final String LAUNCHABLE_ACTIVITY = "launchable-activity";
private ProcessBuilder mBuilder;
private static final String SPLIT_REGEX = "(: )|(=')|(' )|'";
//private static final String FEATURE_SPLIT_REGEX = "(:')|(',')|'";
public ApkUtil() {
mBuilder = new ProcessBuilder();
mBuilder.redirectErrorStream(true);
}
/**
* 返回一个apk程序的信息。
*
* @param mAaptPath aapt所在的目录
* @param apkPath apk的路径。
* @return apkInfo 一个Apk的信息。
*/
public VersionInfo getApkInfo(String mAaptPath, String apkPath) throws Exception {
Process process = mBuilder.command(mAaptPath, "d", "badging", apkPath).start();
InputStream is = null;
is = process.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is, "utf8"));
String tmp = br.readLine();
try {
if (tmp == null || !tmp.startsWith("package")) {
throw new Exception("参数不正确,无法正常解析APK包。输出结果为:\n" + tmp + "...");
}
VersionInfo info = new VersionInfo();
do {
setAppProperty(info, tmp);
} while ((tmp = br.readLine()) != null);
return info;
} catch (Exception e) {
throw e;
} finally {
process.destroy();
closeIO(is);
closeIO(br);
}
}
/**
* 设置APK的属性信息。
*
* @param info
* @param source
*/
private void setAppProperty(VersionInfo info, String source) {
if (source.startsWith(PACKAGE)) {
splitPackageInfo(info, source);
}
if (source.startsWith(APPLICATION_LABEL)) {
info.setAppName(getPropertyInQuote(source));
}
}
/**
* 返回出格式为name: 'value'中的value内容。
*
* @param source
* @return
*/
private String getPropertyInQuote(String source) {
int index = source.indexOf("'") + 1;
return source.substring(index, source.indexOf('\'', index));
}
/**
* 获取出包名、版本等信息。
*
* @param info
* @param packageSource
*/
private void splitPackageInfo(VersionInfo info, String packageSource) {
String[] packageInfo = packageSource.split(SPLIT_REGEX);
info.setVersionCode(Integer.parseInt(packageInfo[4]));
info.setVersionName(packageInfo[6]);
}
/**
* 释放资源。
*
* @param c
* 将关闭的资源
*/
private final void closeIO(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在Spring框架下,需要将aapt.exe文件拷贝到Deployed Resources/webapp路径下,如果实际放置路径为Deployed Resources/webapp/lib/aapt.exe,调用代码如:
VersionInfo versionInfo = new ApkUtil().getApkInfo(request.getSession().getServletContext()
.getRealPath("/lib/aapt"), apkFilePath);
VersionInfo定义如下:
public class VersionInfo{
/** 版本号 */
private int versionCode;
/** 版本名称 */
private String versionName;
/** 应用程序名*/
private String appName;
public int getVersionCode(){
return versionCode;
}
public void setVersionCode(int versionCode){
this.versionCode = versionCode;
}
public String getVersionName(){
return versionName;
}
public void setVersionName(String versionName){
this.versionName = versionName;
}
public String getAppName(){
return appName;
}
public void setAppName(String appName){
this.appName = appName;
}
}