关于 android
android 是一项使用 ADB 和 shell 命令操作及检查 Android 设备的参考技能,面向开发者和测试人员。内容涵盖设备连接、应用管理、界面输入、日志、系统检查与故障排查。
主要功能
- 01USB 与无线设备连接
- 02应用安装和软件包管理
- 03文件传输与应用数据访问
- 04界面输入、截图和层级导出
- 05日志、崩溃与系统检查
- 06ADB 故障排查指南
适用场景
- 01在设备上安装并启动 APK
- 02自动执行点击并截取屏幕
- 03检查应用日志和崩溃报告
- 04查看设备服务、内存或设置
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ hyperb1iss/ hyperdroid-skill --skill android安装后在 Claude Code 中输入/android确认已加载。
附带文件references/deep-dive.md
详细介绍
Android 设备精通
本技能涵盖通过 ADB 和 shell 命令与 Android 设备交互的所有内容。
设备连接
检查已连接的设备
adb devices -l
输出会显示序列号、状态和设备信息。常见状态:
device- 已连接并获授权unauthorized- 请在设备上接受 USB 调试提示offline- 连接存在问题,请尝试adb kill-server && adb start-server
无线调试
Android 11+(推荐):
- 在开发者选项中启用无线调试
- 点按“使用配对码配对设备”
- 运行:
adb pair <ip>:<pairing_port>并输入配对码 - 然后运行:
adb connect <ip>:<connection_port>
旧版方式(需要先连接 USB):
adb tcpip 5555
adb connect <device_ip>:5555
多台设备
始终使用 -s 指定设备:
adb -s <serial> shell
adb -s emulator-5554 install app.apk
Shell 命令
交互式 Shell
adb shell # Enter shell
adb shell <command> # Run single command
adb shell "cmd1 && cmd2" # Chain commands
基本命令
# Device info
getprop ro.product.model # Device model
getprop ro.build.version.release # Android version
getprop ro.build.version.sdk # SDK level
# File operations
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
安全限制输出
对于可能产生大量输出的命令:
adb shell "logcat -d | head -500"
adb shell "dumpsys activity | head -200"
应用管理
安装/卸载
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
启动/停止应用
# Start main activity
adb shell monkey -p com.example.app -c android.intent.category.LAUNCHER 1
# Start specific activity
adb shell am start -n com.example.app/.MainActivity
# Start with intent extras
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
# Clear app data
adb shell pm clear com.example.app
列出软件包
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
权限
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
文件操作
推送/拉取文件
adb push local_file.txt /sdcard/
adb pull /sdcard/remote_file.txt ./
# Recursive
adb push local_dir/ /sdcard/target/
adb pull /sdcard/dir/ ./local/
访问应用数据(可调试应用)
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 自动化
输入命令
# Tap at coordinates
adb shell input tap 500 800
# Swipe (x1 y1 x2 y2 [duration_ms])
adb shell input swipe 500 1500 500 500 300
# Text input (needs focused field)
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
常用按键码
| 代码 | 按键 | 代码 | 按键 |
|---|---|---|---|
| 3 | HOME | 4 | BACK |
| 24 | VOL_UP | 25 | VOL_DOWN |
| 26 | POWER | 66 | ENTER |
| 67 | DEL | 82 | MENU |
截图和录屏
# Screenshot
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png
# One-liner (binary-safe)
adb exec-out screencap -p > screen.png
# Screen recording (max 3 min)
adb shell screenrecord /sdcard/video.mp4
adb shell screenrecord --time-limit 10 /sdcard/video.mp4
UI 层级结构
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xml
调试和日志
Logcat 基础
# Dump and exit
adb logcat -d
# Last N lines
adb logcat -t 100
# Filter by tag:priority
adb logcat ActivityManager:I *:S
# Filter by package (get PID first)
adb logcat --pid=$(adb shell pidof -s com.example.app)
# Crash buffer
adb logcat -b crash
优先级: V(Verbose,详细)、D(Debug,调试)、I(Info,信息)、W(Warn,警告)、E(Error,错误)、F(Fatal,致命)、S(Silent,静默)
常见调试模式
# Find crashes
adb logcat *:E | grep -E "(Exception|Error|FATAL)"
# Activity lifecycle
adb logcat ActivityManager:I ActivityTaskManager:I *:S
# Memory issues
adb logcat art:D dalvikvm:D *:S | grep -i "gc"
内存分析
adb shell dumpsys meminfo com.example.app
关键指标:
- PSS:按比例分摊的内存用量(可用它比较应用)
- Private Dirty:进程独占的内存
- Heap:Java/Native 堆用量
崩溃分析
# ANR traces
adb shell cat /data/anr/traces.txt
# Tombstones (native crashes, needs root)
adb shell ls /data/tombstones/
# Recent crashes via logcat
adb logcat -b crash -d
系统检查
dumpsys 服务
adb shell dumpsys -l # List all services
# 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
系统属性
adb shell getprop # All properties
adb shell getprop | grep <filter> # Filter properties
# 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)
设置
# Namespaces: 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 reboot # Normal reboot
adb reboot recovery # Recovery mode
adb reboot bootloader # Fastboot mode
adb reboot sideload # Sideload mode
adb reboot-bootloader # Alias for bootloader
故障排除
未找到设备:
adb kill-server && adb start-server
未授权:
- 检查是否已启用 USB 调试
- 在开发者选项中撤销 USB 调试授权,然后重新连接
多台设备错误:
- 使用
-s <serial>指定设备
找不到命令(在设备上):
- 某些命令需要 root 权限,或仅适用于特定版本
- 尝试使用
/system/bin/<cmd>,或检查命令是否存在
快速参考
| 任务 | 命令 |
|---|---|
| 列出设备 | 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。
评论
支持 Markdown