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);
原文地址:https://www.cnblogs.com/rchao/p/13336081.html