UiAutomator 2.0 Overview, Features, and Integration Guide for Android Testing
This article provides a comprehensive overview of Android UiAutomator 2.0, detailing its major features, migration differences from UiAutomator 1.0, important considerations, Gradle integration steps, test case creation and execution, as well as the UiDevice API and new UiObject2 capabilities for mobile UI automation.
Google Android Developers released UiAutomator 2.0 in March 2015, introducing Instrumentation‑based testing, Logcat logging, a new package name (android.support.test.uiautomator), JUnit4 support without inheritance, Gradle‑based APK output, and new interfaces such as UiObject2, Until, By, and BySelector.
Key differences from UiAutomator 1.0 include the ability to run UiAutomator within Instrumentation tests, access to application context and Android services, and the requirement to specify EditText fields explicitly for text input.
Important considerations cover matching the tested app's signature, handling reflection and code obfuscation due to package name changes, retrieving command‑line arguments via InstrumentationRegistry, Logcat versus System.out logging, dynamic permission requests on Android M+, and limitations on shell commands that may require system signing, root access, or a Java daemon.
Common issues such as NoClassDefFoundError on Android 4.4 can be resolved by enabling MultiDex.
Integration steps involve adding the UiAutomator 2.0 dependency to the Gradle build file, creating test cases under src/androidTest/java , building the test APK with Gradle (assembleAndroidTest), installing it, and launching tests via the appropriate command line.
The UiDevice API provides a singleton representing device state, offering methods to query screen resolution, rotation, power status, and to perform actions like pressing home, back, volume keys, swiping, dragging, taking screenshots, and registering listeners.
Two usage patterns for UiDevice are shown, with the first ( UiDevice.getInstance().pressHome() ) recommended over the second ( getUiDevice().pressHome() ) due to stability.
Visual references illustrate the new UiObject2 class, basic UiObject features, and BySelector usage.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.