android

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Android 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 -l
Output shows serial, status, and device info. Common statuses:
  • device
    - Connected and authorized
  • unauthorized
    - Accept USB debugging prompt on device
  • offline
    - Connection issues, try
    adb kill-server && adb start-server
bash
adb devices -l
输出显示设备序列号、状态及设备信息。常见状态:
  • device
    - 已连接且已授权
  • unauthorized
    - 请在设备上接受USB调试授权提示
  • offline
    - 连接异常,尝试执行
    adb kill-server && adb start-server

Wireless Debugging

无线调试

Android 11+ (Recommended):
  1. Enable Wireless debugging in Developer Options
  2. Tap "Pair device with pairing code"
  3. Run:
    adb pair <ip>:<pairing_port>
    and enter the code
  4. Then:
    adb connect <ip>:<connection_port>
Legacy (requires USB first):
bash
adb tcpip 5555
adb connect <device_ip>:5555
Android 11及以上版本(推荐方式):
  1. 在开发者选项中启用无线调试
  2. 点击“使用配对码配对设备”
  3. 执行:
    adb pair <ip>:<pairing_port>
    并输入配对码
  4. 然后执行:
    adb connect <ip>:<connection_port>
传统方式(需先通过USB连接):
bash
adb tcpip 5555
adb connect <device_ip>:5555

Multiple Devices

多设备场景

Always specify device with
-s
:
bash
adb -s <serial> shell
adb -s emulator-5554 install app.apk

操作时需通过
-s
指定设备:
bash
adb -s <serial> shell
adb -s emulator-5554 install app.apk

Shell Commands

Shell命令

Interactive Shell

交互式Shell

bash
adb shell                    # Enter shell
adb shell <command>          # Run single command
adb shell "cmd1 && cmd2"     # Chain commands
bash
adb shell                    # 进入Shell环境
adb shell <command>          # 执行单个命令
adb shell "cmd1 && cmd2"     # 链式执行多个命令

Essential Commands

常用核心命令

bash
undefined
bash
undefined

Device 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
undefined
ps -A | grep <name> pidof <package_name> top -n 1 -m 10
undefined

Safe 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 data
bash
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
undefined
bash
undefined

Start 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"
adb shell am start -n com.example.app/.Activity
-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
undefined
adb shell pm clear com.example.app
undefined

List 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 info
bash
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 permission

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 permission

File 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/
undefined
adb push local_dir/ /sdcard/target/ adb pull /sdcard/dir/ ./local/
undefined

Access 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
undefined
bash
undefined

Tap 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
undefined
adb 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 # 音量加键
undefined

Common Keycodes

常用按键码

CodeKeyCodeKey
3HOME4BACK
24VOL_UP25VOL_DOWN
26POWER66ENTER
67DEL82MENU
代码按键代码按键
3主页4返回
24音量加25音量减
26电源66回车
67删除82菜单

Screenshots & Recording

截图与录屏

bash
undefined
bash
undefined

Screenshot

截图

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
undefined
adb shell screenrecord /sdcard/video.mp4 adb shell screenrecord --time-limit 10 /sdcard/video.mp4
undefined

UI Hierarchy

UI层级结构

bash
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xml

bash
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xml

Debugging & Logs

调试与日志

Logcat Essentials

Logcat核心用法

bash
undefined
bash
undefined

Dump 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
undefined
bash
undefined

Find 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"
undefined
adb logcat art:D dalvikvm:D *:S | grep -i "gc"
undefined

Memory Analysis

内存分析

bash
adb shell dumpsys meminfo com.example.app
Key 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
undefined
bash
undefined

ANR 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 services
bash
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
undefined
adb 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 # 网络状态
undefined

System Properties

系统属性

bash
adb shell getprop                       # All properties
adb shell getprop | grep <filter>       # Filter properties
bash
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)
undefined
getprop ro.product.model # 设备型号 getprop ro.build.fingerprint # 构建指纹 getprop ro.serialno # 设备序列号 getprop sys.boot_completed # 启动状态(1表示已完成)
undefined

Settings

设置管理

bash
undefined
bash
undefined

Namespaces: 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 bootloader

bash
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-server
Unauthorized:
  • Check USB debugging is enabled
  • Revoke USB debugging authorizations in Developer Options, reconnect
Multiple devices error:
  • Use
    -s <serial>
    to specify device
Command not found (on device):
  • Some commands require root or are version-specific
  • Try
    /system/bin/<cmd>
    or check if command exists

设备未找到:
bash
adb kill-server && adb start-server
未授权:
  • 检查是否已启用USB调试
  • 在开发者选项中撤销USB调试授权,重新连接设备
多设备错误:
  • 使用
    -s <serial>
    指定目标设备
设备上命令未找到:
  • 部分命令需要Root权限或仅支持特定Android版本
  • 尝试使用
    /system/bin/<cmd>
    或检查命令是否存在

Quick Reference

快速参考

TaskCommand
List devices
adb devices -l
Install app
adb install -r -g app.apk
Start app
adb shell monkey -p pkg -c android.intent.category.LAUNCHER 1
Stop app
adb shell am force-stop pkg
Screenshot
adb exec-out screencap -p > screen.png
Logs
adb logcat -d -t 100
Shell
adb shell
Push file
adb push local /sdcard/
Pull file
adb pull /sdcard/file ./
Tap
adb shell input tap X Y
Back
adb shell input keyevent 4
Home
adb shell input keyevent 3
For deep dives into specific topics, see
references/deep-dive.md
.
任务命令
列出设备
adb devices -l
安装应用
adb install -r -g app.apk
启动应用
adb shell monkey -p pkg -c android.intent.category.LAUNCHER 1
停止应用
adb shell am force-stop pkg
截图
adb exec-out screencap -p > screen.png
查看日志
adb logcat -d -t 100
进入Shell
adb shell
推送文件
adb push local /sdcard/
拉取文件
adb pull /sdcard/file ./
点击屏幕
adb shell input tap X Y
返回键
adb shell input keyevent 4
主页键
adb shell input keyevent 3
如需深入了解特定主题,请查看
references/deep-dive.md