zoukankan      html  css  js  c++  java
  • java 获取apk包的版本号、包路径。权限信息

     需要引入AXMLPrinter

    <dependency>
        <groupId>com.android</groupId>
        <artifactId>AXMLPrinter</artifactId>
        <version>1.0.0</version>
    </dependency>

    但是一直爆红下载不下来,需要到该地址http://dev.91xmy.com/nexus/content/repositories/releases/com/android/AXMLPrinter/1.0.0/去下载

     

    这几个文件点击下载下来 然后放入到你的本地仓库根目录/com/android/AXMLPrinter/1.0.0/目录下

    就可以使用了

     ApkInfo 是自己定义的类。来存放客户端的基本信息

            ApkInfo apkInfo = new ApkInfo();
            File file = new File(apkPath);
            InputStream inputStream = null;
    
            long fileLength = file.length();
            apkInfo.setName(file.getName());
            apkInfo.setSize(fileLength);
    
            List<String> permission = new ArrayList<>();
            AXmlResourceParser parser = new AXmlResourceParser();
            try {
                ZipFile zipFile = new ZipFile(file);
                ZipEntry zipEntry = new ZipEntry("AndroidManifest.xml");
                inputStream = zipFile.getInputStream(zipEntry);
                parser.open(inputStream);
                while (true) {
                    int type = parser.next();
                    if (type == XmlPullParser.END_DOCUMENT) break;
    
                    if (type == XmlPullParser.START_TAG) {
                        if ("manifest".equals(parser.getName())) {
                            int attributeCount = parser.getAttributeCount();
                            for (int i = 0; i < attributeCount; i++) {
                                switch (parser.getAttributeName(i)) {
                                    case "versionCode":
                                        apkInfo.setVersionCode(parser.getAttributeValueData(i) + "");
                                    case "versionName":
                                        apkInfo.setVersionName(parser.getAttributeValue(i));
                                    case "package":
                                        apkInfo.setApkPackage(parser.getAttributeValue(i));
                                }
                            }
                        }
    
                        if ("uses-sdk".equals(parser.getName())) {
                            int attributeCount = parser.getAttributeCount();
                            for (int i = 0; i < attributeCount; i++) {
                                if ("minSdkVersion".equals(parser.getAttributeName(i))) {
                                    apkInfo.setMinSdkVersion(parser.getAttributeValueData(i) + "");
                                }
                            }
                        }
    
                        if ("uses-permission".equals(parser.getName())) {
                            int attributeCount = parser.getAttributeCount();
                            for (int i = 0; i < attributeCount; i++) {
                                if ("name".equals(parser.getAttributeName(i))) {
                                    permission.add(parser.getAttributeValue(i));
                                }
                            }
                        }
                    }
                }
            } catch (XmlPullParserException | IOException e) {
    
            } finally {
                try {
                    parser.close();
                    inputStream.close();
                } catch (IOException e) {
    
                }
            }
            apkInfo.setUses_permission(permission);
  • 相关阅读:
    php静态调用非静态方法
    phalcon 框架3.0更新时报错
    centos7.5更换docker-ce镜像源
    腾讯云更换镜像源遇到的坑
    php cli模式下调试
    审查php.ini自动分析程序
    docker WARNING: IPv4 forwarding is disabled. Networking will not work.
    git常用命令,制作缩写命令
    学习GRPC(一) 简单实现
    mac与linux服务器之间使用ssh互通有无
  • 原文地址:https://www.cnblogs.com/rchao/p/13336081.html
Copyright © 2011-2022 走看看