Android 打开 USB 调试
打开 Android 系统的开发者模式的通用模式:
- 打开“设置”应用,然后滚动到底部。
- 如果它没有“开发者选项”设置,请点击“关于手机”(或“关于平板电脑”),滚动到底部,然后点击内置版本号7次。
- 返回并点击开发者选项。
- 启用 USB 调试。
不同手机型号打开方式不一样,自行google
安装 adb
Mac OS X
使用 brew 可以很简单的安装
brew cask install android-platform-tools
adb devices
随后就可以使用 adb 命令,不需要在配置各种环境变量
链接设备
启动服务
adb start-server
查看设备列表
adb devices
List of devices attached
emulator-5554 device
关闭服务
adb kill-server
链接设备
当设备没有出现在设备列表中时,我们可以手动链接设备
adb connect <ip>:5555
Android 的 uiautomator 模块默认使用了 5555 端口号
链接成功后,设备会出现在设备列表中
adb devices
List of devices attached
10.0.2.1:5555 device
emulator-5554 device
使用 shell 命令
adb shell
root@x86:/ #
当有一个设备时,默认链接,如果有多个设备,需要指定设备
adb -s 10.0.2.1:5555 shell
root@x86:/ #
如果不想每次都手动指定,可以设置环境变量
export ANDROID_SERIAL="DC886B75"
shell 登陆后即可使用常见的脚本命令,比如 ls
adb shell
root@x86:/ # ls
mnt
lib
...
如果只是执行简单的命令,我们也可以直接在本地执行
adb shell ls
mnt
lib
...
包管理
安装
adb install test.apk
adb install -l test.apk # forward lock application
adb install -r test.apk # replace existing application
adb install -t test.apk # allow test packages
adb install -s test.apk # install application on sdcard
adb install -d test.apk # allow version code downgrade
adb install -p test.apk # partial application install
卸载
adb uninstall com.test.app
adb uninstall -k com.test.app # Keep the data and cache directories around after package removal.
查看包安装列表
adb shell pm list packages
adb shell pm list packages -f # See their associated file.
adb shell pm list packages -d # Filter to only show disabled packages.
adb shell pm list packages -e # Filter to only show enabled packages.
adb shell pm list packages -s # Filter to only show system packages.
adb shell pm list packages -3 # Filter to only show third party packages.
adb shell pm list packages -i # See the installer for the packages.
adb shell pm list packages -u # Also include uninstalled packages.
adb shell pm list packages --user <USER_ID> # The user space to query.
包路径
adb shell pm path com.android.phone
package:/system/priv-app/TeleService/TeleService.apk
清除数据和缓存
adb shell pm clear com.test.abc
进程管理
启动应用
adb shell 进入 Android
am start -n <package-name>/<package-name>.<activity-name>
或者直接运行
adb shell am start -n <package-name>/<package-name>.<activity-name>
一些系统应用打开方式
# 设置
am start -n com.android.settings/com.android.settings.Settings
# 日历
am start -n com.android.calendar/com.android.calendar.LaunchActivity
# 闹钟
am start -n com.android.alarmclock/com.android.alarmclock.AlarmClock
# 音乐和视频
am start -n com.android.music/com.android.music.MusicBrowserActivity
am start -n com.android.music/com.android.music.VideoBrowserActivity
am start -n com.android.music/com.android.music.MediaPlaybackActivity
# 照相机
am start -n com.android.camera/com.android.camera.Camera
# 浏览器
am start -n com.android.browser/com.android.browser.BrowserActivity
# 打开网址
am start -a android.intent.action.VIEW -d http://www.google.cn/
# 拨打电话
am start -a android.intent.action.CALL -d tel:10086
这里有个问题,我们可以通过 pm 命令查到 apk 的包名,但是 activity 就不方便知道了,我们希望只通过包名就能启动应用,从网上查到这个方法
am start -a android.intent.action.MAIN -n <package-name>
但是这种方式,我在模拟器中只可以打开系统应用,第三方应用会报错,再次查找发现可以使用 monkey 命令来启动应用
monkey -p <package-name> -c android.intent.category.LAUNCHER 1
经过测试,这种方式是比较方便打开各种应用
杀掉应用
am force-stop <package-name>
杀掉所有后台进程
am kill-all
更多命令
推送文件到设备
adb push test.apk /data
拉取设备的文件到本地
adb pull /data/test.apk
adb pull /data/test.apk ~/Downloads
查看设备 SSN 号
adb shell getprop | grep ro.boot.serialno




















