Android Device Automation
Android Device Automation
CRITICAL RULES — VIOLATIONS WILL BREAK THE WORKFLOW:
- Never run midscene commands in the background. Each command must run synchronously so you can read its output (especially screenshots) before deciding the next action. Background execution breaks the screenshot-analyze-act loop.
- Run only one midscene command at a time. Wait for the previous command to finish, read the screenshot, then decide the next action. Never chain multiple commands together.
- Allow enough time for each command to complete. Midscene commands involve AI inference and screen interaction, which can take longer than typical shell commands. A typical command needs about 1 minute; complex
actcommands may need even longer.
Automate Android devices using npx @midscene/android@1. Each CLI command maps directly to an MCP tool — you (the AI agent) act as the brain, deciding which actions to take based on screenshots.
Prerequisites
Midscene requires models with strong visual grounding capabilities. The following environment variables must be configured — either as system environment variables or in a .env file in the current working directory (Midscene loads .env automatically):
MIDSCENE_MODEL_API_KEY="your-api-key"
MIDSCENE_MODEL_NAME="model-name"
MIDSCENE_MODEL_BASE_URL="https://..."
MIDSCENE_MODEL_FAMILY="family-identifier"
Example: Gemini (Gemini-3-Flash)
MIDSCENE_MODEL_API_KEY="your-google-api-key"
MIDSCENE_MODEL_NAME="gemini-3-flash"
MIDSCENE_MODEL_BASE_URL="https://generativelanguage.googleapis.com/v1beta/openai/"
MIDSCENE_MODEL_FAMILY="gemini"
Example: Qwen3-VL
MIDSCENE_MODEL_API_KEY="your-openrouter-api-key"
MIDSCENE_MODEL_NAME="qwen/qwen3-vl-235b-a22b-instruct"
MIDSCENE_MODEL_BASE_URL="https://openrouter.ai/api/v1"
MIDSCENE_MODEL_FAMILY="qwen3-vl"
Example: Doubao Seed 1.6
MIDSCENE_MODEL_API_KEY="your-doubao-api-key"
MIDSCENE_MODEL_NAME="doubao-seed-1-6-250615"
MIDSCENE_MODEL_BASE_URL="https://ark.cn-beijing.volces.com/api/v3"
MIDSCENE_MODEL_FAMILY="doubao-vision"
Commonly used models: Doubao Seed 1.6, Qwen3-VL, Zhipu GLM-4.6V, Gemini-3-Pro, Gemini-3-Flash.
If the model is not configured, ask the user to set it up. See Model Configuration for supported providers.
Commands
Connect to Device
npx @midscene/android@1 connect
npx @midscene/android@1 connect --deviceId emulator-5554
Take Screenshot
npx @midscene/android@1 take_screenshot
After taking a screenshot, read the saved image file to understand the current screen state before deciding the next action.
Perform Action
Use act to interact with the device and get the result. It autonomously handles all UI interactions internally — tapping, typing, scrolling, swiping, waiting, and navigating — so you should give it complex, high-level tasks as a whole rather than breaking them into small steps. Describe what you want to do and the desired effect in natural language:
# specific instructions
npx @midscene/android@1 act --prompt "type hello world in the search field and press Enter"
npx @midscene/android@1 act --prompt "long press the message bubble and tap Delete in the popup menu"
# or target-driven instructions
npx @midscene/android@1 act --prompt "open Settings and navigate to Wi-Fi settings, tell me the connected network name"
Disconnect
npx @midscene/android@1 disconnect
Workflow Pattern
Since CLI commands are stateless between invocations, follow this pattern:
- Connect to establish a session
- Launch the target app and take screenshot to see the current state, make sure the app is launched and visible on the screen.
- Execute action using
actto perform the desired action or target-driven instructions. - Disconnect when done
Best Practices
- Bring the target app to the foreground before using this skill: For best efficiency, launch the app using ADB (e.g.,
adb shell am start -n <package/activity>) before invoking any midscene commands. Then take a screenshot to confirm the app is actually in the foreground. Only after visual confirmation should you proceed with UI automation using this skill. ADB commands are significantly faster than using midscene to navigate to and open apps. - Be specific about UI elements: Instead of vague descriptions, provide clear, specific details. Say
"the Wi-Fi toggle switch on the right side"instead of"the toggle". - Describe locations when possible: Help target elements by describing their position (e.g.,
"the search icon at the top right","the third item in the list"). - Never run in background: Every midscene command must run synchronously — background execution breaks the screenshot-analyze-act loop.
- Batch related operations into a single
actcommand: When performing consecutive operations within the same app, combine them into oneactprompt instead of splitting them into separate commands. For example, "open Settings, tap Wi-Fi, and toggle it on" should be a singleactcall, not three. This reduces round-trips, avoids unnecessary screenshot-analyze cycles, and is significantly faster. - Summarize report files after completion: After finishing the automation task, collect and summarize all report files (screenshots, logs, output files, etc.) for the user. Present a clear summary of what was accomplished, what files were generated, and where they are located, making it easy for the user to review the results.
Example — Popup menu interaction:
npx @midscene/android@1 act --prompt "long press the message bubble and tap Delete in the popup menu"
npx @midscene/android@1 take_screenshot
Example — Form interaction:
npx @midscene/android@1 act --prompt "fill in the username field with 'testuser' and the password field with 'pass123', then tap the Login button"
npx @midscene/android@1 take_screenshot
Troubleshooting
| Problem | Solution |
|---|---|
| ADB not found | Install Android SDK Platform Tools: brew install android-platform-tools (macOS) or download from developer.android.com. |
| Device not listed | Check USB connection, ensure USB debugging is enabled in Developer Options, and run adb devices. |
| Device shows "unauthorized" | Unlock the device and accept the USB debugging authorization prompt. Then run adb devices again. |
| Device shows "offline" | Disconnect and reconnect the USB cable. Run adb kill-server && adb start-server. |
| Command timeout | The device screen may be off or locked. Wake the device with adb shell input keyevent KEYCODE_WAKEUP and unlock it. |
| API key error | Check .env file contains MIDSCENE_MODEL_API_KEY=<your-key>. See Model Configuration. |
| Wrong device targeted | If multiple devices are connected, use --deviceId <id> flag with the connect command. |