zoukankan      html  css  js  c++  java
  • java通过解析文件获取apk版本等信息

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.Namespace;
    import org.jdom.input.SAXBuilder;
    
    import cn.zsmy.constant.Constant;
    
    public class ApkUtil {
        
        private static final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");
        
        @SuppressWarnings({"unchecked", "rawtypes"})
        public static ApkInfo getApkInfo(String apkPath){
            ApkInfo apkInfo = new ApkInfo();
            SAXBuilder builder = new SAXBuilder();
            Document document = null;
            try{
                document = builder.build(getXmlInputStream(apkPath));
            }catch (Exception e) {
                e.printStackTrace();
            }
            Element root = document.getRootElement();//跟节点-->manifest
            apkInfo.setVersionCode(root.getAttributeValue("versionCode",NS));
            apkInfo.setVersionName(root.getAttributeValue("versionName", NS));
            apkInfo.setApkPackage(root.getAttributeValue("package"));
            Element elemUseSdk = root.getChild("uses-sdk");//子节点-->uses-sdk
            apkInfo.setMinSdkVersion(elemUseSdk.getAttributeValue("minSdkVersion", NS));
            List listPermission = root.getChildren("uses-permission");//子节点是个集合
            List permissions = new ArrayList();
            for(Object object : listPermission){
                String permission = ((Element)object).getAttributeValue("name", NS);
                permissions.add(permission);
            }
            apkInfo.setUses_permission(permissions);
            Constant.MY_LOG.debug("
    版本号:"+apkInfo.getVersionCode()+"
    版本名:"+apkInfo.getVersionName()+"
    包名:"+apkInfo.getApkPackage());
            //String str = "
    版本号:"+versionCode+"
    版本名:"+versionName+"
    包名:"+packageName;
            Constant.MY_LOG.debug(root.getAttributes().toString());
            return apkInfo;
    //        String s = root.getAttributes().toString();
    //        String c[] = s.split(",");
    //        String versionCode = null;
    //        String versionName = null;
    //        String packageName = null;
    //        for(String a: c){
    //            if(a.contains("versionCode")){
    //                versionCode = a.substring(a.indexOf("versionCode="")+13, a.lastIndexOf("""));
    //            }
    //            if(a.contains("versionName")){
    //                versionName = a.substring(a.indexOf("versionName="")+13, a.lastIndexOf("""));
    //            }
    //            if(a.contains("package")){
    //                packageName = a.substring(a.indexOf("package="")+9, a.lastIndexOf("""));
    //            }
    //        }        
    //        
    //        Constant.MY_LOG.debug("
    版本号:"+versionCode+"
    版本名:"+versionName+"
    包名:"+packageName);
    //        String str = "
    版本号:"+versionCode+"
    版本名:"+versionName+"
    包名:"+packageName;
    ////        return root.getAttributes().toString();
    //        return str;
    //        return "ss";
        }
    
        private static InputStream getXmlInputStream(String apkPath) {
            InputStream inputStream = null;
            InputStream xmlInputStream = null;
            ZipFile zipFile = null;
            try {
                zipFile = new ZipFile(apkPath);
                ZipEntry zipEntry = new ZipEntry("AndroidManifest.xml");
                inputStream = zipFile.getInputStream(zipEntry);
                AXMLPrinter xmlPrinter = new AXMLPrinter();
                xmlPrinter.startPrinf(inputStream);
                xmlInputStream = new ByteArrayInputStream(xmlPrinter.getBuf().toString().getBytes("UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    inputStream.close();
                    zipFile.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            return xmlInputStream;
        }
    
    }

    调用:

    @ResponseBody
        @RequestMapping(value = "/getApkVersion", method = RequestMethod.POST)
        public String getApkVersion(VersionForm form, MultipartFile apkFile) throws Exception {
            Constant.MY_LOG.debug("获取上传的apk版本");
            File apkTempFile = new File(DictInit.dictMap.get(Constant.Dict.APK_UPLOAD_PATH) + "temp.apk");
            // File apkTempFile = new File("d:\temp.apk");//测试用
            apkFile.transferTo(apkTempFile);
            // 获得apk信息
            ApkInfo apkInfo = new ApkInfo();
            apkInfo = ApkUtil.getApkInfo(apkTempFile.getPath());
            return apkInfo.getVersionName();
        }    
  • 相关阅读:
    腾讯广告算法大赛2019
    Mysql的部分常用SQL语句
    org.activiti.dependencies 7.1.0.M6 造成版本冲突问题的解决
    windows 将 redis 注册为服务 自动启动或手动启动
    org.springframework.security.access.AccessDeniedException: Access is denied
    对两个List进行关联匹配,选择匹配上的记录形成新的List输出
    越是大型的组织,越需要试验基地,试验基地应有特殊待遇
    dubbo+zookeeper 安装所遇系列问题
    签名与验签——图解
    关于航空母舰战斗群出海训练,常有大量鱼群跟随问题的建议和设想
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6265346.html
Copyright © 2011-2022 走看看