Android 调试桥(adb)的详细使用大家可以看官方教程。
一、常用adb命令:
查看设备
adb devices
通过 IP 地址连接到设备
adb connect your_ip:5555
断开设备连接
adb disconnect your_ip:5555
安装apk
adb install -t -r path_to_apk
常用的options:
-
-r
:重新安装现有应用,并保留其数据。 -
-t
:允许安装测试 APK。仅当您运行或调试了应用或者使用了 Android Studio 的 Build > Build APK 命令时,Gradle 才会生成测试 APK。如果是使用开发者预览版 SDK 构建的 APK,那么安装测试 APK 时必须在install
命令中包含-t
选项。
- 卸载apk
adb uninstall [options] 包名
-
-k
:可选参数-k的作用为卸载软件但是保留配置和缓存文件。
启动adb服务
adb start-server
关闭adb服务
adb kill-server
进入shell命令可以进入设备的shell环境,如需退出交互式 shell,请按 Control+D 或输入 exit。
adb shell
二、am命令
在 adb shell 中,您可以使用 ActivityManager (am) 工具发出命令以执行各种系统操作,如启动 activity、强行停止进程、广播 intent、修改设备屏幕属性,等等。
//am help
<INTENT> specifications include these flags and arguments:
[-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-n <COMPONENT_NAME>]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--esn <EXTRA_KEY> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
[--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> ...]
[--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
[--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE>]
[--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
(mutiple extras passed as Integer[])
[--eial <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
(mutiple extras passed as List<Integer>)
[--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
(mutiple extras passed as Long[])
[--elal <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
(mutiple extras passed as List<Long>)
[--efa <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
(mutiple extras passed as Float[])
[--efal <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
(mutiple extras passed as List<Float>)
[--esa <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
(mutiple extras passed as String[]; to embed a comma into a string,
escape it using "\,")
[--esal <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
(mutiple extras passed as List<String>; to embed a comma into a string,
escape it using "\,")
[-f <FLAG>]
[--grant-read-uri-permission] [--grant-write-uri-permission]
[--grant-persistable-uri-permission] [--grant-prefix-uri-permission]
[--debug-log-resolution] [--exclude-stopped-packages]
[--include-stopped-packages]
[--activity-brought-to-front] [--activity-clear-top]
[--activity-clear-when-task-reset] [--activity-exclude-from-recents]
[--activity-launched-from-history] [--activity-multiple-task]
[--activity-no-animation] [--activity-no-history]
[--activity-no-user-action] [--activity-previous-is-top]
[--activity-reorder-to-front] [--activity-reset-task-if-needed]
[--activity-single-top] [--activity-clear-task]
[--activity-task-on-home] [--activity-match-external]
[--receiver-registered-only] [--receiver-replace-pending]
[--receiver-foreground] [--receiver-no-abort]
[--receiver-include-background]
[--selector]
[<URI> | <PACKAGE> | <COMPONENT>]
- 启动activity
am start -n 包名/Activity类名
am start -n com.example.com/com.example.com.MyActivity
- -D // 调试模式 debugging---- 参考 android studio 点击调试模式(debugging install)
- -N // 启动本地调试 native debugging
- -W // 等带launch启动完毕
--start-profiler <FILE> // 会启动profiler 分析器 FILE 为分析文件
--sampling <INTERVAL> //使用间隔为微秒的样本分析,通常和start-profiler 一起使用 <INTERVAL>这个是微妙值,直接输入数字就可以了
--streaming //将分析输出到文件 ,配合start-profiler 一起使用 - -P <FILE> //和上面streaming功能一样,但是当应用程序空闲时,分析就会停止。
- -R //设置重复启动的次数。每次重复启动,都会将上一次的启动进行finished 关闭。
- -S //在开始活动之前强制停止目标应用程序
- --track-allocation //启用跟踪对象分配
- --user <USER_ID> | current //指定作为哪个用户运行;如果不是指定,作为当前用户运行。 设置current 就是将当前用户设置为运行账户,在当前用户id下运行。
- --stack <STACK_ID> //设置在哪个堆栈之中启动, statc_id 需要通过相关指令去进行查询。
- 启动service
am start -n 包名/服务类名
am startservice -n com.example.servicetest/com.example.servicetest.MyService
- 启动前台service
am startforegroundservice -n com.example.servicetest/com.example.servicetest.MyService
- 关闭service
am stopservice com.inspur.istv/com.inspur.kotlin.activity.BookService
- 发送广播
am broadcast -a <ACTION> --es <EXTRA_KEY> <EXTRA_STRING_VALUE>
am broadcast -a android.net.conn.CONNECTIVITY_CHANGE
am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true
//value分别为String类型,int类型,boolean类型
三、pm命令
在 adb shell 中,您可以使用软件包管理器 (pm) 工具发出命令,以对设备上安装的应用软件包执行操作和查询。
- 列出设备所有应用包名
pm list packages [options]
* -f: 显示包名所关联的文件;
* -d: 只显示disabled包名;
* -e: 只显示enabled包名;
* -s: 只显示系统包名;
* -3: 只显示第3方应用的包名;
* -i: 包名所相应的installer;
* -u: 包含uninstalled包名.
- 获取指定包名的apk路径
pm path PACKAGE_NAME
- 输出apk信息
pm dump PACKAGE_NAME
- 权限查询
pm list permissions [options]
-g:按组进行列出权限
-f:打印所有信息
-s:简短的摘要
-d:只有危险的权限列表
-u:只有权限的用户将看到列表
- 安装apk
pm install -r /data/local/tmp/test.apk
-l:锁定应用程序
-r:重新安装应用,且保留应用数据
-t:允许测试apk被安装
-i <INSTALLER_PACKAGE_NAME>:指定安装包的包名
-s:安装到sd卡
-f:安装到系统内置存储中(默认安装位置)
-d:允许降级安装(同一应用低级换高级)
-g:授予应用程序清单中列出的所有权限(只有6.0系统可用)
- 卸载apk
pm uninstall /data/local/tmp/test.apk
- 清除应用数据
pm clear <PACKAGE_NAME>
- 启用应用
pm enable <PACKAGE_NAME>
- 禁用应用
pm disable <PACKAGE_NAME>
四、dumpsys命令
dumpsys
是一种在 Android 设备上运行的工具,可提供有关系统服务的信息。可以使用 Android 调试桥 (adb) 从命令行调用 dumpsys
,获取在连接的设备上运行的所有系统服务的诊断输出。
- 查询系统支持的所有服务信息
adb shell dumpsys -l
- 查询指定服务信息
adb shell dumpsys window
- activity
dumpsys activity [options] [cmd]
//options
-a:包括所有可用的服务器状态
-c:包括客户端状态
-p:限制输出为给定的包,例如 dumpsys activity -p com.android.browser
-h:打印帮助信息
//cmd
a[ctivities]:activity堆栈状态
r[recents]:最近activity的状态
b[rodacasts] [package_name] [histpry [-s]]:广播状态
i[ntents] [package_name]:挂起的intent状态
p[rocesses] [package_name]:进程状态
o[om]:oom管理
perm[issions]:url权限授权状态
prov[iders] [comp_spec…]:content provider状态
provider [comp_spec]:provider客户端状态
s[ervices] [comp_spec…]:服务状态
as[sociations]:跟踪应用程序的关联
service [comp_spec]:服务客户端状态
package [package_name]:给的包的所有状态
all:转储所有的activityes
top:转储栈顶的activity
write:写入所有挂起状态存储
track-associations:允许会话跟踪
untrack-associations:禁用和明确会话跟踪,命令参数可能也是一个comp_spec 转储的activity
- Window
#dumpsys window命令帮助信息
dumpsys window -h
#查看上一次ANR信息()
dumpsys window l
#查看Window策略状态
dumpsys window p
#查看Window的动画状态
dumpsys window a
#查看Window的活动会话
dumpsys window s
#查看Window的活跃的显示内容
dumpsys window d
#查看Window的token列表
dumpsys window t
#查看Window列表
dumpsys window w
#获取当前活动的包名和类名
dumpsys window | grep mCurrentFocus
例如:查看当前界面的activity
//获取Activity信息过滤出最后一条记录,也就是当前界面(顶层top)activity
dumpsys activity top | grep ACTIVITY | tail -n 1
//获取当前activity
dumpsys window w | grep name=
例如:查看当前界面的Fragment
//查看Fragment栈信息
dumpsys activity top | grep '#0: '
//查看当前界面的Fragment
dumpsys activity top | grep '#0: ' | tail -n 1
五、input命令
adb shell input keyevent KEYCODE_SLEEP //休眠
adb shell input keyevent KEYCODE_POWER //唤醒、也是电源键的意思
六、content命令
Uri介绍
ContentProvider 中Uri代表了要操作的数据。一个Uri由以下几部分组成:
- scheme :由Android所规定scheme为:content://。
- authority :用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。
- 路径(path):可以用来表示我们要操作的数据,路径的构建应根据业务而定。
例如:content://mobile.android.data/product/1
其中authority是mobile.android.data,path是product/1。意思是:操作mobile.android.data中product表中id为1的数据。
命令使用
content对数据库(uri)进行query,insert,update,delete等操作。
帮助文档:adb shell content -h
1、查询(query)
--uri:要操作的内容URI。
--projection:查询内容
--where:条件
--sort:排序
Example:
# Select "name" and "value" columns from secure settings where "name" is equal to "new_setting" and sort the result by name in ascending order.
adb shell content query --uri content://settings/secure --projection name:value --where "name='new_setting'" --sort "name ASC"
2、插入(insert)
--uri:要操作的内容URI。
--bind:要插入的数据name和value。其中数据类型有b - boolean, s - string, i - integer, l - long, f - float, d - double。
Example:
# Add "new_setting" secure setting with value "new_value".
adb shell content insert --uri content://settings/secure --bind name:s:new_setting --bind value:s:new_value
3、更新(update)
--uri:要操作的内容URI。
--bind:要更新的value
--where: 条件
Example:
# Add "new_setting" secure setting with value "new_value".
adb shell content insert --uri content://settings/secure --bind name:s:new_setting --bind value:s:new_value
4、删除(delete)
--uri:要操作的内容URI。
--where:条件
Example:
# Remove "new_setting" secure setting.
adb shell content delete --uri content://settings/secure --where "name='new_setting'"
5、调用方法(call)
--uri:要操作的内容URI。
--method:方法
adb shell content call --uri content://com.example.provider/items --method com.example.provider.METHOD_NAME
6、读取数据(read)
--uri:要操作的内容URI。
Example:
adb shell 'content read --uri content://settings/system/ringtone_cache' > host.ogg
7、写入数据(write)
--uri:要操作的内容URI。
Example:
adb shell 'content write --uri content://settings/system/ringtone_cache' < host.ogg
8、获取内容URI的MIME类型(gettype)
--uri:要操作的内容URI。
Example:
adb shell content gettype --uri content://media/internal/audio/media/
注意:在执行这些操作时,你可能需要有适当的权限,并且设备必须已经root或者处于开发者模式。
七、获取IP命令
adb shell ip addr show
adb shell ifconfig
adb shell getprop
getprop命令的作用就是从系统的各种配置文件中读取一些设备的信息。
一些参数说明:
dalvik.vm.heapgrowthlimit:默认给进程分配的可使用堆内存
dalvik.vm.heapsize:设置了android:largeHeap以后可使用的内存大小
ro.product.brand:手机品牌
ro.product.device:设备名称
ro.product.model:设备内部代号
ro.product.name:设备名称
ro.product.manufacturer:设备制造商
ro.serialno:设备序列号
ro.sf.lcd_density:设备屏幕密度
ro.config.ringtone:默认来电铃声
ro.config.notification_sound:默认通知铃声
ro.config.alarm_alert:默认闹钟铃声
dalvik.vm.stack-trace-file:trace文件放置目录