VUE/JS解析(读取)APK的详情

本文介绍了如何使用app-info-parser插件从APK文件中读取包名,并展示了相关代码实现。在Vue环境中,通过选择APK文件,点击读取按钮,插件会解析出包名,然后将信息添加到表格中。同时,提供了错误处理和输入验证,确保用户选择正确的APK文件。此外,还提到了另一种插件nodejs_apkTool作为替代方案。
摘要由CSDN通过智能技术生成

见上图,两种方式获取包名,1.选择apk,点击读取实现;2.直接输入包名(前提是你得知道APK得包名),点击添加。

我们来实现通过1方法选择APK方式实现

事前准备下载:app-info-parser-master这个插件,这个插件可以支持安卓的应用APK,也可以支持ios的应用ipa

这个是直接选择APK的路径,我是是通过选择自动获取 const parser = new AppInfoParser(files[0]);

直接上代码,组件html

 <p style="margin-left: 230px;" id="BappInstallCSX">
                            <p style="margin-left: 230px;display:none;" id="BPappInstall1">
                                白名单应用:<input type="file" style="width: 200px; height: 30px;" accept=".apk" id="BfileappInstallC"  οnchange="BappSelect()"/>
                                           <input type="button" value="读取" style="margin-top: -10px;" onclick="BappSelect()"/>
                            </p>

                            <p style="margin-left: 230px;display:none;" id="BPappInstall2">
                                应用包名:<input type="text" style="width: 150px; height: 15px;" id="BinputappInstallC"/>
                                         <input type="button" value="添加" style="margin-top: -10px;" onclick="addBPappInstall()"/>
                                         <input type="button" value="删除" style="margin-top: -10px;" onclick="deleBPappInstall()"/>
                            </p>

                              <table style="margin-left: 230px;align-self: auto; text-align: center;display:none;"class="appInstallCtable" id="BappInstallCtableid">
                                <thead style="background-color:#acabac;border-style:solid;border-width:1px;" class="appInstallCthead">
                                  <tr class="appInstallCheader">
                                     <th> </th>
                                     <th>apk文件名 </th>
                                     <th>包名 </th>
                                     <th>应用名 </th>
                                  </tr>
                                </thead>
                                <tbody id="BappInstallCtbody" class="appInstallCtbody" style="background-color: #FCFDFD;color: #414C5E;border-top: 1px solid #D6DBDF;border-bottom: 1px solid #D6DBDF;">
                                  <!-- js中动态拼接-->
                                </tbody>
                              </table>
                        </p>

VUE的实现:

data() {
  return {

      newpagesBAPPINSTALL: {},
      pagesBAPPINSTALLs: [],
      pagesBAPPINSTALLlist:[],//获取发送给后端的配置      

  }
}

methods: {


 BappSelect1(){//选择文件按钮,判断是否是apk
              var objFile = document.getElementById('BfileappInstallC').files[0];//获取文件内容对象
              var objStr = /\.(apk)$/; //文件类型正则验证
              if(!objStr.test(objFile.name)){
                this.$alert('请选择正确的APK!', '警告', {
                        confirmButtonText: '确定',
                        // callback: action => {
                        //     this.$message({
                        //         type: 'info',
                        //         message: `action: ${ action }`
                        //     });
                        // }
                  });
                return false;
              } 
       },
       BappSelect(){ //点击添加按钮
              let AppInfoParser = require('../../../../build/app-info-parser-master/dist/app-info-parser.js');
              const files = document.getElementById('BfileappInstallC').files;
              console.log("aaa"+JSON.stringify(files))
              const parser = new AppInfoParser(files[0]);
              console.log("bbb"+ JSON.stringify(parser))
              parser.parse().then(result => {
                console.log('app info ----> ', result);
                console.log('包名:', result.package);
                console.log('BfileappInstallC buffer ----> ', parser.file);
                console.log("APK名称"+parser.file.name);

                let APKname = parser.file.name; //APK文件名
                let PACKname = result.package;//包名
                let APPname = " ";//应用名
                let APKList = {"newpagesBAPPINSTALL":APKname,"inputUSERBAPPINSTALLC":PACKname,"apkUSERBAPPINSTALLC":APPname}
                this.pagesBAPPINSTALLs.push(APKList);//添加显示出来之用
                this.pagesBAPPINSTALLlist.push(PACKname);//收集数据发送给后端
                
                // BPappInstallpp.push(APKname);
                // BPappInstallpp.push(PACKname);
                // BPappInstallpp.push(APPname);

              }).catch(err => {
                this.$alert('无法获取apk信息,请检查!', '警告', {
                        confirmButtonText: '确定',
                        // callback: action => {
                        //     this.$message({
                        //         type: 'info',
                        //         message: `action: ${ action }`
                        //     });
                        // }
                  });
                console.log('无法获取apk信息 ----> ', err)
              })
       },
       addBPappInstall(){
           if (this.newpagesBAPPINSTALL.inputUSERBAPPINSTALLC == "" || this.newpagesBAPPINSTALL.inputUSERBAPPINSTALLC == null){
               this.$alert('包名不能为空!', '警告', {
                        confirmButtonText: '确定',
                        // callback: action => {
                        //     this.$message({
                        //         type: 'info',
                        //         message: `action: ${ action }`
                        //     });
                        // }
                  });
           }else{
               this.pagesBAPPINSTALLs.push(this.newpagesBAPPINSTALL);//把获取的APK放到表格中展示
               this.pagesBAPPINSTALLlist.push(this.newpagesBAPPINSTALL.inputUSERBAPPINSTALLC);//获取的参数要转给后台
               this.newpagesBAPPINSTALL = {inputUSERBAPPINSTALLC:""};//置空,否则重复添加按钮内容重复
           }
       },
}

扩展:

我们选择APK文件的时候可以先判断一下。JQ/JS写法:

 $(document).ready(function(){//监听选择文件--判断选的是不是APK
    $("#BfileappInstallC").change(function(evt){
      var objFile = document.getElementById('BfileappInstallC').files[0];//获取文件内容对象
      var objStr = /\.(apk)$/; //文件类型正则验证
      if(!objStr.test(objFile.name)){
        alert("请选择正确的APK");
        return false;
      }
    });
    $("#HfileappInstallC").change(function(evt){
      var objFile = document.getElementById('HfileappInstallC').files[0];//获取文件内容对象
      console.log("aaa"+ objFile);
      var objStr = /\.(apk)$/; //文件类型正则验证
      if(!objStr.test(objFile.name)){
        alert("请选择正确的APK");
        return false;
      }

    });
   });

选择文件后,直接点击读取按钮,然后就会获取APK信息,我这里只取APK包名。

 function BappSelect () {//读取APK包名的方法
        const files = document.getElementById('BfileappInstallC').files;
        console.log("aaa"+JSON.stringify(files) )
        const parser = new AppInfoParser(files[0]);
        console.log("bbb"+ JSON.stringify(parser))
        parser.parse().then(result => {
          console.log('app info ----> ', result);
          console.log('包名:', result.package);
          console.log('BfileappInstallC buffer ----> ', parser.file);
          console.log("APK名称"+parser.file.name);
          // document.getElementById('BinputappInstallC').value = result.package;

          var APKname = parser.file.name; //APK文件名
          var PACKname = result.package;//包名
          var APPname = " ";//应用名
          var str = "";
          var checkbox='<input type="checkbox" name="appInstallCcheckId" class="appInstallCcheckbox" id="appInstallCcekall" >';
          str = "<tr><td>" + checkbox +"</td><td>"+APKname+"</td><td>"+PACKname+"</td><td>"+APPname+"</td></tr>";
          BPappInstallpp.push(APKname);
          BPappInstallpp.push(PACKname);
          BPappInstallpp.push(APPname);
          if(APKname == "" || APKname == null){
             alert("包名不能为空!!!");
          }else{
            $(".appInstallCtbody").append(str);
          }

        }).catch(err => {
          alert("无法获取apk信息,请检查!");
          console.log('无法获取apk信息 ----> ', err)
        })
  }

//添加按钮方法
function addBPappInstall(){ //添加APK包名到输入框

     var APKname = ""; //APK文件名
     var PACKname = document.getElementById("BinputappInstallC").value;//包名
     var APPname = "";//应用名
     var str = "";
     var checkbox='<input type="checkbox" name="appInstallCcheckId" class="appInstallCcheckbox" id="appInstallCcekall" >';
     str = "<tr><td>" + checkbox +"</td><td>"+APKname+"</td><td>"+PACKname+"</td><td>"+APPname+"</td></tr>";
      BPappInstallpp.push(APKname);
      BPappInstallpp.push(PACKname);
      BPappInstallpp.push(APPname);

     if(PACKname == "" || PACKname == null || PACKname == " "){
       alert("白名单不能为空!!!");
     }else{
       $(".appInstallCtbody").append(str);
     }
     $("#BinputappInstallC").val("");
  }

简化下上面的代码:

就是导入APK,直接就读取了

appSelect1(){//选择文件按钮,判断是否是apk
              var objFile = document.getElementById('appPush').files[0];//获取文件内容对象
              var objStr = /\.(apk)$/; //文件类型正则验证
              if(!objStr.test(objFile.name)){
                this.$alert('请选择正确的APK!', '警告', {
                        confirmButtonText: '确定',
                  });
                return false;
              }
              this.appSelect();//直接就调用实现apk读取
        },
appSelect(){
                let AppInfoParser = require('../../build/app-info-parser-master/dist/app-info-parser.min');
                const files = document.getElementById('appPush').files;
                console.log("aaa"+JSON.stringify(files))
                const parser = new AppInfoParser(files[0]);
                console.log("bbb"+ JSON.stringify(parser))

                parser.parse().then(result => {
                    console.log('app info ----> ', result);
                    console.log('包名:', result.package);
                    console.log("图片路径:"+result.icon);
                    console.log("版本号:"+result.versionName);
                    console.log("应用名称:"+result.application.label);
                    console.log('appPush buffer ----> ', parser.file);//等同于files[0]
                    console.log("APK名称:"+parser.file.name);
                    
                    //这里需要什么就要什么,看看上面打印的


                }).catch(err => {
                    this.$alert('无法获取apk信息,请检查!', '警告', {
                            confirmButtonText: '确定',
                    });
                    console.log('无法获取apk信息 ----> ', err)
                })
        },

注意:原生的选择文件按钮和element使用是有细微差别的,详情请看:文件选择按钮原生和element框架获取的区别_测试狂人的博客-CSDN博客

读取到的信息打印:

或者通过另个插件下载:https://github.com/chen2009277025/nodejs_apkTool

参考:nodejs解析apk _51CTO博客_nodejs

来实现apk的相关操作

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试狂人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值