封装python调用aapt直接获取包名和tagertSdkversion的工具
背景:
在上一篇已经介绍过如何利用bat脚本直接运行py文件并获取结果
https://www.cnblogs.com/reseelei-despair/p/11082060.html
因为本机本身有py环境,所以可以运行,为了让没有py环境的电脑也能使用这个工具,考虑使用pyqt5将工具界面化,直接拖拽进工具后打印相关信息
思路:
两个py文件,aapt_tools负责调用aapt查询安装包信息,qt_tools2负责将工具界面化显示
代码:
qt部分我还没开始学,所以此次参考
对部分代码进行修改
qt_tools2.py
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import re from aapt_tools import ApkInfo class Fennbk_com(QWidget): def __init__(self): super(Fennbk_com, self).__init__() # 窗口标题 self.setWindowTitle('aapt_tools') # 定义窗口大小 self.resize(300, 60) self.QLabl = QLabel(self) self.QLabl.setGeometry(0, 0, 4000, 50) # 调用Drops方法 self.setAcceptDrops(True) # 鼠标拖入事件 def dragEnterEvent(self, evn): self.QLabl.setText('文件路径:\n' + evn.mimeData().text()) c = evn.mimeData().text() d = re.sub("file:///", "", c) apk_info = ApkInfo(r"%s"%d) self.QLabl.setText("Activity:%s \napkName:%s \nsdkVersion:%s \ntargetSdkVersion:%s" % (apk_info.get_apk_activity(),apk_info.get_apk_base_info(),apk_info.get_apk_sdkVersion(),apk_info.get_apk_targetSdkVersion())) # 鼠标放开函数事件 evn.accept() if __name__ == "__main__": app = QApplication(sys.argv) fennbk = Fennbk_com() fennbk.show() sys.exit(app.exec_())
aapt_tools.py
# -*- coding: utf-8 -*- import re import subprocess import os class ApkInfo: def __init__(self, apk_path): self.apkPath = apk_path self.aapt_path = self.get_aapt() @staticmethod def get_aapt(): dir = os.getcwd() dir2 = dir + r"\aapt.exe" print(dir2) return dir2 def get_apk_base_info(self): p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) (output, err) = p.communicate() match = re.compile("package: name='(\S+)'").match(output.decode()) if not match: raise Exception("can't get packageinfo") package_name = match.group(1) return package_name def get_apk_activity(self): p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) (output, err) = p.communicate() match = re.compile("launchable-activity: name='(\S+)'").search(output.decode()) if match is not None: return match.group(1) def get_apk_sdkVersion(self): p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) (output, err) = p.communicate() match = re.compile("sdkVersion:'(\S+)'").search(output.decode()) return match.group(1) def get_apk_targetSdkVersion(self): p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) (output, err) = p.communicate() match = re.compile("targetSdkVersion:'(\S+)'").search(output.decode()) return match.group(1)
完成后将aapt.exe以及上述两个py文件放入同一文件夹,使用pyinstaller进行封装
封装采坑:
1.打开程序提示:it could not find or load the Qt platform plugin "windows"
参考
https://blog.csdn.net/qq_36100960/article/details/79413085
我是将plugins下的platforms文件夹复制到项目文件目录下(要复制整个文件夹),问题解决...
2.打包过程中出现Cannot find existing PyQt5 plugin directories,信息如下
Exception:
Cannot find existing PyQt5 plugin directories
Paths checked: C:\qt64\qt_1544645195969\_h_env\Library\plugins
发现C盘没有这个路径,所以我按照这个路径创建了相关的文件夹,并把pyqt5.dll 和pyqt5qmlplugin.dull丢进去...就这么解决了
使用效果如下图所示
拖入前
拖入后
bug:
1.丢文件的时候会卡顿
2.若安装包是重复下载的安装包,包名带了后缀(1),这类apk拖拽进工具会直接造成程序停止运行