u2-cli
CLI tool for controlling Android devices via uiautomator2, designed for use by AI agents.
All commands output structured JSON: {"success": bool, "output": str, "code": str} where code is the uiautomator2 Python expression that was executed.
Installation
pip install u2-cli
Or install from source:
git clone https://github.com/yourname/u2-cli
cd u2-cli
uv sync
Usage
# Connect to the only connected device
u2 <command>
# Connect to a specific device by serial
u2 -s <serial> <command>
Commands
device — Device state and low-level operations
u2 device info # Show device info
u2 device battery # Show battery info
u2 device wlan-ip # Show WLAN IP address
u2 device shell <cmd> # Run adb shell command
u2 device keyevent <key> # Send key event (home, back, power, ...)
u2 device push <src> <dst> # Push file to device
u2 device pull <src> <dst> # Pull file from device
u2 device screen-on # Turn screen on
u2 device screen-off # Turn screen off
u2 device orientation # Get screen orientation
u2 device orientation <value> # Set orientation: natural, left, right, upsidedown
app — App lifecycle
u2 app start <package> # Start app
u2 app start <package> -a <activity> # Start specific activity
u2 app stop <package> # Stop app
u2 app install <path-or-url> # Install APK
u2 app uninstall <package> # Uninstall app
u2 app clear <package> # Clear app data
u2 app list # List installed apps
u2 app current # Show foreground app
u2 app wait-activity <activity> # Wait for activity to appear (--timeout/-t)
screen — Visual state
u2 screen screenshot # Print PNG bytes to stdout
u2 screen screenshot <path> # Save screenshot to file
u2 screen dump # Dump UI hierarchy as XML
u2 screen dump --simplify # Dump as compact JSON (removes invisible nodes, shortens names)
u2 screen size # Show screen size (WxH)
u2 screen brightness # Get brightness (0–255)
u2 screen brightness <value> # Set brightness
interact — Coordinate-based input
u2 interact tap <x> <y> # Tap at coordinates
u2 interact long-tap <x> <y> # Long tap (--duration/-d, default 0.5s)
u2 interact swipe <fx> <fy> <tx> <ty> # Swipe from point to point
u2 interact drag <sx> <sy> <ex> <ey> # Drag from point to point
u2 interact type <text> # Type text into focused element (supports Unicode/CJK)
u2 interact clear # Clear text in focused element
element — Selector-based element interaction
Selector options (at least one required): --text/-t, --resource-id/-r, --class-name/-c, --xpath/-x, --description/-d
u2 element find -t "Login" # Find elements matching selector
u2 element wait -t "Login" # Wait for element to appear (--timeout/-T)
u2 element exists -r "com.app:id/btn" # Check if element exists
u2 element get-text -t "Username" # Get text content of element
u2 element set-text <value> -t "..." # Set text on element
u2 element tap -t "Login" # Tap matching element
u2 element long-tap -x "//button" # Long-tap matching element
watch — UI watchers
Watchers auto-trigger actions when a matching element appears.
u2 watch add <name> -x <xpath> # Register watcher (--action: click/back/home, --timeout/-t)
u2 watch remove <name> # Remove named watcher (use __all__ to remove all)
u2 watch list # List watcher status
u2 watch run -x <xpath> # Run one-shot unnamed watcher
Output Format
Every command returns a JSON object:
{
"success": true,
"output": "...",
"code": "d(text='Login').click()"
}
success: whether the command succeededoutput: human-readable result or error messagecode: the uiautomator2 Python expression that was executed
Development
# Install deps
uv sync
# Run during development (no install needed)
uv run src/u2/__init__.py <cmd>
# Lint
uv run ruff check .
uv run ruff format .
# Build for PyPI
uv build
Requirements
- Python >= 3.12
- Android device with USB debugging enabled (or connected via TCP/IP)
- uiautomator2 server installed on device (
u2initor viapython -m uiautomator2 init)
Metadata
Release files for u2-cli 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| u2_cli-0.1.1.tar.gz | 45.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| u2_cli-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 57.2 kB
Release files / u2_cli-0.1.1.tar.gz
| Download URL | u2_cli-0.1.1.tar.gz |
|---|---|
| Size | 45.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9f38dc0b23af85ae8b524b9a14e95f9cc8a93250957ac5df3c6082e62dedaeb8
|
|
BLAKE2b-256 checksum How to use checksums |
f335da9bebbb25a2781174d5ee0f83667f8e68804dd1a46dc2466908a5a23566
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / u2_cli-0.1.1-py3-none-any.whl
| Download URL | u2_cli-0.1.1-py3-none-any.whl |
|---|---|
| Size | 12.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fa6b39f7b76b1a8e4903254775e62ee8994d365db5e4d8552f02a30279ff9466
|
|
BLAKE2b-256 checksum How to use checksums |
3dd3c2dd6f70ad4d6ae72cf9c7552f31a1219eb3f4711920c40b33957498cbfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for u2-cli 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.