由于在测试中经常需要安装apk进行测试,为了提高测试效率,用python写了一个,拖拽电脑上下载的apk文件到窗口,点击安装,实现快捷安装apk到安卓设备。该脚本能实现多台手机同步安装apk的功能。直接附上源码: # -*- coding: utf-8 -*- import os import threading import windnd import tkinter as tk from tkinter import * # 导入 Tkinter 库 class APKInstaller: def install_apk(self, device_name, apk_name): # 安装apk os.system("adb -s " + device_name + " install -r " + os.path.join(apk_name)) def get_device_list(self): os.system("adb devices") res = os.popen("adb devices").readlines() device_list = [sub.split('\t')[0] for sub in res[1:-1]] return device_list # 此类用来制作界面 class APKTk(): def dragged_files(self, files): msg = '\n'.join((item.decode('gbk') for item in files)) print(msg) self.apk_name.set(msg) def __init__(self, APKInstaller): self.root = Tk() self.root.geometry('540x170') self.root.title("Install_APK/APKS") self.apk_name = StringVar() self.apkInstaller = APKInstaller self.select_device_list = [] self.device_list = self.apkInstaller.get_device_list() self.cb_list = [] windnd.hook_dropfiles(self.root, self.dragged_files) def mul_check_box(self): for index, item in enumerate(self.device_list): self.select_device_list.append(tk.StringVar()) cb = Checkbutton(self.root, text=item, variable=self.select_device_list[-1], onvalue=item, offvalue='') self.cb_list.append(cb) cb.grid(row=index, column=0, sticky='w') cb.select() # 刷新设备列表 def refresh_data(self): print("refresh") for item in self.cb_list: item.grid_remove() self.cb_list = [] self.device_list = self.apkInstaller.get_device_list() self.select_device_list = [] self.mul_check_box() print("\n") # 全选设备列表 def select_all(self): for index, item in enumerate(self.device_list): self.device_list[index].set(item) # 安装按钮 def install_button(self): button_install = Button(self.root, text="安装", command=self.install) button_install.grid(row=len(self.device_list) + 1, column=1) # 刷新设备列表按钮 def refresh_button(self): button_install = Button(self.root, text="刷新", command=self.refresh_data) button_install.grid(row=len(self.device_list) + 1, column=2) def input_text(self): entry_log = Entry(self.root, width=60, textvariable=self.apk_name) entry_log.grid(row=len(self.device_list) + 1, column=0, sticky='w') def get_apk_name(self): return self.apk_name.get() def mainloop(self): self.root.mainloop() def install(self): selected_device_list = [i.get() for i in self.select_device_list if i.get()] print(selected_device_list) apkName = self.get_apk_name() print(apkName) for device in selected_device_list: threading.Thread(target=self.apkInstaller.install_apk, args=(device, apkName)).start() print("启动" + device) if __name__ == '__main__': apkInstaller = APKInstaller() apkTk = APKTk(apkInstaller) apkTk.mul_check_box() apkTk.input_text() apkTk.install_button() apkTk.refresh_button() apkTk.root.mainloop()
adb安装命令脚本
于 2023-05-18 10:55:03 首次发布