错误内容
There was a timeout error capturing the layout data from the device.
The device may be too slow, the captured view may be too complex, or the view may contain animations.
Please retry with a simplified view and ensure the device is responsive.
错误原因
如错误提示所示,这种获取布局的失败常常发生在布局较复杂的场景,比如SytemUI的statusWindow的布局截取。
原因是AndroidStudio获取布局时传入的超时时间默认是20s,超过这个时间还没有获取结果就会显示这个错误。
具体代码如下:
@JvmStatic
fun captureView(
window: ClientWindow, options: LayoutInspectorCaptureOptions
): LayoutInspectorResult {
val hierarchy =
window.loadWindowData(options, 20, TimeUnit.SECONDS) ?: return LayoutInspectorResult(
null,
"There was a timeout error capturing the layout data from the device.\n" +
"The device may be too slow, the captured view may be too complex, or the view may contain animations.\n\n" +
"Please retry with a simplified view and ensure the device is responsive."
)
...
}
解决方法
解决办法是修改这个超时的参数,这里我直接根据Android Studio的源码写了一个JAR,以用来规避这个问题。其实重新编译Android Studio源码或者重新编译里面的相关模块,也是可以的,只是难度比较大,我尝试的时候发现有一些依赖jar无法下载就放弃了。
JAR源码地址
下面给出这个JAR的源码,在本人的Github上。
Github源码,包含源码与生成的可直接使用的Jar