基于Python实现批量安装android apk包
by:授客 QQ:1033553122
1.相关软件包及文件下载
2.测试环境
Win7 64位
Python 3.3.2
3.用法及代码show
用法:
把.apk包放入apk目录下,然后进入到install_apk_once_many.py所在目录,运行程序:
python install_apk_once_many.py
运行结果:
代码展示
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import subprocess
import os
workdir = os.getcwd()
apk_path = os.path.join(workdir, 'apk')
adb_apth = os.path.join(workdir, 'adb.exe')
p = subprocess.Popen(adb_apth + ' root', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
try:
p.communicate()
if p.returncode:
print('运行出错,程序已退出')
exit()
except Exception as e:
p.kill()
filename_list = os.listdir(apk_path)
fail_cnt = 0 # 统计安装成功的包数
pass_cnt = 0 # 统计安装失败的包数
fail_list = [] # 存放安装失败的包文件名
for filename in filename_list:
if os.path.splitext(filename)[1] == '.apk': # 判断是否是apk包
print('正在安装apk包:%s' % filename)
p = subprocess.Popen(adb_apth + ' install -r apk/' + filename, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, shell=True) # adb install -r 如果已存在则覆盖安装
try:
outs = p.communicate()[0]
if outs.find('Success'):
print('安装成功\n')
pass_cnt = pass_cnt + 1
else:
print('安装失败\n')
fail_cnt = fail_cnt + 1
fail_list.append(filename)
except Exception as e:
p.kill()
print('安装成功总数:', pass_cnt)
print('安装失败总数:', fail_cnt)
print ('安装失败的文件有:', fail_list)
注:
adb root 以root权限重新启动 adb的守护进程