android
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAndroid Device Mastery
Android 设备全方位操作指南
This skill covers everything about interacting with Android devices via ADB and shell commands.
本技能涵盖了所有通过ADB和shell命令与Android设备交互的操作。
Device Connection
设备连接
Check Connected Devices
查看已连接设备
bash
adb devices -lOutput shows serial, status, and device info. Common statuses:
- - Connected and authorized
device - - Accept USB debugging prompt on device
unauthorized - - Connection issues, try
offlineadb kill-server && adb start-server
bash
adb devices -l输出显示设备序列号、状态及设备信息。常见状态:
- - 已连接且已授权
device - - 请在设备上接受USB调试授权提示
unauthorized - - 连接异常,尝试执行
offlineadb kill-server && adb start-server
Wireless Debugging
无线调试
Android 11+ (Recommended):
- Enable Wireless debugging in Developer Options
- Tap "Pair device with pairing code"
- Run: and enter the code
adb pair <ip>:<pairing_port> - Then:
adb connect <ip>:<connection_port>
Legacy (requires USB first):
bash
adb tcpip 5555
adb connect <device_ip>:5555Android 11及以上版本(推荐方式):
- 在开发者选项中启用无线调试
- 点击“使用配对码配对设备”
- 执行:并输入配对码
adb pair <ip>:<pairing_port> - 然后执行:
adb connect <ip>:<connection_port>
传统方式(需先通过USB连接):
bash
adb tcpip 5555
adb connect <device_ip>:5555Multiple Devices
多设备场景
Always specify device with :
-sbash
adb -s <serial> shell
adb -s emulator-5554 install app.apk操作时需通过 指定设备:
-sbash
adb -s <serial> shell
adb -s emulator-5554 install app.apkShell Commands
Shell命令
Interactive Shell
交互式Shell
bash
adb shell # Enter shell
adb shell <command> # Run single command
adb shell "cmd1 && cmd2" # Chain commandsbash
adb shell # 进入Shell环境
adb shell <command> # 执行单个命令
adb shell "cmd1 && cmd2" # 链式执行多个命令Essential Commands
常用核心命令
bash
undefinedbash
undefinedDevice info
设备信息
getprop ro.product.model # Device model
getprop ro.build.version.release # Android version
getprop ro.build.version.sdk # SDK level
getprop ro.product.model # 设备型号
getprop ro.build.version.release # Android版本
getprop ro.build.version.sdk # SDK版本号
File operations
文件操作
ls -la /sdcard/
cat /path/to/file
cp /source /dest
rm /path/to/file
ls -la /sdcard/
cat /path/to/file
cp /source /dest
rm /path/to/file
Process info
进程信息
ps -A | grep <name>
pidof <package_name>
top -n 1 -m 10
undefinedps -A | grep <name>
pidof <package_name>
top -n 1 -m 10
undefinedSafe Output Limiting
输出内容限制(避免过多输出)
For commands with potentially large output:
bash
adb shell "logcat -d | head -500"
adb shell "dumpsys activity | head -200"对于可能产生大量输出的命令:
bash
adb shell "logcat -d | head -500"
adb shell "dumpsys activity | head -200"App Management
应用管理
Install/Uninstall
安装/卸载
bash
adb install app.apk # Basic install
adb install -r app.apk # Replace existing
adb install -g app.apk # Grant all permissions
adb install -r -g app.apk # Both
adb uninstall com.example.app # Full uninstall
adb uninstall -k com.example.app # Keep databash
adb install app.apk # 基础安装
adb install -r app.apk # 覆盖安装现有应用
adb install -g app.apk # 授予应用所有权限
adb install -r -g app.apk # 覆盖安装并授予所有权限
adb uninstall com.example.app # 完全卸载
adb uninstall -k com.example.app # 保留数据卸载Start/Stop Apps
启动/停止应用
bash
undefinedbash
undefinedStart main activity
启动主Activity
adb shell monkey -p com.example.app -c android.intent.category.LAUNCHER 1
adb shell monkey -p com.example.app -c android.intent.category.LAUNCHER 1
Start specific activity
启动指定Activity
adb shell am start -n com.example.app/.MainActivity
adb shell am start -n com.example.app/.MainActivity
Start with intent extras
携带Intent参数启动
adb shell am start -n com.example.app/.Activity
-a android.intent.action.VIEW
-d "myapp://page/123"
--es "key" "value"
-a android.intent.action.VIEW
-d "myapp://page/123"
--es "key" "value"
adb shell am start -n com.example.app/.Activity
-a android.intent.action.VIEW
-d "myapp://page/123"
--es "key" "value"
-a android.intent.action.VIEW
-d "myapp://page/123"
--es "key" "value"
Force stop
强制停止应用
adb shell am force-stop com.example.app
adb shell am force-stop com.example.app
Clear app data
清除应用数据
adb shell pm clear com.example.app
undefinedadb shell pm clear com.example.app
undefinedList Packages
列出应用包
bash
adb shell pm list packages # All packages
adb shell pm list packages -3 # Third-party only
adb shell pm list packages | grep term # Filter
adb shell pm path com.example.app # APK location
adb shell dumpsys package com.example.app # Full package infobash
adb shell pm list packages # 列出所有应用包
adb shell pm list packages -3 # 仅列出第三方应用包
adb shell pm list packages | grep term # 过滤查找应用包
adb shell pm path com.example.app # 查看APK文件路径
adb shell dumpsys package com.example.app # 查看应用包完整信息Permissions
权限管理
bash
adb shell pm grant com.example.app android.permission.CAMERA
adb shell pm revoke com.example.app android.permission.CAMERA
adb shell dumpsys package com.example.app | grep permissionbash
adb shell pm grant com.example.app android.permission.CAMERA
adb shell pm revoke com.example.app android.permission.CAMERA
adb shell dumpsys package com.example.app | grep permissionFile Operations
文件操作
Push/Pull Files
推送/拉取文件
bash
adb push local_file.txt /sdcard/
adb pull /sdcard/remote_file.txt ./bash
adb push local_file.txt /sdcard/
adb pull /sdcard/remote_file.txt ./Recursive
递归推送/拉取
adb push local_dir/ /sdcard/target/
adb pull /sdcard/dir/ ./local/
undefinedadb push local_dir/ /sdcard/target/
adb pull /sdcard/dir/ ./local/
undefinedAccess App Data (Debuggable Apps)
访问应用数据(仅可调试应用)
bash
adb shell run-as com.example.app ls files/
adb shell run-as com.example.app cat shared_prefs/prefs.xml
adb shell run-as com.example.app sqlite3 databases/app.db ".tables"bash
adb shell run-as com.example.app ls files/
adb shell run-as com.example.app cat shared_prefs/prefs.xml
adb shell run-as com.example.app sqlite3 databases/app.db ".tables"UI Automation
UI自动化
Input Commands
输入命令
bash
undefinedbash
undefinedTap at coordinates
点击指定坐标
adb shell input tap 500 800
adb shell input tap 500 800
Swipe (x1 y1 x2 y2 [duration_ms])
滑动操作(x1 y1 x2 y2 [持续时间毫秒])
adb shell input swipe 500 1500 500 500 300
adb shell input swipe 500 1500 500 500 300
Text input (needs focused field)
输入文本(需先聚焦输入框)
adb shell input text "hello"
adb shell input text "hello"
Key events
按键事件
adb shell input keyevent KEYCODE_HOME # Home
adb shell input keyevent KEYCODE_BACK # Back
adb shell input keyevent KEYCODE_ENTER # Enter
adb shell input keyevent KEYCODE_POWER # Power
adb shell input keyevent KEYCODE_VOLUME_UP # Volume up
undefinedadb shell input keyevent KEYCODE_HOME # 主页键
adb shell input keyevent KEYCODE_BACK # 返回键
adb shell input keyevent KEYCODE_ENTER # 回车键
adb shell input keyevent KEYCODE_POWER # 电源键
adb shell input keyevent KEYCODE_VOLUME_UP # 音量加键
undefinedCommon Keycodes
常用按键码
| Code | Key | Code | Key |
|---|---|---|---|
| 3 | HOME | 4 | BACK |
| 24 | VOL_UP | 25 | VOL_DOWN |
| 26 | POWER | 66 | ENTER |
| 67 | DEL | 82 | MENU |
| 代码 | 按键 | 代码 | 按键 |
|---|---|---|---|
| 3 | 主页 | 4 | 返回 |
| 24 | 音量加 | 25 | 音量减 |
| 26 | 电源 | 66 | 回车 |
| 67 | 删除 | 82 | 菜单 |
Screenshots & Recording
截图与录屏
bash
undefinedbash
undefinedScreenshot
截图
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
One-liner (binary-safe)
一行命令(二进制安全)
adb exec-out screencap -p > screen.png
adb exec-out screencap -p > screen.png
Screen recording (max 3 min)
录屏(最长3分钟)
adb shell screenrecord /sdcard/video.mp4
adb shell screenrecord --time-limit 10 /sdcard/video.mp4
undefinedadb shell screenrecord /sdcard/video.mp4
adb shell screenrecord --time-limit 10 /sdcard/video.mp4
undefinedUI Hierarchy
UI层级结构
bash
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xmlbash
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xmlDebugging & Logs
调试与日志
Logcat Essentials
Logcat核心用法
bash
undefinedbash
undefinedDump and exit
导出日志后退出
adb logcat -d
adb logcat -d
Last N lines
导出最后N行日志
adb logcat -t 100
adb logcat -t 100
Filter by tag:priority
按标签:优先级过滤
adb logcat ActivityManager:I *:S
adb logcat ActivityManager:I *:S
Filter by package (get PID first)
按应用包过滤(需先获取PID)
adb logcat --pid=$(adb shell pidof -s com.example.app)
adb logcat --pid=$(adb shell pidof -s com.example.app)
Crash buffer
崩溃日志缓冲区
adb logcat -b crash
**Priority levels:** V(erbose), D(ebug), I(nfo), W(arn), E(rror), F(atal), S(ilent)adb logcat -b crash
**优先级等级:** V(详细)、D(调试)、I(信息)、W(警告)、E(错误)、F(致命)、S(静默)Common Debug Patterns
常用调试命令组合
bash
undefinedbash
undefinedFind crashes
查找崩溃信息
adb logcat *:E | grep -E "(Exception|Error|FATAL)"
adb logcat *:E | grep -E "(Exception|Error|FATAL)"
Activity lifecycle
查看Activity生命周期
adb logcat ActivityManager:I ActivityTaskManager:I *:S
adb logcat ActivityManager:I ActivityTaskManager:I *:S
Memory issues
内存问题排查
adb logcat art:D dalvikvm:D *:S | grep -i "gc"
undefinedadb logcat art:D dalvikvm:D *:S | grep -i "gc"
undefinedMemory Analysis
内存分析
bash
adb shell dumpsys meminfo com.example.appKey metrics:
- PSS: Proportional memory use (compare apps with this)
- Private Dirty: Memory exclusive to process
- Heap: Java/Native heap usage
bash
adb shell dumpsys meminfo com.example.app关键指标:
- PSS: 比例内存使用量(用于对比不同应用的内存占用)
- Private Dirty: 进程独占的内存
- Heap: Java/Native堆内存使用情况
Crash Analysis
崩溃分析
bash
undefinedbash
undefinedANR traces
ANR追踪信息
adb shell cat /data/anr/traces.txt
adb shell cat /data/anr/traces.txt
Tombstones (native crashes, needs root)
Tombstones(原生崩溃,需Root权限)
adb shell ls /data/tombstones/
adb shell ls /data/tombstones/
Recent crashes via logcat
通过Logcat查看近期崩溃
adb logcat -b crash -d
---adb logcat -b crash -d
---System Inspection
系统检查
dumpsys Services
dumpsys服务
bash
adb shell dumpsys -l # List all servicesbash
adb shell dumpsys -l # 列出所有服务Common services
常用服务
adb shell dumpsys activity # Activities, processes
adb shell dumpsys package <pkg> # Package details
adb shell dumpsys battery # Battery status
adb shell dumpsys meminfo # System memory
adb shell dumpsys cpuinfo # CPU usage
adb shell dumpsys window displays # Display info
adb shell dumpsys connectivity # Network state
undefinedadb shell dumpsys activity # Activity与进程信息
adb shell dumpsys package <pkg> # 应用包详细信息
adb shell dumpsys battery # 电池状态
adb shell dumpsys meminfo # 系统内存信息
adb shell dumpsys cpuinfo # CPU使用情况
adb shell dumpsys window displays # 显示信息
adb shell dumpsys connectivity # 网络状态
undefinedSystem Properties
系统属性
bash
adb shell getprop # All properties
adb shell getprop | grep <filter> # Filter propertiesbash
adb shell getprop # 列出所有系统属性
adb shell getprop | grep <filter> # 过滤系统属性Useful properties
常用属性
getprop ro.product.model # Model
getprop ro.build.fingerprint # Build fingerprint
getprop ro.serialno # Serial number
getprop sys.boot_completed # Boot status (1 = done)
undefinedgetprop ro.product.model # 设备型号
getprop ro.build.fingerprint # 构建指纹
getprop ro.serialno # 设备序列号
getprop sys.boot_completed # 启动状态(1表示已完成)
undefinedSettings
设置管理
bash
undefinedbash
undefinedNamespaces: system, secure, global
命名空间:system、secure、global
adb shell settings get global airplane_mode_on
adb shell settings put system screen_brightness 128
adb shell settings list global
---adb shell settings get global airplane_mode_on
adb shell settings put system screen_brightness 128
adb shell settings list global
---Reboot Commands
重启命令
bash
adb reboot # Normal reboot
adb reboot recovery # Recovery mode
adb reboot bootloader # Fastboot mode
adb reboot sideload # Sideload mode
adb reboot-bootloader # Alias for bootloaderbash
adb reboot # 正常重启
adb reboot recovery # 恢复模式
adb reboot bootloader # Fastboot模式
adb reboot sideload # 侧载模式
adb reboot-bootloader # Fastboot模式别名Troubleshooting
故障排除
Device not found:
bash
adb kill-server && adb start-serverUnauthorized:
- Check USB debugging is enabled
- Revoke USB debugging authorizations in Developer Options, reconnect
Multiple devices error:
- Use to specify device
-s <serial>
Command not found (on device):
- Some commands require root or are version-specific
- Try or check if command exists
/system/bin/<cmd>
设备未找到:
bash
adb kill-server && adb start-server未授权:
- 检查是否已启用USB调试
- 在开发者选项中撤销USB调试授权,重新连接设备
多设备错误:
- 使用 指定目标设备
-s <serial>
设备上命令未找到:
- 部分命令需要Root权限或仅支持特定Android版本
- 尝试使用 或检查命令是否存在
/system/bin/<cmd>
Quick Reference
快速参考
| Task | Command |
|---|---|
| List devices | |
| Install app | |
| Start app | |
| Stop app | |
| Screenshot | |
| Logs | |
| Shell | |
| Push file | |
| Pull file | |
| Tap | |
| Back | |
| Home | |
For deep dives into specific topics, see .
references/deep-dive.md| 任务 | 命令 |
|---|---|
| 列出设备 | |
| 安装应用 | |
| 启动应用 | |
| 停止应用 | |
| 截图 | |
| 查看日志 | |
| 进入Shell | |
| 推送文件 | |
| 拉取文件 | |
| 点击屏幕 | |
| 返回键 | |
| 主页键 | |
如需深入了解特定主题,请查看 。
references/deep-dive.md