def app_install_local(serial: str, apk_path: str, launch: bool = False) -> str: """ install apk to device Returns: package name Raises: AdbInstallError, FileNotFoundError """ # 解析apk文件 device = adbclient.device_with_serial(serial) try: apk = apkutils.APK(apk_path) except apkutils.apkfile.BadZipFile: raise InstallError("ApkParse", "Bad zip file") # 提前将重名包卸载 package_name = apk.manifest.package_name pkginfo = device.package_info(package_name) if pkginfo: logger.debug("uninstall: %s", package_name) device.uninstall(package_name) # 解锁手机,防止锁屏 # ud = u2.connect_usb(serial) # ud.open_identify() try: # 推送到手机 dst = "/data/local/tmp/tmp-%d.apk" % int(time.time() * 1000) logger.debug("push %s %s", apk_path, dst) device.sync.push(apk_path, dst) logger.debug("install-remote %s", dst) # 调用pm install安装 device.install_remote(dst) except adbutils.AdbInstallError as e: raise InstallError("install", e.output) finally: # 停止uiautomator2服务 logger.debug("uiautomator2 stop") # ud.session().press("home") # ud.service("uiautomator").stop() # 启动应用 if launch: logger.debug("launch %s", package_name) device.app_start(package_name) return package_name
def __init__(self, serial: str, callback=nop_callback): self._serial = serial self._procs = [] self._current_ip = current_ip() self._device = adbclient.device_with_serial(serial) self._callback = callback
def main(): parser = argparse.ArgumentParser() # formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-s", "--serial", help="device serial number") parser.add_argument("-i", "--install", help="install from local apk or url") parser.add_argument("-u", "--uninstall", help="uninstall apk") parser.add_argument("--clear", action="store_true", help="clear all data when uninstall") parser.add_argument("-l", "--list", action="store_true", help="list packages installed") parser.add_argument("--grep", help="filter matched package names") args = parser.parse_args() d = adbclient.device_with_serial(args.serial) if args.install: dst = "/data/local/tmp/tmp-%d.apk" % (int(time.time() * 1000)) print("push to %s" % dst) if re.match(r"^https?://", args.install): resp = requests.get(args.install, stream=True) resp.raise_for_status() length = int(resp.headers.get("Content-Length", 0)) r = ReadProgress(resp.raw, length) else: length = os.stat(args.install).st_size fd = open(args.install, "rb") r = ReadProgress(fd, length) start = time.time() d.sync.push(r, dst) print("Success pushed, time used %d seconds" % (time.time() - start)) new_dst = "/data/local/tmp/tmp-%s.apk" % r._hash[:8] d.shell_output("mv", dst, new_dst) dst = new_dst info = d.sync.stat(dst) print("verify pushed apk, md5: %s, size: %s" % (r._hash, humanize(info.size))) assert info.size == r.copied print("install to android system ...") try: start = time.time() d.install_remote(dst, clean=True) print("Success installed, time used %d seconds" % (time.time() - start)) except AdbInstallError as e: sys.exit( "Failure " + e.reason + "\n" + "Remote apk is not removed. Manually install command:\n\t" + "adb shell pm install -r -t " + dst) elif args.uninstall: d.shell_output("pm", "uninstall", args.uninstall) elif args.list: patten = re.compile(args.grep or ".*") for p in d.list_packages(): if patten.search(p): print(p)