美文网首页
常用adb命令

常用adb命令

作者: 升级打怪啊怪 | 来源:发表于2018-04-07 18:42 被阅读34次

查询设备

adb device
输出格式:序列号+状态
序列号:由adb创建的字符串,用于通过其控制台端口号唯一识别设备。
状态:offline:实例未连接到adb或不响应;device:实例已连接到adb服务器;no device:未连接设备

将命令发送至特定设备

db -s 序列号 command
如果有多个设备且只有一个设备是模拟器,使用-e选项将命令发送至该模拟器
如果有多个设备且只有一个设备是硬件设备,使用-d选项将命令发送至该硬件设备

安装应用

adb install path_to_apk

设置端口转发

forward命令设置任意端口转发:将对特定主机端口的请求转发到设备实例上的其他端口
例如:设置主机端口6100到设备端口7100的转发:adb forward tcp:6100 tcp:7100
设置传输到指定的抽象UNIX网域套接字的转发:adb forward tcp:6100 local:logd

主机与设备之间文件复制传输

pull和push命令可以将任意目录和文件复制到设备中的任意位置
adb pull remote local :从设备复制文件或目录
adb push local remote :将文件或目录复制到设备
local指主机上的目标文件/目录的路径
remote指设备上的目标文件/目录的路径

停止adb服务器

adb kill-server

启动adb服务器

adb start-server 检查adb服务器进程是否在运行,如果未运行则启动它

日志输出

adb logcat [option] [filter-specs]
option:
-s:设置输出日志的标签,只显示该标签的日志
Log.d("TAG","first test");
adb logcat -s TAG

指定logcat的日志输出格式:
将logcat日志输出到文件中,adb logcat > log
-v:设置日志的输出格式
"time"格式:查看日志的输出时间 adb logcat -v time
"brief"格式:默认的日志格式:"优先级/标签(进程ID):日志信息" adb logcat -v prief
"process"格式:"优先级(进程ID):日志信息" adb logcat -v process
"tag"格式:"优先级/标签:日志信息" adb logcat -v tag
"thread"格式:"优先级(进程ID:线程ID)标签:日志信息" adb logcat -v thread

adb logcat -c :清空日志缓存信息,重新开始输出日志信息

filter-specs
过滤项格式:<tag>[:priority],标签:日志等级,默认的日志过滤项是"*:I"
-- V : Verbose (明细);
-- D : Debug (调试);
-- I : Info (信息);
-- W : Warn (警告);
-- E : Error (错误);
-- F : Fatal (严重错误);
-- S : Silent(Super all output) (最高的优先级, 可能不会记载东西);

过滤指定等级日志:
adb logcat 10 *:E 命令, 显示最近10条 Error 以上级别的日志

过滤指定标签等级日志:
adb logcat 标签:D *:S 过滤该标签且优先级Debug等级以上的级别的日志 *:S,如果没有会输出错误

同时设置多个过滤器:
adb logcat 标签1:D 标签2:I *:S 输出标签1的Debug以上级别和标签2的Info以上级别的日志

使用管道过滤日志
过滤固定字符串:只要命令行出现的日志都可以过滤,不管是不是标签
adb logcat | grep Wifi
过滤字符串忽略大小写:adb logcat | grep -i wifi

adb get-serialno 输出adb实例序列号字符串
adb get-state 输出设备的adb在线状态
adb wait-for-device 阻止执行,直至设备处于在线状态 adb wait-for-device install app.apk

发出shell命令

使用shell命令通过adb发出设备命令,可以进入或不进入设备实例上的adb远程shell
要在不进入远程shell的情况下发出一个命令:adb shell shell_command
进入设备实例上的远程shell:adb shell
shell 命令二进制文件存储在模拟器或设备的文件系统中,其路径为 /system/bin/。

调用Activity Manager(am)

使用Activity Manager工具发出命令以执行各种系统操作,如启动Activity、强行停止进程、广播intent、修改设备屏幕属性及其他操作
am command
也可以直接从adb发出Activity Manager命令,无需进入远程shell
adb shell am start -a android.intent.action.VIEW

adb shell am start [options] intent :启动intent指定的Activity
options:
-D:启用调试。
-W:等待启动完成。
--start-profiler file:启动分析器并将结果发送到 file。
-P file:类似于 --start-profiler,但当应用进入空闲状态时分析停止。
-R count:重复 Activity 启动 count 次数。在每次重复前,将完成顶部 Activity。
-S:启动 Activity 前强行停止目标应用。
--opengl-trace:启用 OpenGL 函数的跟踪。
--user user_id | current:指定要作为哪个用户运行;如果未指定,则作为当前用户运行。

startservice [options] intent :启动intent指定的service
options:
--user user_id | current:指定要作为哪个用户运行;如果未指定,则作为当前用户运行。

force-stop package :强行停止与package关联的所有应用

kill [options] package :终止与 package(应用的包名称)关联的所有进程。此命令仅终止可安全终止且不会影响用户体验的进程。
选项包括:
--user user_id | all | current:指定将终止其进程的用户;如果未指定,则终止所有用户的进程。

kill-all:终止所有后台进程

broadcast [options] intent:发出广播 intent。
选项包括:
[--user user_id | all | current]:指定要发送到的用户;如果未指定,则发送到所有用户。

instrument [options] component :使用 Instrumentation 实例启动监控。通常,目标 component 是表单 test_package/runner_class

调用软件包管理器pm

pm command
adb shell pm command

list packages [options] filter:输出包名称包含filter中的文本的软件包
选项:
-f:查看它们的关联文件。
-d:进行过滤以仅显示已停用的软件包。
-e:进行过滤以仅显示已启用的软件包。
-s:进行过滤以仅显示系统软件包。
-3:进行过滤以仅显示第三方软件包。
-i:查看软件包的安装程序。
-u:也包括卸载的软件包。
--user user_id:要查询的用户空间。

list permission-groups 输出所有已知的权限组

list permissions [options] group
输出所有已知权限,或者,仅输出 group 中的权限。
选项:
-g:按组加以组织。
-f:输出所有信息。
-s:简短摘要。
-d:仅列出危险权限。
-u:仅列出用户将看到的权限。

list instrumentation [options]
列出所有测试软件包。
选项:
-f:列出用于测试软件包的 APK 文件。
target_package:列出仅用于此应用的测试软件包

install [options] path
将软件包(通过 path 指定)安装到系统。
选项:
-l:安装具有转发锁定功能的软件包。
-r:重新安装现有应用,保留其数据。
-t:允许安装测试 APK。
-i installer_package_name:指定安装程序软件包名称。
-s:在共享的大容量存储(如 sdcard)上安装软件包。
-f:在内部系统内存上安装软件包。
-d:允许版本代码降级。
-g:授予应用清单中列出的所有权限。

uninstall [options] package
从系统中移除软件包。
选项:
-k:移除软件包后保留数据和缓存目录。

clear package
删除与软件包关联的所有数据。

查看设备系统信息

adb shell getprop

获取系统版本:adb shell getprop ro.build.version.release

获取系统api版本:adb shell getprop ro.build.version.sdk

dumpsys

adb shell dumpsys [options]

options:

meminfo:显示内存信息;cpuinfo:显示CPU信息;account:显示accounts信息; activity:显示所有的activities的信息;window:显示键盘,窗口和它们的关系;wifi:显示wifi信息

查看当前的activity&Package信息

1、adb logcat | grep ActivityManager

04-03 10:46:56.343 1617 1617 I ActivityManager_activity: Resuming ActivityRecord{f7f8de token=android.os.BinderProxy@c2ac0e8 {com.android.settings/com.android.settings.Settings$SecurityAndPrivacySettingsActivity}} with isForward=true,forwardBitChanged=false onlyLocalRequest=false

2、adb shell dumpsys activity activities

  • TaskRecord{67b3f36 #11 A=com.android.settings U=0 StackId=1 sz=2}

    userId=0 effectiveUid=1000 mCallingUid=u0a59 mUserSetupComplete=true mCallingPackage=com.huawei.android.launcher
    
    affinity=com.android.settings
    
    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.settings/.HWSettings}
    

cmp=com.android.settings/.HWSettings

com.android.settings是包名,HWSettings是当前活动的activity

屏幕截图

screencap filename
adb shell screencap /sdcard/screen.png

录制视频

screenrecord [options] filename
adb shell screenrecord /sdcard/demo.mp4

其他 shell 命令

所有可用shell程序的列表:adb shell ls /system/bin

参考:https://developer.android.com/studio/command-line/adb.html?hl=zh-cn

相关文章

网友评论

      本文标题:常用adb命令

      本文链接:https://www.haomeiwen.com/subject/dmnyhftx.html