不通过文件转储直接从adb shell读取stdout数据

通常我们从Android设备中捕获当前UI界面信息使用以下命令:

1
2
adb shell uiautomator dump /sdcard/screen.xml
adb pull /sdcard/screen.xml

在上面的代码块中我们经历了两个步骤

  1. 执行命令结果输出到/sdcard/screen.xml

  2. 拉取设备上的文件

从效率上来看是很慢的那有没有比较快的办法呢? 答案是有的!
我们可以利用/dev/tty设备让它直接输出到stdout上这样就省去文件转储了

1
adb shell uiautomator dump /dev/tty

你满怀期待着等着结果只看到

1
UI hierchary dumped to: /dev/tty

WTF? 这是什么鬼dump出来的信息去哪了?眉头一紧事情好像貌似没有这么简单…
经过一番研究发现我们使用的adb shell会将命令输出发给pty经过一顿骚操作pty把数据搞丢了,所以在新版的adb中引入了一个新的功能exec-out该参数可以将二进制数据直接原封不动的输出。详情请参考

所以只需要将命令改成这样即可:

1
adb exec-out uiautomator dump /dev/tty
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2015-2024 Kaisar
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信