Zero-Dependency Android Automation for AI Agents

Control Android devices with 85% fewer tokens.

u2-agent converts messy Android accessibility trees into ultra-compact semantic handles (@1, @2, ...) with sub-15ms cached execution. Built specifically for Claude, GPT-4o, and autonomous agent loops.

$ git clone https://github.com/themikehage/u2-agent
agent-session • Pixel 7 Pro Daemon: Hot (11ms)

Why existing tools cripple mobile AI agents

Standard mobile tools dump raw Android View hierarchies containing thousands of useless container nodes. Your agent's context window gets choked before it even takes its first action.

Standard XML Dump / Appium
~15,200 Tokens
Token Footprint
~15k tokens
Execution Latency
~850 ms
<hierarchy rotation="0"> <node index="0" class="android.widget.FrameLayout" package="com.google.android.youtube" bounds="[0,0][1080,2400]"> <node index="0" class="android.widget.LinearLayout" bounds="[0,0][1080,2400]"> <node index="0" class="android.view.ViewGroup" bounds="[0,96][1080,2400]"> <node index="0" class="androidx.recyclerview.widget.RecyclerView" bounds="[0,96][1080,2400]"> <node index="0" class="android.widget.FrameLayout" bounds="[0,96][1080,320]"> <node index="0" text="Search YouTube" resource-id="com.google.android.youtube:id/search_box" class="android.widget.EditText" bounds="[144,120][936,216]" /> </node> </node> </node> </node> </node> </hierarchy>
u2-agent Semantic Tree
~240 Tokens (-85%)
Token Footprint
~240 tokens
Execution Latency
< 15 ms
[App: com.google.android.youtube] [@1] Input "Search YouTube" [@2] Button "Explore trending" [@3] Item "Live lo-fi hip hop radio - beats to relax" [@4] Button "Subscribe" [@5] Tab "Home" [@6] Tab "Shorts" [@7] Tab "Subscriptions"

Token & Cost Savings Calculator

Slide to see how much context and API budget your agent saves across an automation workflow:

Agent Routine Steps: 15 steps
Standard XML Dumps: 225,000 tokens
u2-agent Snapshots: 3,600 tokens
Context Savings: 98% (221,400)
Est. Cost Saved (Claude / GPT-4o): $0.66 / run

How the 3-step agent loop works

Zero token overhead, predictable deterministic error codes, and warm background sessions.

01

Semantic Snapshot

u2-agent ui snapshot strips frame containers, merges overlapping ghost wrappers, and indexes every interactive button, input, and item with an ephemeral reference handle (@1, @2, ...).

$ u2-agent ui snapshot
02

Sub-15ms Cached Tap

The LLM issues an action specifying --ref "@N". The background daemon resolves exact screen coordinates directly from RAM in sub-15ms without cold re-dumping or ADB disconnects.

$ u2-agent ui tap --ref @1
03

Clean Stream Separation

stdout emits strictly compact machine text (ok or snapshot trees). Diagnostics, self-healing hints, and audit logs flow to stderr, preventing agent reasoning contamination.

ok

Engineered for agent autonomy

Strictly adhering to the architectural rules defined in AGENTS.md.

⚡

Zero Heavy Dependencies

Crafted purely with Bun and native TypeScript APIs. No bulky WebDriver dependencies, Puppeteer emulators, or Java runtime wrappers on the host.

🚀

Persistent Hot Daemon

Keeps ADB socket forwards and UIAutomator2 JSON-RPC sessions alive in the background, slashing per-action execution latency from 800ms to under 15ms.

🧠

Semantic Deduplication

Automatically collapses nested ghost wrappers with ≥ 85% bounding box overlap into single high-level semantic components (Input, Button, Tab, Toggle).

🤖

Universal Model Support

Drop-in compatible with Claude Code, OpenCode, Cursor, Antigravity, AutoGen, and custom Python / LangChain agent orchestration loops.

🔧

Tool Schema Discovery

Export standard function-calling schemas with tools schema --format openai ready to plug into Anthropic, OpenAI, or Gemini tool call catalogs.

🛡️

Deterministic Recovery

Every failure emits distinct machine error codes (SELECTOR_NOT_FOUND, DEVICE_OFFLINE, USAGE) accompanied by actionable recovery hints on stderr.

CLI Command Simulator

Test drive u2-agent commands and inspect their LLM-compact output shapes live.

UI Snapshot

Extracts the screen view tree and maps actionable items to handles.

u2-agent ui snapshot --limit 30

          

Up and running in 2 minutes

All you need is Bun and an Android device with USB debugging enabled.

1 Clone and install

Clone the repository and install dependencies with Bun:

git clone https://github.com/themikehage/u2-agent.git && cd u2-agent && bun install
2 Provision target device

Verify your connected device and install the lightweight UIAutomator2 RPC server:

bun run src/index.ts setup install
3 Inspect and interact

Take your first compact semantic snapshot and tap elements by handle:

bun run src/index.ts ui snapshot
4 (Optional) Compile standalone executable

Build a single self-contained binary without needing Bun on PATH:

bun run build

Under the hood

How u2-agent achieves sub-15ms interaction loops over ADB.

🤖
AI Agent Layer (Claude, GPT, OpenCode)
Issues CLI commands with handle selectors (--ref "@1")
Client
⚡
u2-agent Background Daemon (HTTP/IPC)
Holds active ADB connection hot • Resolves @handles in RAM (< 15ms)
In-Memory Cache
📱
Android Device (UIAutomator2 JSON-RPC)
Executes click, type, swipe, and hardware key operations natively
Target Device