Starting March 27, 2025, we recommend using android-latest-release instead of aosp-main to build and contribute to AOSP. For more information, see Changes to AOSP.
Stay organized with collections
Save and categorize content based on your preferences.
When running device-side tests (for example, instrumentations, UI Automator
tests), host-side collectors might not be ideal because it's difficult to
synchronize metric collection to a test running on a device. For example, a
screenshot taken asynchronously is likely to miss the wanted screen and be
useless.
To meet these use cases, a device-side version of our collectors exists
and can be used in any AndroidJUnitRunner instrumentation.
BaseMetricListener
can be implemented to automatically report metrics that are collected
in a way fully compatible with the Tradefed reporting pipeline.
This library is decoupled from Tradefed itself and can be used without Tradefed.
If you're using the AndroidJUnitTest
runner from Tradefed, you can specify the following command line option
to have your collector running with your tests:
When implementing on top of the base class BaseMetricListener, you can choose
when you would like to collect your metrics during the lifecycle of the
instrumentation:
When a test run starts: onTestRunStart
When a test case starts: onTestStart
When a test case ends: onTestEnd
When a test case fails: onTestFail
When a test run ends: onTestRunEnd
Interaction
The collection of metrics on the device side is made synchronously to the
instrumentation execution itself, and metrics are passed back to the
instrumentation results and parsed by Tradefed to be reported as part of the
invocation.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-06-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-18 UTC."],[],[],null,["# Device-side metrics collectors\n\nWhen running device-side tests (for example, instrumentations, UI Automator\ntests), host-side collectors might not be ideal because it's difficult to\nsynchronize metric collection to a test running on a device. For example, a\nscreenshot taken asynchronously is likely to miss the wanted screen and be\nuseless.\n\nTo meet these use cases, a device-side version of our collectors exists\nand can be used in any `AndroidJUnitRunner` instrumentation.\n[`BaseMetricListener`](https://android.googlesource.com/platform/platform_testing/+/refs/heads/android16-release/libraries/device-collectors/src/main/java/android/device/collectors/BaseMetricListener.java)\ncan be implemented to automatically report metrics that are collected\nin a way fully compatible with the Tradefed reporting pipeline.\n\nThis library is decoupled from Tradefed itself and can be used without Tradefed.\n\nIf you're using the [`AndroidJUnitTest`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/AndroidJUnitTest.java)\nrunner from Tradefed, you can specify the following command line option\nto have your collector running with your tests: \n\n --device-listeners android.device.collectors.ScreenshotListener\n\n**Caution:** In order for the collector classes to be resolved at runtime, your instrumentation APK needs to statically include them by adding the following to your makefile: \n\n LOCAL_STATIC_JAVA_LIBRARIES += collector-device-lib\n\nImplementation\n--------------\n\nWhen implementing on top of the base class `BaseMetricListener`, you can choose\nwhen you would like to collect your metrics during the lifecycle of the\ninstrumentation:\n\n- When a test run starts: `onTestRunStart`\n- When a test case starts: `onTestStart`\n- When a test case ends: `onTestEnd`\n- When a test case fails: `onTestFail`\n- When a test run ends: `onTestRunEnd`\n\nInteraction\n-----------\n\nThe collection of metrics on the device side is made synchronously to the\ninstrumentation execution itself, and metrics are passed back to the\ninstrumentation results and parsed by Tradefed to be reported as part of the\ninvocation."]]