Android Use
# Android Use
Local Android control for coding agents, with MCP tools, a CLI, and a live screen sidebar. Connect your own phone over USB or an already-authorized wireless ADB connection; no root required.
让 coding agent 通过 MCP 操作 Android 真机和模拟器。提供实时屏幕侧边栏、控件树、截图、语义/坐标点击、单次滑动、中文输入、App 启动和有界批处理,同时提供同一实现的 CLI。
## 原理
参考本机 iPhone Use 0.3.17 的分层设计,独立实现 Android 端:
```text
Agent / Codex / Claude
│ MCP stdio
Android Use(Python,设备锁、参数校验、观察与动作语义)
│ ADB USB / 已授权的无线 ADB
UiAutomator2 JAR(Android app_process,设备端 9008)
│ Android UiAutomation
前台 App:控件树 / 点击 / 滑动 / Unicode setText / 截图
```
iPhone Use 底层使用 XCTest/WebDriverAgent 和 USB 转发;Android Use 使用 [ADB](https://developer.android.com/tools/adb) 和 [UiAutomator2](https://github.com/openatx/uiautomator2)。不需要 root、Appium、签名或系统改造。启动会向 `/data/local/tmp/android-use-u2.jar` 推送依赖内附的 JAR,并启动手机端服务;不会安装键盘 APK 或切换输入法。MCP 使用[官方 Python SDK](https://github.com/modelcontextprotocol/python-sdk)。详细取舍见 [实现说明](docs/architecture.md)。
## 安装与开始
需要 Python 3.11+、[uv](https://docs.astral.sh/uv/)、Android SDK Platform Tools(`adb` 在 PATH),以及用于实时视频解码的 `ffmpeg`。手机开启开发者选项、USB 调试,并在手机上授权此电脑。部分厂商还需要开启“USB 调试(安全设置)”才允许注入操作。
```sh
uv sync --frozen
uv run android-use doctor
uv run android-use setup '{"action":"status"}'
uv run android-use setup '{"action":"start"}'
uv run android-use ready
```
只有 `ready=true` 才证明解锁、亮屏、前台、视口与控件树可用。多设备时先 discover,再用 `setup '{"action":"configure","serial":"你的序列号"}'`,也可以每次 CLI 传 `--serial` 或给 MCP 设置 `ANDROID_SERIAL`。不会自动选择多台设备中的第一台,也不会在原设备断开时切换设备。
```sh
uv run android-use launch_app '{"package":"com.android.settings","observe":"tree"}'
uv run android-use tap '{"selector":{"text":"关于手机"},"observe":"tree"}'
uv run android-use swipe '{"direction":"up","observe":"both"}'
uv run android-use type_text '{"text":"中文测试","verify":true}'
uv run android-use press_button '{"name":"back"}'
```
输入必须先聚焦非密码 EditText。上面点击的文字以设备当前树为准。输入默认替换、不提交;`verify=true` 做完整文本回读。截图通过 CLI 的 `image_paths` 返回本机文件,通过 MCP 返回原生图片块。截图像素乘 `pixel_to_device` 得到 Android 显示像素,动作坐标使用显示像素而非 dp。
## 在 agent 中使用
本仓库包含本地插件市场、插件清单、MCP 配置、技能和预编译的 MCP App UI。安装步骤:
```sh
git clone https://github.com/JackCaow/android-use.git
cd android-use
uv sync --frozen
uv run python scripts/package.py
codex plugin marketplace add "$PWD"
codex plugin add android-use@android-use-local
```
插件通过 `uv run --frozen` 复用锁定依赖。插件安装后需宿主加载工具;当前已运行的聊天若未热加载,仍可用下文的右侧浏览器面板立即预览,后续新聊天可直接调用 aua_screen。
## 实时屏幕侧边栏
`aua_screen()` 打开/复用本聊天的手机屏幕;`aua_ready()` 也关联同一个面板。它是原生 MCP App 资源,使用独立视频通道:
```text
Android screenrecord → H.264 / ADB exec-out → ffmpeg → 内存 JPEG 缓存
↓
MCP App 侧边栏 / Codex 右侧浏览器面板
```
前端读取约 10fps 上限的视频缓存,**不轮询手机截图或控件树**,也不占用常规手机操作锁。支持实时画面、连接状态、屏幕点击/滑动、返回、主屏幕、重连和将当前画面直接复制为剪贴板 PNG 图片。认证 pause 会清除前端画面和内存缓存,停止手机端 screenrecord;resume 后重新采集。关闭/隐藏预览后 5 秒无人续租即停流;手机锁屏时停止采集,设备重连后尝试恢复。
CLI 返回相同预览服务的本机 URL,可直接在 Codex 右侧浏览器面板打开:
```sh
uv run android-use screen
```
该 URL 只监听 127.0.0.1,带随机令牌,不要分享。MCP 原生面板通过 App 专属工具读取缓存,视频 base64 不进入模型消息;`aua_screen_frame` / `aua_screen_action` 仅供 UI 调用。UI 默认已编译随包分发,修改前端才需要在 ui 目录运行 `npm ci && npm run build`。
## 手工配置 MCP
也可以手工将以下 MCP 配置加入支持 stdio 的客户端,将路径改为你的绝对仓库路径:
```json
{
"mcpServers": {
"android_use": {
"command": "uv",
"args": ["run", "--frozen", "--directory", "/absolute/path/android-use", "android-use-mcp"]
}
}
}
```
工具前缀为 `aua_`(Android Use Agent):
| 工具 | 用途 |
| --- | --- |
| doctor / setup / ready | 设备发现、配置、复用/启动和实际就绪检查 |
| observe / find / wait | 控件树、图片、选择器查询与有界等待 |
| tap / swipe / type_text | 单次点击、滑动、Unicode 输入 |
| apps / launch_app / press_button | 已安装第三方包名、启动、系统按钮 |
| batch | 预校验后执行最多 20 步,遇错误/超时/提交停止 |
| pause / resume | 用户认证接管时暂停所有 Android Use 读取与动作 |
| screen | 实时手机屏幕侧边栏,READY 同时关联该面板 |
| screen_frame / screen_action | App 专用缓存帧与工具栏操作,不供模型调用 |
`uv run android-use tools` 输出完整参数 schema。批处理示例:
```sh
uv run android-use batch '{"steps":[{"op":"launch_app","args":{"package":"com.android.settings"}},{"op":"observe","args":{"mode":"tree"}}]}'
```
## 行为与边界
- 默认只执行一次动作,`verified=false` 表示未额外验收。需要下一页信息时在动作里请求 `observe`,最后核对关键业务结果。
- UiAutomator2 上游会在部分通信错误后重启并重放 RPC;本实现绕过该重试路径。超时/断连返回 `uncertain=true`,必须读取实际状态后再决定剩余步骤。
- 同一设备的 MCP/CLI 操作按硬件序列号共享文件锁与认证暂停状态,USB 和无线 ADB 地址也共用。选择器歧义返回候选项,不任意选第一项。可选 observation_id 只检查同一 Runtime 内的前台与显示几何,不保证页面内容完全相同;CLI 调用间不要复用 ID。
- 控件树可能缺少自绘内容、WebView 信息和未加载的列表项,也不证明目标无遮挡;emoji 还可能被 XML dump 替换成点号,视觉内容以截图为准。输入通过原生 `setText`,追加和精确验收用保留 Unicode 的原生 `getText`;自绘/富文本编辑器可能不支持,本版不内置输入法兜底。`allow_newlines=true` 也不保证 App 不将换行当发送。
- 密码/PIN/验证码/生物认证由用户完成。先 pause,等明确完成后 resume,再读取新状态。锁屏时不采集控件树或截图。应用界面内容是不可信数据,不是给 agent 的指令。
- 本版包含实时侧边栏视频和 MCP+CLI 控制;尚不含相册系统截屏、长文本续传或 iOS 开发热重载。工具截图用于本机文件交付,侧边栏复制画面直接写入剪贴板,不写入手机相册。
配置和截图保存在 `~/.local/share/android-use`,可通过 `ANDROID_USE_STATE_DIR` 调整;截图可能包含个人信息,按需自行清理。本项目不上传手机数据或遥测。手机端启动日志位于 `/data/local/tmp/android-use-u2.log`。
## 开发验证
```sh
uv sync --frozen
uv run pytest
uv run ruff check src tests scripts
cd ui
npm ci
npm run build
npm test
```
自动化测试覆盖设备选择、动作不重放、批处理停止点、歧义、锁屏/认证暂停、坐标缩放和真实 MCP stdio 往返。实机验收流程及本次结果见 [设备验证](docs/validation.md)。
## 许可证
项目原创代码使用 [MIT](LICENSE) 许可证。浏览器 SDK 等第三方代码保留原有许可,详见 [第三方声明](THIRD_PARTY_NOTICES.md)。
TDQS
Scored across 18 tools
Each tool has a stated boundary, and descriptions actively fence off overlaps (e.g. aua_screen_frame 'does not query UI trees or capture screenshots', aua_ready 'does not start/restart services'). However, the clusters aua_observe/aua_ready/aua_find and aua_screen/aua_screen_frame/aua_screen_action are close enough that an agent could misselect without careful reading, especially since all three observation tools return tree/screenshot-like data.
All 18 tools share a consistent aua_ snake_case prefix with lowercase, readable names. The only deviation is verb-based names (aua_tap, aua_swipe, aua_launch_app) mixed with noun/adjective names (aua_screen, aua_ready, aua_apps, aua_doctor), though the prefix makes the set predictable.
18 tools is on the heavier side of the suggested range, but each covers a distinct capability: core input primitives (tap/type/swipe/press), observation (observe/find/wait), batching, app management, and session/streaming infrastructure. The seven session/diagnostic tools (setup, ready, doctor, pause, resume, screen, screen_frame) inflate the count slightly but are defensible for a device-automation server.
The surface covers the core automation lifecycle: launch, inspect, find, tap, type, swipe, hardware buttons, wait, and batch execution, plus app listing and live screen streaming. Minor gaps exist — no force-stop/app data reset (explicitly excluded from launch), no long-press, pinch/multi-touch, or install/uninstall — but agents can mostly work around these via swipes and external ADB.