java 解析 manifest_Java解析 APK AndroidManifest.xml

参考文档: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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值