使用uiautomatorviewer工具遇到以下问题-Unexpected error while obtaining UI hierarchy

手机型号:荣耀V9 安卓9
使用adb工具自带的uiautomatorviewer.bat,打开后连接手机,报错如下:
据说是因为uiautomatorview 不兼容安卓8.0以上版本

解决方法:
新建一个uiauto.bat文件,代码如下:

adb shell uiautomator dump /sdcard/app.uix
#会在 D:softwareappium下生成app.uix,地址可根据自己的需求进行更改
adb pull /sdcard/app.uix D:softwareappiumapp.uix
adb shell screencap -p /sdcard/app.png
#会在 D:softwareappium下生成app.png,地址可根据自己的需求进行更改
adb pull /sdcard/app.png D:softwareappiumapp.png

双击运行uiauto.bat文件
打开uiautomatorviewer.bat,导入生成的app.uix和app.png文件,点击OK即可

原理可以理解成“截屏”:先将手机当前页面给“截屏”生成app.png,并且生成相对应的ui解析文件app.uix,将两个同时导入uiautomatorviewer就可以定位手机页面元素,不方便的是手机页面发生变化就要重新“截屏”一次,所以写成bat文件方便执行

原文地址:https://www.cnblogs.com/loveniko/p/13253815.html