自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
Tradefed 中的设备检测
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
新设备连接会触发一系列异步事件,这些事件不好理解,但值得我们了解一下。
物理连接
Tradefed 使用 ddmlib
库(一个 Java adb
库)提供与 adb
和设备的基本交互。此解决方案的一部分是 IDeviceChangeListener 接口,该接口用于接收新设备事件,如:
deviceConnected
:当 adb
看到新设备时
deviceDisconnected
:当设备不再向 adb
报告时
deviceChanged
:当出现主要设备状态时(如设备离线或设备在线)
这些事件在 adb
级别足以决定设备是处于已连接、在线还是离线状态。但对于自动化测试框架,我们需要一种比这更强大的状态来确保设备真正准备好开始运行测试;它不应受新连接的设备可能带来的潜在状态不稳定的影响。
下面是 Tradefed 中的事件序列:
- 设备被识别为
deviceConnected
并对 adb
中的常规事件开放
创建一个内部 Tradefed 事件,该事件将:
- 检查设备是否已知;Tradefed 会保持对某些设备的内部引用(尤其是当前已分配且正在运行测试的设备),以避免 TF 随便地与它们失去联系。
- 检查设备是处于
ONLINE
还是 OFFLINE
状态。
如果设备处于以下状态:
OFFLINE
:设备将切换到 Tradefed CONNECTED_OFFLINE
状态,这种状态尚不允许设备运行测试。如果设备之后变为在线状态,它将经过 ONLINE
循环。如果我们收到 deviceDisconnect
事件,会直接从列表中移除该设备。
ONLINE
(adb 所看到的状态):系统会将设备置于 CONNECTED_ONLINE
状态,并检查其可用性以进行测试分配:checking_availability
。
如果 availability
检查成功,会将设备标记为可用于测试分配,因此它将能够运行测试。否则,会将设备标记为 unavailable
进行分配,因此它无法接收任何测试。
通过 tf> list devices
列出相应设备时,所有这些状态都会反映在 Tradefed 控制台中。
务必注意,如果当前已分配设备来运行测试,那么上述大部分事件都不会发生,而是由 Tradefed 在内部确定设备状态。因此,有可能某个设备已从 adb devices
中消失,而仍由 Tradefed 列出。例如,当测试重新启动设备时,可能会发生这种情况。
通过“adb connect”连接的虚拟设备
创建远程虚拟设备后,Tradefed 将使用 adb
connect
连接到该设备。这通常会触发以 <some ip>:<port number>
的形式显示在 adb devices
中的设备,并且将遵循与物理连接的设备相同的顺序。
发生 deviceConnected 事件时的设备跟踪
当发生 deviceConnected
时,ddmlib
会创建一个新的引用 IDevice 来跟踪新连接的设备。
Tradefed 使用该引用并将其封装在自己的设备接口 ITestDevice 实现中,以提供更高级的服务。但是,底层 IDevice 始终来自 ddmlib
。
这意味着,如果连接了一个新设备,系统会创建一个新的 ITestDevice 并使其与 IDevice 相关联。如果在调用期间发生这种情况,并且正在使用该 ITestDevice,仍会替换底层 IDevice,以便可以基于正确的引用继续进行测试。每次设备断开连接/重新连接时(例如,在重新启动期间),都会顺畅地完成此操作。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Device detection in Tradefed\n\nA new device connection triggers a series of asynchronous events that are not\nobvious yet worth understanding.\n\nPhysically connected\n--------------------\n\nTradefed uses the `ddmlib` library (a Java `adb` library) to provide the basic\ninteraction with `adb` and devices. Part of this solution is the\n[IDeviceChangeListener interface](https://android.googlesource.com/platform/tools/base/+/refs/heads/android16-release/ddmlib/src/main/java/com/android/ddmlib/AndroidDebugBridge.java)\nthat allows reception of new device events, such as:\n\n- `deviceConnected`: When a new device is seen by `adb`\n- `deviceDisconnected`: When a device is not reporting to `adb` anymore\n- `deviceChanged`: When a major device state occurs (such as device offline or device online)\n\nThese events are enough at the `adb` level to decide whether or not a device is\nconnected, online, or offline. But for the test harness, we need a stronger\nstate than this to ensure a device is truly ready to start running tests; it\nshould not be impacted by potential state flakiness that can come with a newly\nconnected device.\n\nThis is the sequence of events in Tradefed:\n\n1. Device is recognized as `deviceConnected` and open to regular events from `adb`\n2. An internal Tradefed event is created that will:\n\n - Check if the device is known already; Tradefed keeps internal reference to some devices (especially the one current allocated and running tests) to avoid TF losing track of them randomly.\n - Check if the device is `ONLINE` or `OFFLINE`.\n3. If device is:\n\n - `OFFLINE`: Device will be switched to Tradefed `CONNECTED_OFFLINE`\n state, which doesn't allow the device to run tests yet. If the device is\n online later, it will go through the `ONLINE` cycle. If we receive a\n `deviceDisconnect` event, the device will simply be removed from the list.\n\n - `ONLINE` (as seen by adb): Device will be put in the state\n `CONNECTED_ONLINE` and will have its availability checked for test\n allocation: `checking_availability`.\n\n4. If `availability` check is successful, the device will be marked as\n available for test allocation; it will be able to run tests. If not, the\n device will be marked as `unavailable` for allocation and cannot receive any\n tests.\n\nAll of these states are reflected in the Tradefed console when listing the\ndevices via: `tf\u003e list devices`\n\nIt's important to note that when the device is currently allocated for a test,\nmost of the above will not occur and Tradefed will determine the device state\ninternally. So it's possible for a device to disappear from `adb devices` while\nstill being listed by Tradefed. That can happen when a test is rebooting the\ndevice for example.\n\nVirtual device connected with adb connect\n-----------------------------------------\n\nWhen a remote virtual device is created, Tradefed connects to it using `adb\nconnect`. This will usually trigger the device showing in `adb devices` as\n`\u003csome ip\u003e:\u003cport number\u003e` and will follow the same sequence as physically\nconnected devices.\n\nDevice tracking when a deviceConnected event occurs\n---------------------------------------------------\n\nWhen `deviceConnected` occurs, `ddmlib` creates a new reference\n[IDevice](https://android.googlesource.com/platform/tools/base/+/refs/heads/android16-release/ddmlib/src/main/java/com/android/ddmlib/IDevice.java)\nto track the newly connected device.\n\nTradefed uses that reference and wraps it in its own implementation of device\ninterface\n[ITestDevice](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/device_build_interfaces/com/android/tradefed/device/ITestDevice.java)\nto provide more advanced service. But the underlying IDevice is always the one\ncoming from `ddmlib`.\n\nThis means if a new device is connected, a new ITestDevice is created and\nassociated with the IDevice. When this happens during an invocation and the\nITestDevice is being used, the underlying IDevice is still replaced so\ntesting can proceed on the proper reference. This is done seamlessly each time a\ndevice is disconnected/reconnected (for example, during a reboot)."]]