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);
复制代码

 

posted @   荣超  阅读(1048)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2019-07-18 自定义控件 监控宿主activity的生命周期
2018-07-18 RecyclerView notifyDataSetChanged无效问题
点击右上角即可分享
微信分享提示