背景
最近在学习Python,发现Python真是个有趣的语言。
比如开发代码审查脚本,去网站爬取小说,使用NLK分析处理文本。
作为一个Android开发人员,发现可以用Python来操作ADB实现自动化控制Android。
简单的命令
检查设备在线情况
命令:
adb devices
# 检查设备在线
def checkDevice():
devices = os.popen('adb devices').read().strip('\n').split('\n')
if len(devices) > 1:
return devices[1].split('\t')[0]
else:
return ""
# 获取设备名称
device = checkDevice()
print('查找到设备%s' % device)
查找到设备UYT0217C27001193
获取前台Activity
adb shell dumpsys activity activities | sed -En -e ‘/Running activities/,/Run #0/p’
# 获取前台Activity
def getFrontActivity(package):
info = ''
activitys = []
if package.strip('\n') == '':
info = os.popen('adb shell dumpsys activity activities | sed -En -e \'/Running activities/,/Run #0/p\'').read().strip('\n')
else:
info = os.popen('adb shell dumpsys activity activities | sed -En -e \'/Running activities/,/Run #0/p\' | grep %s' % package).read().strip('\n')
# 换行分组
infos = info.split('\n')
for one in infos:
if 'ActivityRecord' in one:
words = one.split(' ')
for word in words:
if '/' in word:
activitys.append(word)
return activitys
activitys = getFrontActivity('')
print(activitys)
[‘com.huawei.android.launcher/.unihome.UniHomeLauncher’, ‘com.smartisanos.smartfolder.aoa/.MainActivity’, ‘com.tencent.qqlive/.ona.activity.HomeActivity’, ‘com.tencent.mm/.ui.LauncherUI’, ‘com.tencent.tim/com.tencent.mobileqq.activity.SplashActivity’, ‘com.tencent.mm/.ui.chatting.ChattingUI’, ‘com.github.shadowsocks/.MainActivity’, ‘com.android.incallui/.InCallActivity’, ‘com.android.mms/.ui.ConversationList’]
拉取手机中的第三方应用APK
获取手机内app
- 不使用参数:adb shell pm list packages,打印设备/模拟器上的所有软件包
- 使用-f参数:adb shell pm list packages -f,输出包和包相关联的文件
- 使用-d参数:adb shell pm list packages -d,只输出禁用的包。由于本机禁用没有,输出为空。
- 使用-e参数:adb shell pm list packages -e,只输出启用的包。
- 使用-s参数:adb shell pm list packages -s,只输出系统的包。
- 使用-3参数:adb shell pm list packages -3,只输出第三方的包。
- 使用-i参数:adb shell pm list packages -i,只输出包和安装信息(安装来源)。
- 使用-u参数:adb shell pm list packages -u,只输出包和未安装包信息(安装来源)
- 使用–user参数:adb shell pm list packages --user <USER_ID>,根据用户id查询用户的空间的所有包,USER_ID代表当前连接设备的顺序,从零开始:
- 使用-e参数且设置过滤条件:adb shell pm list packages -e “ximalaya”,只输出启用的包。
根据包名获取apk位置
adb shell pm path 包名
拉取
adb pull 手机中apk的位置 保存在本地的位置
# 获取第三方应用
def getThirdApps():
packageNames = []
commond = 'adb shell pm list packages -3'
info = os.popen(commond).read().strip('\n')
apps = info.split('\n')
for app in apps:
app = app.replace('package:', '')
packageNames.append(app)
return packageNames
# 拉取apk
def pullApk(package):
getPathCommond = 'adb shell pm path %s' % package
path = bashInfo(getPathCommond)
path = path.replace('package:', '')
pullCommond = 'adb pull %s apk/%s.apk' % (path, package)
# 持续输出
subp = subprocess.Popen(pullCommond,shell=True,stdout=subprocess.PIPE)
c=subp.stdout.readline()
str = ''
while c:
str = c.decode().replace('\n','')
print(str,end='\r')
c=subp.stdout.readline()
print('')
print('\033[1;31m拉取%s完成\033[0m' % app)
apps = getThirdApps()
for app in apps:
pullApk(app)
/data/app/com.github.shadowsocks-NPJbkJaMOVQp7siAu87quw==/base.apk: 1 file pulled. 21.2 MB/s (5358433 bytes in 0.241s)
拉取com.github.shadowsocks完成
/data/app/com.google.android.youtube-nBzgg9z9RMQWfSvmIeqvWQ==/base.apk: 1 file pulled. 26.2 MB/s (31823824 bytes in 1.158s)
拉取com.google.android.youtube完成
/data/app/com.hpbr.bosszhipin-irjQ0m6h-BP9MPzAzpw6nA==/base.apk: 1 file pulled. 26.0 MB/s (38421175 bytes in 1.409s)
拉取com.hpbr.bosszhipin完成
……
结语
只是实现了一些简单的ADB命令,熟练的使用完全可以做到Android的自动化测试,自己编写测试脚本。