public AccessibilityNodeInfo getRootInActiveWindow() {
     return this.mUiAutomation.getRootInActiveWindow();

注入事件:

public boolean injectInputEvent(InputEvent event, boolean sync) {
     return this.mUiAutomation.injectInputEvent(event, sync);
Bitmap screenshot = this.mUiAutomation.takeScreenshot();

UiAutomatorBridge

UiAutomatorBridge相当于UiAutomation的代理,进行获取界面空间信息和注入事件,截图都需要经过UiAutomatorBridge

private boolean injectEventSync(InputEvent event) {
    return this.mUiAutomatorBridge.injectInputEvent(event, true);
this.mUiAutomatorBridge.getRootInActiveWindow(); 

QueryController

QueryController做的所有事情就是去把UiSelector这个UI控件选择子翻译成真实的适合我们使用的Android.view.accessibility.AccessibilityNodeInfo

protected AccessibilityNodeInfo findAccessibilityNodeInfo(long timeout)
    AccessibilityNodeInfo node = null;
  long startMills = SystemClock.uptimeMillis();
   long currentMills = 0L;
   while (currentMills <= timeout) {
     node = getQueryController().findAccessibilityNodeInfo(getSelector());
     if (node != null) {
        break;
     UiDevice.getInstance().runWatchers();
     currentMills = SystemClock.uptimeMillis() - startMills;
      if (timeout > 0L) {
        SystemClock.sleep(1000L);
    return node;

UiDevice的实例化

private UiDevice(Instrumentation instrumentation) {
    this.mInstrumentation = instrumentation;
    UiAutomation uiAutomation = instrumentation.getUiAutomation();
    this.mUiAutomationBridge = new InstrumentationUiAutomatorBridge(instrumentation.getContext(), uiAutomation);