【汇总】android adb 命令功能大全(点击跳转原文)
【汇总】android adb 命令功能大全 (点击跳转原文)
前言全局说明
adb 命令是 Android 官方提供,调试 Android 系统的工具。
adb 全称为 Android Debug Bridge(Android 调试桥),是 Android SDK 中提供的用于管理 Android 模拟器或真机的工具。
adb 是一种功能强大的命令行工具,可让 PC 端与 Android 设备进行通信。adb 命令可执行各种设备操作,例如安装和调试应用,并提供对 Unix shell 的访问权限。官方使用说明: https://adbshell.com/commands
官方下载说明;http://adbshell.com/downloads
一、基础命令
1 | adb reboot #重启 |
二、查看设备
1 | adb devices #查看可连接操作的设备 |
三、连接设备
1 | adb [-d|-e|-s <serialNumber>] <command> |
示例:
1 | adb connect 127 0.0.1:7555 # 以 WLAN 网络方式连接(比如:连接电脑上模拟器 MUMU 等) |
四、安装、卸载APP应用
1.安装应用
1 | adb install test.apk #安装应用 |
2.卸载应用
1 | adb uninstall cn.com.test.mobile #卸载应用,需要指定包 |
-k: 卸载 APP 但保留数据和缓存文件
五、adb shell
5-1.adb shell
command 用途:
start [options]启动 指定的 Activity
startservice [options]启动 指定的 Service
broadcast [options]发送 指定的广播
force-stop停止 相关的进程
参数很灵活,和写 Android 程序时代码里的 Intent 相对应,用于决定 intent 对象的选项如下:
-a指定 action,如android.intent.action.VIEW
-c指定 category,如android.intent.category.APP_CONTACTS
-n指定完整 component 名,用于明确指定启动哪个 Activity,
如: com.example.app/.ExampleActivity
5-1-1.adb shell am start
在Android中,除了从界面上启动程序之外,还可以从命令行启动程序,使用的是命令行工具am.
adb shell am start [options]
adb shell am start -n {包(package)名}/{包名}.
1 | start an Activity: am start [-D] <INTENT> |
start 相关说明:https://blog.csdn.net/yegshun/article/details/80982404
5-1-2.adb shell am startservice
adb shell am startservice [options]
示例: 表示调起微信的某 Service 界面。
1 | adb shell am startservice -n com.tencent.mm/com.tencent.mm.plugin.accountsync.model.AccountAuthenticatorService |
5-1-3.adb shell am broadcast
发送广播, 可以向所有组件广播,也可以只向指定组件广播。
adb shell am broadcast [options]
adb shell am broadcast -a[-d ] [-t ] [-c [-c ] …] -a参数指定广播的Action
-d参数指定广播的Data URI
-t参数指定广播的MIME类型
-c参数指定广播的Category
示例:
1 | #向所有组件广播 BOOT_COMPLETED (开机广播) |
系统预定义的广播(文字、图片版):
更多详细:https://www.cnblogs.com/wutou/p/17940047
5-1-4.adb shell am force-stop
1 | adb shell am force-stop cn.com.test.mobile #强制停止应用 |
5-2.adb shell pm
5-2-1 adb shell pm list
1 | adb shell pm list packages #列出手机装的所有app 的包名 |
5-2-2 adb shell pm clear
1 | adb shell pm clear cn.com.test.mobile #清除应用数据与缓存 |
5-3.adb shell dumpsys
Dumpsys用户系统诊断,它运行在设备上,并提供系统服务状态信息
更详细用法:https://www.cnblogs.com/xiaowenshu/p/10390246.html
5-3-1.adb shell dumpsys [options]
1 | adb shell dumpsys package #包信息Package Information |
示例:
adb shell dumpsys package com.tencent.mm 显示微信包全部信息
adb shell dumpsys package com.jingdong.app.mall | findstr version ## 查看‘京东’安装包版本
5-3-2.adb shell dumpsys activity
1 | adb shell dumpsys activity services [<packagename>] #查看正在运行的Services |
参数不是必须的,指定 表示查看与某个包名相关的Services,不指定表示查看所有Services。
不一定要给出完整的包名,比如运行adb shell dumpsys activity services org.zhihu,那么包名org.zhihu.demo1、org.zhihu.demo2 和org.zhihu 等相关的Services 都会列出来。
5-3-3.adb shell dumpsys iphonesubinfo
1 | adb shell dumpsys iphonesubinfo #IMEI( Android 5.0 及以下有效) |
5-3-4.adb shell dumpsys battery
1 | adb shell dumpsys battery #显示电磁信息 |
因为在CMD 终端下执行命令 | 会被解析,所以把 “dumpsys battery | grep powered” 用双引号括起来
5-4.adb shell getprop/setprop
5-4-1.adb shell getprop
列出所有配置属性值
1 | adb shell getprop ro.product.model #查看设备型号 |
查看设备信息
1 | adb shell getprop ro.product.brand #设备品牌 |
5-4-2.adb shell setprop
setprop [key] [value] 设置指定key的属性值
1 | setprop service.adb.tcp.port 5555 |
5-5.adb shell top
使用方法:
1 | top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ] |
参数如下:
-m num 最多显示多少个进程
-n num 刷新多少次后退出
-d num 刷新时间间隔(单位秒,默认值 5)
-s col 按某列排序(可用 col 值:cpu, vss, rss, thr)
-t 显示线程信息
-h 显示帮助文档
示例:
1 | adb shell top -s 10 #查看占用内存前10 的app |
5-6.adb shell wm
5-6-1.adb shell wm size
1 | adb shell wm size #查看屏幕分辨率 |
5-6-2.adb shell wm density
1 | adb shell wm density #查看屏幕密度 |
5-6-3.adb shell wm overscan
显示区域
1 | adb shell wm overscan # 四个数字分别表示距离左、上、右、下边缘的留白像素,以上命令表示将屏幕底部 100px留白 |
5-7.adb shell input
5-7-1.adb shell input keyevent
adb shell input keyevent
1 | adb shell input keyevent 3 #HOME 键 |
keycode位操作参数,不同的 keycode 能实现不同的功能
完整的功能见:https://developer.android.com/reference/android/view/KeyEvent.html
keycode 能实现不同的功能,对应的编码如下:
5-7-2.adb shell input swipe
1 | adb shell input swipe 300 1000 300 500 #滑动解锁,向上滑动手势解锁 |
参数: 300 1000 300 500 分别表示起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标
5-7-3.adb shell input text
1 | adb shell input text hello #焦点处于某文本框时输入文本hello |
5-8.adb shell dmesg
1 | adb shell dmesg #查看内核日志 |
5-9.adb shell settings
更详细说明:https://www.cnblogs.com/wutou/p/17945015
5-9-1.adb shell settings get
adb shell settings get secure android_id #android_id
1 | adb shell settings get system screen_off_timeout #获取屏幕休眠时间 |
5-9-2.adb shell settings put
5-9-2-1.adb shell settings put system
1 | adb shell settings put system screen_brightness 150 #更改亮度值(亮度值在0—255之间) |
user_rotation后取值范围为0,1,2,3
5-9-2-2.adb shell settings put secure
1 | adb shell settings put secure install_non_market_apps1 #允许安装来源 |
5-9-2-3.adb shell settings put global
adb shell settings put global policy_control
可由如下几种键及其对应的值组成,格式为 = : =
key 键对应的值:
这些键对应的值可则如下值用逗号组合:
示例:
1 | # 设置在所有界面下都同时隐藏状态栏和导航栏 |
设置后的配置文件路径:
老版本,文件路径:/data/system/users/0/settings_global.xml
文件路径:/data/misc/apexdata/com.android.wifi/MuMuWifiConfigStore.xml
https://www.cnblogs.com/wutou/p/18175319
飞行模式开启后,就可以发送广播,来更改飞行模式的状态:
1 | adb shell settings put global airplane_mode_on 1 |
飞行模式关闭:
1 | adb shell settings put global airplane_mode_on 0 |
动画缩放
1 | adb shell settings put global window_animation_scale 0 #关闭窗口动画缩放 |
5-9-3.adb shell settings delete
示例:
1 | adb shell settings delete global http_proxy #取消代理 |
5-9-4.adb shell settings list
5-9-4-1.adb shell settings list system
系统设置
示例:
1 | adb shell "settings list system | grep timeout" #获取自动锁屏时间 |
5-9-4-2.adb shell settings list secure
安全设置
示例:
1 |
5-9-4-3.adb shell settings list global
全局设置
示例:
1 |
5-9-5.adb shell settings reset
5-9-5.adb shell settings reset global
示例:
1 | adb shell settings reset global http_proxy #代理恢复默认(取消代理) |
5-10.adb shell ifconfig
获取 IP 地址
1 | adb shell ifconfig | find “Mask” |
5-11.adb shell $(运行Linux命令)
1 | adb shell "cat /sys/class/net/wlan0/address" # 获取 Mac 地址,设备不同可能地址不同 |
5-12.adb shell screencap
5-12-1.老版本截屏
1 | adb shell screencap -p /sdcard/img.png #老版本截图先保存在设备端 |
screencap 参数:
-p 指定保存文件为 png 格式
-d display-id 指定截图的显示屏编号(有多显示屏的情况下)
5-12-2.新版本截屏
1 | adb exec-out screencap -p > img.png #老版本无exec-out命令,只适合于新版的截图 |
更详细,查看第八项
5-13.adb shell screenrecord
录制屏幕
1 | adb shell screenrecord /sdcard/filename.mp4 |
screenrecord 参数:
—size WIDTHxHEIGHT 视频的尺寸,比如 1280x720,默认是屏幕分辨率。
—bit-rate RATE 视频的比特率,默认是 4Mbps。
—time-limit TIME 录制时长,单位秒。默认录制时间和最长录制时间都是180s。
—verbose 输出更多信息。
5-14.检测设备是否已 root
1 | adb shell |
此时命令行提示符是 $ 则表示没有 root 权限,是 # 则表示已 root。
5-15.adb shell monkey
Monkey 可以生成伪随机用户事件来模拟单击、触摸、手势等操作,可以对正在开发中的程序进行随机压力测试。
1 | adb shell monkey -p <packagename> -v 500 # 向 <packagename> 指定的应用程序发送 500 个伪随机事件 |
monkey 官方文档 https://developer.android.com/studio/test/monkey.html
5-16.adb shell svc
5-16-1.adb shell svc wifi
1 | adb root |
5-17.adb shell ps
1 | adb shell ps #查看进程 |
5-18.adb shell -c
-c :不进入终端的命令行执行命令
1 | adb shell su -c setprop service.adb.tcp.port 5555 #以root用户执行,添加端口的命令 |
su :获取root权限
-c setprop service.adb.tcp.port 5555 :获取root命令后,执行向/system/build.prop文件中添加端口
5-19.adb shell getevent
输出所有event设备的基本信息
1 | adb shell getevent --help #帮助 |
输出格式:设备名 事件type 事件code 事件value
getevent输出的是16进制,sendevent使用的是10进制,注意转换。
5-20.adb shell sendevent
设置event设备的基本信息
1 | adb shell sendevent --help #帮助 |
5-21.adb shell service
服务
1 | adb shell service call phone 3 #挂断电话 |
5-22.adb shell setenforce
selinux 使用 setenforce 命令进行设置
1 | adb shell setenforce 0 #设置成permissive 模式 |
设置SELinux 成为permissive模式 临时关闭selinux的。在eng/userdebug版本中
注意此方法重启后失效
5-23.adb shell content
旋转屏幕
1 | adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0 #禁用自动旋转 |
i后面跟的不同的值代表旋转为不同的方向,取值范围为0,1,2,3
需要root权限
5-24.adb shell ip addr show
显示网络ip地址
1 | adb shell ip addr show # 显示网络IP |
5-25.adb shell recovery
恢复出厂设置的操作
注意:这是个危险的命令,不要在手机里操作,除非你知道在干什么
注意:这是个危险的命令,不要在手机里操作,除非你知道在干什么
注意:这是个危险的命令,不要在手机里操作,除非你知道在干什么
1 | adb shell recovery --wipe_data #恢复出厂设置的操作 |
5-26. adb shell 重定向shell脚本文件名
1 | adb shell < shell_script.sh |
shell_script.sh 里写的是shell命令,这样多条命令或shell就可以直接执行了
5-*.
1 |
5-*.
1 |
六、上传、下载文件
1 | adb push <local> <remote> #从本地复制文件到设备 |
七、adb logcat
adb logcat [
7-1.adb logcat
1 | adb logcat #查看日志 |
7-2.adb logcat -c
1 | adb logcat -c #清除log 缓存 |
7-3.adb logcat -v
1 | adb logcat -v <format> 指定日志输出格式 |
7-4.adb logcat :
按某级别过滤日志则会将该级别及以上的日志输出,Android 日志的优先级如下:
V —— Verbose(最低,过滤少,输出得最多)
D —— Debug
I —— Info
W —— Warning
E —— Error
F —— Fatal
S —— Silent(最高,过滤多,啥也不输出)
示例:输出W之上的日志,W,E,F,S
1 | adb logcat *:W |
按 tag 和级别过滤日志:
示例:
输出 tag ActivityManager 的 I 以上级别日志,输出tag MyApp的D 以上级别日志,及其它 tag 的 S 级别日志(即屏蔽其它 tag 日志)。
1 | adb logcat ActivityManager:I MyApp:D *:S |
7-5: 获取已安装应用Activity类名
旧版:
1 | adb logcat ActivityManager:I *:s | findstr "cmp" |
新版:
1 | adb logcat ActivityManager:I *:s | findstr "pre-top-activity" |
下图{}花括号里就是类名
类名启动设置:
1 | adb shell am start -n com.android.settings/com.android.settings.Settings |
类名关闭设置:
1 | adb shell am force-stop com.android.settings |
八、adb get-serialno
1 | adb get-serialno #获取序列号 |
九、adb bugreport
1 | adb bugreport #查看bug 报告 |
十、adb exec-out
执行命令并输出到本地。
10-1.adb exec-out screencap
1 | adb exec-out screencap -p > img.png # 老版本无exec-out命令,只适合于新版的截图 |
十一、adb 自身管理
11-1. 启动停止服务
1 | adb start-server #启动adb 服务,基本不会用到,因为只要设备连接正确,会自动启动adb server |
11-2.系统更新
11-2-1.重启到 Recovery 模式
1 | adb reboot recovery |
在设备的 Recovery 界面上操作进入 Apply update-Apply from ADB。
注:不同的 Recovery 菜单可能与此有差异,有的是一级菜单就有 Apply update from ADB。
11-2-2.重启到 Fastboot 模式
1 | adb reboot bootloader |
11-2-3.通过 adb 上传和更新系统。
1 | adb sideload <path-to-update.zip> |
十二、命令行切换到root权限
12-1.方式一:
1 | adb comnnect IP:端口 |
12-2.方式二:
1 | adb comnnect IP:端口 |
12-3.方式三:
1 | adb comnnect IP:端口 |
十三、连接
13.1 adb 等待设备连接:
1 | #adb wait-for-device |
此时命令行提示符是 $ 则表示没有 root 权限,是 # 则表示已 root。
免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。
参考、来源:
https://blog.csdn.net/y281252548/article/details/130619040
https://zhuanlan.zhihu.com/p/605831031 (adb 详细介绍)
https://blog.csdn.net/zh6526157/article/details/129583527 (adb shell am broadcast 广播介绍)
https://blog.51cto.com/u_16213586/8478014 (adb shell -c 不登录在终端执行命令)
https://blog.csdn.net/mazhidong/article/details/79412073
http://www.taodudu.cc/news/show-6122139.html?action=onClick
https://www.jianshu.com/p/11a201e705ac
https://blog.csdn.net/userwyh/article/details/82430665
https://www.jianshu.com/p/bae933aac556
https://blog.csdn.net/fxdaniel/article/details/45846333
https://blog.csdn.net/zh6526157/article/details/132495569
https://sspai.com/post/33696
https://blog.51cto.com/u_16087831/6223245
https://blog.csdn.net/luckywang1103/article/details/76804856
https://baijiahao.baidu.com/s?id=1756775420151725732&wfr=spider&for=pc
https://blog.51cto.com/u_16175448/6833996
https://cloud.tencent.com/developer/article/1360063
https://blog.csdn.net/zh6526157/article/details/134440688 (发送一个广播来更改飞行模式的状态)
https://blog.51cto.com/u_16213430/8410476
https://www.codenong.com/25864385/
https://www.jianshu.com/p/8a52eb8771e5
《Python3网络爬虫开发实战 第2版》崔庆才 P667
https://mumu.163.com/help/20230504/35047_1086360.html#a7 (查看设备信息)
https://doc.miyun.app/android-emulator-proxy
https://mumu.163.com/help/20210513/35047_947512.html (显示当前UI窗口名、获取已安装应用Activity类名)
https://www.cnblogs.com/dengqing9393/p/7210479.html (am说明)
https://www.cnblogs.com/dengqing9393/p/7210479.html (adb 打开浏览器)
https://www.cnblogs.com/greatverve/archive/2012/02/10/android-am.html (adb 打开浏览器)
https://www.52pojie.cn/thread-1505329-1-1.html (https_proxy)
https://www.52pojie.cn/thread-1600980-1-1.html (cmd 重定向shell脚本执行)
https://blog.csdn.net/liaochaoyun/article/details/122085681 (adb devices)