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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM