1 问题背景
作为Android开发者的我们一般用Android Studio自带的Layout Inspector(布局检查器)来检查可调试APP的实时布局层级:
但是Layout Inspector有很大的局限性,它只能用来调试那些“可调试APP”,即该APP的AndroidManifest.xml
文件中Application
标签的android:debuggable
属性的值为true
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.helloapplication">
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HelloApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
一般来说,“可调试APP”就是我们用Android Studio构建Android项目生成的“Debug应用”,而真正发布到应用市场上的APP都是“Release应用”,例如微信、QQ、支付宝等APP,而“release应用”是不可以用于调试的。
但有的时候我们想要研究这些“Release应用”的布局层级和UI实现时,我们该怎么办呢?例如博主上大学时,实验室Android课题组布置的大作业是开发一个高仿微信APP,在没有安卓微信的源码的情景下,我们有什么办法具体地了解微信APP详细的布局层级和视图属性呢?又或者在工作时,我们想借鉴某款竞品APP详细的UI视图和布局实现,这应该怎么做呢?
其实这个问题目前有3个较为有效的解决方案:
- 使用Android Studio自带的万能的布局检查器UI Automator Viewer,不用将安卓测试机Root和Exposed,也不用反编译任何“Release应用”即可查看任何三方应用和系统应用的布局层级。可参考文章《Android布局层次结构查看工具-uiautomatorviewer介绍》;
- 用ApkTools或者MT管理器对想调试的“Release应用”反编译为“Debug应用”、签名并打包,接着可直接使用Layout Inspector来检查可调试该APP的实时布局层级,例如上述截图中被反编译后可成功调试的“百度贴吧”APP。可参考文章《Android反编译debug调试详解(一)(附图)》;
- 将安卓测试机Root并Exposed,接着安装Xposed插件:Xdebuggable或App Debuggable即可直接使用Layout Inspector来检查可调试该APP的实时布局层级。可参考文章《掘金-android调试release包的方法》。
而本篇文章主要想详细介绍Android Studio自带的万能的布局检查器UI Automator Viewer的用法,因为它的上手成本很低并足够简单。
2 Android Studio自带的万能的布局检查器UI Automator Viewer
UI Automator Viewer是一个用来来扫描和分析Android应用程序的UI组件的GUI工具。这个工具本身是作为Android的UI Automator测试框架(Ui自动化框架)的配套工具而存在的,每当我们使用UI Automator测试框架编写UI功能自动化项目时,一定会经常使用该工具,因为该工具可以查看当前Android设备屏幕上的控件信息。
UI Automator Viewer之所以强大,正是因为它可以用来查看任何任何三方应用和系统应用的布局层级。例如下述下述UI Automator Viewer软件的截图展现了2021年8月26日更新的V8.0.11版本的微信“聊天列表页”的布局层级,我们可以发现微信主要用了ListView来实现“聊天列表页”的,虽然我们初学安卓时一直被教导用RecyclerView,因为它的性能比ListView好上很多。因此要么是做了足够的性能优化,要么就是历史遗留代码结构过于繁杂和耦合,重构的工作量太大,所以微信目前仍使用ListView来实现“聊天列表页”。
另外我们又能看到微信用RecyclerView实现了“通讯录列表页”:
其中,UI Automator Viewer的详细使用教程可参考文章《Android布局层次结构查看工具-uiautomatorviewer介绍》。
2.1 UI Automator Viewer的环境配置和打开方式
介绍了那么多,现在来介绍下UI Automator Viewer的环境配置方法和打开方式吧:
Windows电脑启动UI Automator Viewer可以参考文章《AppiumForWindows 菜鸟计划(五)uiautomatorviewer》;Mac电脑启动UI Automator Viewer可以参考文章《Mac终端快速启动uiautomatorviewer》。
2.2 UI Automator Viewer打开失败问题的解决方案
UI Automator Viewer是个有一定历史年代感的老工具了,所以在2021年的今天启动该工具会碰到无法打开等一系列的问题,通过网上查阅文献以及博主的亲身体验,UI Automator Viewer打开失败问题主要有两类:
- 电脑环境变量配置的JDK版本不是JDK8,导致UI Automator Viewer打开失败并报错
Error:Could not create the Java Virtual Machine
,该问题的解决方案就是将电脑环境变量配置的JDK版本重新指定为JDK8,问题具体的解决方案可参考文章《Mac下不能成功打开uiautomatorviewer的问题解决》; - 博主亲身碰到的问题,即Big Sur版本的Mac电脑无法打开UI Automator Viewer并报错
java.lang.NullPointerException
,该问题的解决方案就是从Eclipse官方下载最新版本的swt.jar包
,问题具体的解决方案可参考文章《StackExchange-Software Quality Assurance & Testing》和《GitHub-Broken GUI of UIAutomatorViewer on MacOS Big Sur #911》。
2.3 UI Automator Viewer打开失败问题的解决方案
按照上述步骤配置好UI Automator Viewer的环境变量并解决无法打开的问题后,我们就可以正常使用UI Automator Viewer啦,其详细使用教程可参考文章《Android布局层次结构查看工具-uiautomatorviewer介绍》。例如,你也可以通过UI Automator Viewer来查看微信各个页面详细的布局层级和视图:
本文参考文献:
android studio 4.0 Live Layout Inspector切换为老版本Layout Inspector
性能优化工具(六)-Legacy Layout Inspector
掘金-路遥TM-Layout Inspector 支持 3D 视图了!
掘金-android调试release包的方法 Xposed插件:Xdebuggable和App Debuggable
uiautomatorviewer not running on Mac Big Sur
Broken GUI of UIAutomatorViewer on MacOS Big Sur #911
Android布局层次结构查看工具-uiautomatorviewer介绍