java解析apk安装包信息
2015-07-16 09:11
519 查看
网上找了些关于解析apk安装包的博客:
但是是在非 WEB 项目下运行,但我们公司项目需求是web项目下解析apk安装包信息,然后供其他系统调用时候参考apk是不是需要升级,所有不能够用网络上的代码,和jar包来解析,通过自己改装,基本能满足需求,下面是代码:
特别注意:这里需要一个appt.exe的可执行文件需要放在C:/ 盘下,也可以自己指定目录位置。
ApkInfo.java
APKutil.java
ImpliedFeature
下面是测试类:APKtest.java
附言:
这个代码是网站上找的,不是原创,但是是自己改装的,所以大家别喷,能用上就自己做做测试吧,不正之处请矫正,谢谢,本人邮箱 :326082040@qq.com 要源码可以联系我。
但是是在非 WEB 项目下运行,但我们公司项目需求是web项目下解析apk安装包信息,然后供其他系统调用时候参考apk是不是需要升级,所有不能够用网络上的代码,和jar包来解析,通过自己改装,基本能满足需求,下面是代码:
特别注意:这里需要一个appt.exe的可执行文件需要放在C:/ 盘下,也可以自己指定目录位置。
ApkInfo.java
package cn.testApk; /* * @(#)ApkInfo.java version: 1.0 * Date:2012-1-10 * * Copyright (c) 2011 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. */ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <B>ApkInfo</B> * <p> * 该类封装了一个Apk的信息。包括版本号,支持平台,图标,名称,权限,所需设备特性等。 * </p> * * @author CFuture.Geek_Soledad(66704238@51uc.com) */ public class ApkInfo { public static final String APPLICATION_ICON_120 = "application-icon-120"; public static final String APPLICATION_ICON_160 = "application-icon-160"; public static final String APPLICATION_ICON_240 = "application-icon-240"; public static final String APPLICATION_ICON_320 = "application-icon-320"; /** * apk内部版本号 */ private String versionCode = null; /** * apk外部版本号 */ private String versionName = null; /** * apk的包名 */ private String packageName = null; /** * 支持的android平台最低版本号 */ private String minSdkVersion = null; /** * apk所需要的权限 */ private List<String> usesPermissions = null; /** * 支持的SDK版本。 */ private String sdkVersion; /** * 建议的SDK版本 */ private String targetSdkVersion; /** * 应用程序名 */ private String applicationLable; /** * 各个分辨率下的图标的路径。 */ private Map<String, String> applicationIcons; /** * 程序的图标。 */ private String applicationIcon; /** * 暗指的特性。 */ private List<ImpliedFeature> impliedFeatures; /** * 所需设备特性。 */ private List<String> features; public ApkInfo() { this.usesPermissions = new ArrayList<String>(); this.applicationIcons = new HashMap<String, String>(); this.impliedFeatures = new ArrayList<ImpliedFeature>(); this.features = new ArrayList<String>(); } /** * 返回版本代码。 * * @return 版本代码。 */ public String getVersionCode() { return versionCode; } /** * @param versionCode * the versionCode to set */ public void setVersionCode(String versionCode) { this.versionCode = versionCode; } /** * 返回版本名称。 * * @return 版本名称。 */ public String getVersionName() { return versionName; } /** * @param versionName * the versionName to set */ public void setVersionName(String versionName) { this.versionName = versionName; } /** * 返回支持的最小sdk平台版本。 * * @return the minSdkVersion */ public String getMinSdkVersion() { return minSdkVersion; } /** * @param minSdkVersion * the minSdkVersion to set */ public void setMinSdkVersion(String minSdkVersion) { this.minSdkVersion = minSdkVersion; } /** * 返回包名。 * * @return 返回的包名。 */ public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } /** * 返回sdk平台版本。 * * @return */ public String getSdkVersion() { return sdkVersion; } public void setSdkVersion(String sdkVersion) { this.sdkVersion = sdkVersion; } /** * 返回所建议的SDK版本。 * * @return */ public String getTargetSdkVersion() { return targetSdkVersion; } public void setTargetSdkVersion(String targetSdkVersion) { this.targetSdkVersion = targetSdkVersion; } /** * 返回所需的用户权限。 * * @return */ public List<String> getUsesPermissions() { return usesPermissions; } public void setUsesPermissions(List<String> usesPermission) { this.usesPermissions = usesPermission; } public void addToUsesPermissions(String usesPermission) { this.usesPermissions.add(usesPermission); } /** * 返回程序的名称标签。 * * @return */ public String getApplicationLable() { return applicationLable; } public void setApplicationLable(String applicationLable) { this.applicationLable = applicationLable; } /** * 返回应用程序的图标。 * * @return */ public String getApplicationIcon() { return applicationIcon; } public void setApplicationIcon(String applicationIcon) { this.applicationIcon = applicationIcon; } /** * 返回应用程序各个分辨率下的图标。 * * @return */ public Map<String, String> getApplicationIcons() { return applicationIcons; } public void setApplicationIcons(Map<String, String> applicationIcons) { this.applicationIcons = applicationIcons; } public void addToApplicationIcons(String key, String value) { this.applicationIcons.put(key, value); } public void addToImpliedFeatures(ImpliedFeature impliedFeature) { this.impliedFeatures.add(impliedFeature); } /** * 返回应用程序所需的暗指的特性。 * * @return */ public List<ImpliedFeature> getImpliedFeatures() { return impliedFeatures; } public void setImpliedFeatures(List<ImpliedFeature> impliedFeatures) { this.impliedFeatures = impliedFeatures; } /** * 返回应用程序所需的特性。 * * @return */ public List<String> getFeatures() { return features; } public void setFeatures(List<String> features) { this.features = features; } public void addToFeatures(String feature) { this.features.add(feature); } @Override public String toString() { return "ApkInfo [versionCode=" + versionCode + ",\n versionName=" + versionName + ",\n packageName=" + packageName + ",\n minSdkVersion=" + minSdkVersion + ",\n usesPermissions=" + usesPermissions + ",\n sdkVersion=" + sdkVersion + ",\n targetSdkVersion=" + targetSdkVersion + ",\n applicationLable=" + applicationLable + ",\n applicationIcons=" + applicationIcons + ",\n applicationIcon=" + applicationIcon + ",\n impliedFeatures=" + impliedFeatures + ",\n features=" + features + "]"; } }
APKutil.java
package cn.testApk; /* * @(#)ApkUtil.java version: 0.2.1 * Date:2012-1-9 * * Copyright (c) 2011 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. */ import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * apk工具类。封装了获取Apk信息的方法。 * * @author CFuture.Geek_Soledad(66704238@51uc.com) * * <p> * <b>version description</b><br /> * V0.2.1 修改程序名字为从路径中获得。 * </p> */ 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"; pu 4000 blic 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:"; private ProcessBuilder mBuilder; private static final String SPLIT_REGEX = "(: )|(=')|(' )|'"; private static final String FEATURE_SPLIT_REGEX = "(:')|(',')|'"; /** * aapt所在的目录。 */ private String mAaptPath = "c:/aapt"; //<p><span style="font-size:14px;"><span style="font-size:32px;"><span style="color:#FF0000;">// 特别注意:</span><u>这里需要一个appt.exe的可执行文件需要放在C:/</u></span></span></p><p><span style="font-size:14px;"><span style="font-size:32px;"><u>// 盘下,也可以自己指定目录位置。</u></span></span></p> public ApkUtil() { mBuilder = new ProcessBuilder(); mBuilder.redirectErrorStream(true); } /** * 返回一个apk程序的信息。 * * @param apkPath * apk的路径。 * @return apkInfo 一个Apk的信息。 */ public ApkInfo getApkInfo(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包。输出结果为:" + tmp + "..."); } ApkInfo apkInfo = new ApkInfo(); do { setApkInfoProperty(apkInfo, tmp); } while ((tmp = br.readLine()) != null); return apkInfo; } catch (Exception e) { throw e; } finally { process.destroy(); closeIO(is); closeIO(br); } } /** * 设置APK的属性信息。 * * @param apkInfo * @param source */ private void setApkInfoProperty(ApkInfo apkInfo, String source) { if (source.startsWith(PACKAGE)) { splitPackageInfo(apkInfo, source); } else if (source.startsWith(SDK_VERSION)) { apkInfo.setSdkVersion(getPropertyInQuote(source)); } else if (source.startsWith(TARGET_SDK_VERSION)) { apkInfo.setTargetSdkVersion(getPropertyInQuote(source)); } else if (source.startsWith(USES_PERMISSION)) { apkInfo.addToUsesPermissions(getPropertyInQuote(source)); } else if (source.startsWith(APPLICATION_LABEL)) { apkInfo.setApplicationLable(getPropertyInQuote(source)); } else if (source.startsWith(APPLICATION_ICON)) { apkInfo.addToApplicationIcons(getKeyBeforeColon(source), getPropertyInQuote(source)); } else if (source.startsWith(APPLICATION)) { String[] rs = source.split("( icon=')|'"); apkInfo.setApplicationIcon(rs[rs.length - 1]); } else if (source.startsWith(USES_FEATURE)) { apkInfo.addToFeatures(getPropertyInQuote(source)); } else if (source.startsWith(USES_IMPLIED_FEATURE)) { apkInfo.addToImpliedFeatures(getFeature(source)); } else { // System.out.println(source); } } private ImpliedFeature getFeature(String source) { String[] result = source.split(FEATURE_SPLIT_REGEX); ImpliedFeature impliedFeature = new ImpliedFeature(result[1], result[2]); return impliedFeature; } /** * 返回出格式为name: 'value'中的value内容。 * * @param source * @return */ private String getPropertyInQuote(String source) { return source.substring(source.indexOf("'") + 1, source.length() - 1); } /** * 返回冒号前的属性名称 * * @param source * @return */ private String getKeyBeforeColon(String source) { return source.substring(0, source.indexOf(':')); } /** * 分离出包名、版本等信息。 * * @param apkInfo * @param packageSource */ private void splitPackageInfo(ApkInfo apkInfo, String packageSource) { String[] packageInfo = packageSource.split(SPLIT_REGEX); apkInfo.setPackageName(packageInfo[2]); apkInfo.setVersionCode(packageInfo[4]); apkInfo.setVersionName(packageInfo[6]); } /** * 释放资源。 * * @param c * 将关闭的资源 */ private final void closeIO(Closeable c) { if (c != null) { try { c.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { String demo = "E:/androidApk/2012/05/百宝工具箱/1.0/signed/Toolbox-360.apk"; if (args.length > 0) { demo = args[0]; } ApkInfo apkInfo = new ApkUtil().getApkInfo(demo); System.out.println(apkInfo); } catch (Exception e) { e.printStackTrace(); } } public String getmAaptPath() { return mAaptPath; } public void setmAaptPath(String mAaptPath) { this.mAaptPath = mAaptPath; } }
ImpliedFeature
/* * @(#)Feature.java Project:androidUtil * Date:2012-11-7 * * Copyright (c) 2011 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.testApk; /** * @author Geek_Soledad (66704238@51uc.com) */ public class ImpliedFeature { /** * 要的设备特性名称。 */ private String feature; /** * 表明所需特性的内容。 */ private String implied; public ImpliedFeature() { super(); } public ImpliedFeature(String feature, String implied) { super(); this.feature = feature; this.implied = implied; } public String getFeature() { return feature; } public void setFeature(String feature) { this.feature = feature; } public String getImplied() { return implied; } public void setImplied(String implied) { this.implied = implied; } @Override public String toString() { return "Feature [feature=" + feature + ", implied=" + implied + "]"; } }
下面是测试类:APKtest.java
package cn.testApk; import cn.testApk.ApkInfo; public class APKtest { public static void main(String[] args) throws Exception { String apkPath = "D:\\upload\\apk\\2015-06-09\\WeChart.apk"; ApkUtil apkutil = new ApkUtil(); ApkInfo apkInfo2 = apkutil.getApkInfo(apkPath); String versionCode = apkInfo2.getVersionCode(); // 版本号 String versionName = apkInfo2.getVersionName(); //版本名 String packageName = apkInfo2.getPackageName(); //包名 System.out.println("版本号:"+versionCode); System.out.println("版本名:"+versionName); System.out.println("包名:"+packageName); } }
附言:
这个代码是网站上找的,不是原创,但是是自己改装的,所以大家别喷,能用上就自己做做测试吧,不正之处请矫正,谢谢,本人邮箱 :326082040@qq.com 要源码可以联系我。
相关文章推荐
- eclipse 快捷键介绍
- 【java工具类】MD5加密
- eclipse 配置更快的Eclipse
- eclipse 背景颜色修改
- 【java工具类】DES加密
- eclipse 设置编辑区、控制台、xml等文本文件字体大小
- eclipse 工程配置(.classpath和.project)文件的作用
- eclipse Code Templates的深入研究
- java web中的中文字符乱码问题的解决
- ConcurrentHashMap原理分析
- java中的日期时间处理
- 新秀翻译(一个)——Java在继承和组合
- Java中ListIterator和Iterator详解与辨析
- Java学习篇-------对于main函数参数的理解
- Java错误提示-------数组下标越界
- servlet--多线程问题及java内存模型
- 在dos环境运行java程序
- 配置java的环境变量
- Java SE 学习笔记(一)
- Eclipse经验总结