[Android] adb 命令总结

调试

adb devices

查看已连接设备

adb root

已root启动adbd进程

adb remount

挂载设备

adb kill-server

杀死adb服务进程,重置adb主机

调用Package Manager

adb shell pm list package [options]

查看设备中安装应用的包名

# 列出所有应用的包名
$ adb shell pm list packages   
# 列出所有包含music的包名
$ adb shell pm list packages music  
# 列出包名和相关文件,如安装路径
$ adb shell pm list packages -f 
# 列出系统应用包名
$ adb shell pm list packages -s 
# 列出三方应用报名
$ adb shell pm list packages -3
# 列出禁用的应用包名
$ adb shell pm list packages -d
# 列出可用的应用包名
$ adb shell pm list packages -e
$ adb shell pm path packagename   列出指定应用包名的apk路径
$ adb shell pm 

adb shell pm path

查看给定apk包名的安装路径

# 查看微信安装路径
$ adb shell pm path com.tencent.mm

adb shell pm clear

清除应用的数据

# 清楚qq数据
$ adb shell pm clear com.tencent.mobileqq

安装卸载应用

adb install [options]

安装应用

# 将本地apk安装到手机
$ adb install /Downloads/app/qq.apk
# 如果存在应用,替换并安装
$ adb install -r /Downloads/app/qq.apk

adb uninstall [options]

卸载指定包名的应用

$ adb uninstall com.tencent.mm
# 卸载应用,但保留数据和缓存
$ adb uninstall -k com.tent.mm

调用 Activity Manager

adb shell am start intent

启动指定Intent的应用

# 启动前强行停止目标应用
$ adb shell am start -S 
# 启动拨号盘
$ adb shell am start com.android.dialer/.app.DialtactsActivity

adb shell am startservice intent

启动指定Intent的Service

adb shell am force-stop package

强行停止与package关联的所有应用

adb shell am broadcast intent

发出Intent广播

adb shll am kill package

结束package关联的所有进程,但不会影响用户体验

adb shell am kill-all

结束所有的后台进程

设置系统属性

adb shell getprop

# 获取所有的属性
$ adb shell getprop 
# 获取指定属性
$ adb shell getprop 
1

adb shell setprop

# 设置属性,必须先root
adb shell setprop  

屏幕相关

adb shell screencap

# 截屏并保存
$ adb shell screencap /storage/emulated/0/1.png

adb shell screenrecord

# 录屏并保存
$ adb shell screenrecord /storage/emulated/0/demo.mp4

抓取log相关

adb logcat [options] [filtersspecs]

filterspecs 格式为 [:priority]
priority有:
  V    Verbose 及以上(设置tag后的默认优先级)
  D    Debug 及以上 (没有tag时的默认优先级)
  I    Info 以及上
  W    Warn 及以上
  E    Error 及以上
  F    Fatal 及以上
  S    Silent (最高优先级,不打印任何输出)

adb logcat -s

过滤对应tag的log

# 查找PowerManagerService的Debug及以上log
$ adb logcat -s "PowerManagerService":D 
# 过滤所有PMS的log
$ adb logcat -s "PowerManagerService"

adb logcat -b

输出缓冲区log,buffer可选值有’main’,’system’,’radio’,’events’,’crash’,’default’或’all’,允许多个值用逗号分割。

# 查看事件相关消息的缓冲区
$ adb logcat -b events、
# -b 默认参数
$ adb logcat -b main
# 查看包含无线电相关消息的缓冲区
$ adb logcat -b radio

adb logcat -v

格式化输出log

# 输出日期、时间、优先级/标记,以及发出消息的进程的PID
adb logcat -v time -s "xxx"
# 输出日期、时间、优先级,标记,以及发出消息的线程的PID和TID
adb logcat -v threadtime 

其他

adb logcat -c   清空整个log
adb logcat -B   二进制输出log
adb logcat > test.log  输出到对应文件

dump系统数据

adb shell dumpsys

打印系统数据,对打印的结果可以使用grep进行过滤:

# 打印PowerManagerService中Power数据,过滤含有Battery字段的信息
$ adb shell dumpsys power | grep Battery
# 打印电池数据
$ adb shell dumpsys battery
# 打印电池统计数据
$ adb shell dumpsys batterystats
# 打印设备处于doze模式的数据
$ adb shell dumpsys deviceidle
# 打印activityManagerService中的数据
$ adb shell dumpsys activity

adb shell dumpstats

打印系统状态

$ adb shell dumpstats

文件管理

adb pull 

将手机中的数据拉取到电脑

$ adb pull /data/data/com.tencent.mm /Downloads/myfolder

adb push

将本地数据上传到手机中

$ adb push framework.jar /system/framework

其他

执行adb shell后,进入设备目录后,创建文件、查看属性等都和linux命令相同,这里列举一两个:

# 查看文件目录
$ adb shell ls
# 创建文件
$ adb shell touch 
# 查看文件路径
$ adb shell pwd

参考资料:
adb command
Android developer

你可能感兴趣的:(Android系统开发)