您的位置:首页 > 移动开发 > Android开发

Android常用adb命令

2015-08-21 15:53 363 查看
一、ADB(Android Debug Bridge);其工具包文件在SDK中,文件路径位于SDK安装路径下platform-tools文件夹中:

    ADB源码在Android工程目录下/system/core/adb中。



    使用:直接将运行目录切换到adb.exe所在路径,真机通过USB连接,或者启动模拟器,即可通过输入命令进行调试:
    


    adb devices命令是查询当前设备的连接状况的,输出格式为[serialNumber][state],state分为三种:一种是Offline,说明当前设备没有连接或无响应;device则表示连接成功;no-device则表示当前无模拟器也无真机连接。

1、常用命令(adb命令文档:http://developer.android.com/tools/help/adb.html)
    adb命令的通用格式为:adb[-d|-e|-s <serialNumber>]
<command>

CategoryCommandDescriptionComments
Target 

Device
-d
将adb command发送给当期唯一使用USB方式连接的设备
如果USB连接设备并不唯一,则会返回错误
-e
将adb command发送给当期唯一连接的模拟器设备如果连接的模拟器并不唯一,则会返回错误
-s <serialNumber>
将adb command发送给指定模拟器/真机设备(通过其serial number进行指定)
See Directing Commands
to a Specific Emulator/Device Instance.
General
devices
显示当前连接的所有模拟器/真机设备See Querying for Emulator/Device
Instancesfor more information.
help
显示所有支持的adb commands列表
version
显示adb版本信息
Debug
logcat [option] [filter-specs]
将logcat信息显示到屏幕上
bugreport
将 
dumpsys
dumpstate
,
and 
logcat
 信息显示到屏幕上,用以显示bug
jdwp
显示可用的JDWP进程列表
You can use the 
forward jdwp:<pid>
 port-forwarding specification to connect to a specific JDWP process. For example: 
adb forward tcp:8000 jdwp:472

jdb -attach localhost:8000
Data
install <path-to-apk>
安装应用程序(执行apk路径名)
pull <remote> <local>
将模拟器/真机中的文件复制到开发环境机器中
push <local> <remote>
拷贝文件都模拟器或设备中.
Ports and Networking
forward <local> <remote>
Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance.Port specifications can use these schemes:
tcp:<portnum>

local:<UNIX domain socket name>

dev:<character device name>

jdwp:<pid>

ppp <tty> [parm]...
Run PPP over USB.
<tty>
 — the tty for PPP stream. For example
dev:/dev/omap_csmi_ttyl
.
[parm]... 
— zero or more PPP/PPPD options, such as
defaultroute
local
,
notty
,
etc.
Note that you should not automatically start a PPP connection.
Scripting
get-serialno
打印所有模拟器/设备的serialnoSee Querying
for Emulator/Device Instancesfor more information.
get-state
打印当前所有模拟器/设备的状态
wait-for-device
当设备不处于在线状态时,这个选项会暂停命令的执行,直到相应设备可用为止You can prepend this command to other adb commands, in which case adb will wait until the emulator/device instance is connected before issuing the other commands. Here's an example:
adb wait-for-device shell getprop

Note that this command does not cause adb to wait until the entire system is fully booted. For that reason, you should not prepend it to other commands that require a fully booted system.
As an example, the 
install
 requires the Android package manager, which is available only after the system is fully booted. A command such as
adb wait-for-device install <app>.apk

would issue the 
install
 command as soon as the emulator or device instance connected to the adb server, but before the Android
system was fully booted, so it would result in an error.
Server
start-server
检查adb server是否已经启动运行。如果未启动,则将其启动
kill-server
终止adb server
Shell
shell
Starts a remote shell in the target emulator/device instance.See ADB
Shell Commands for more information.
shell [shellCommand]
Issues a shell command in the target emulator/device instance and then exits the remote shell.
    adb shell使用分为两种情况,一种是adb shell,不加任何命令,则系统会启动一个shell环境:
    


    一种是adb shell后面加上command,则程序会直接执行命令,不提供交互环境,指令执行完成后,其会自动退出shell环境。

2、常用的adb [shell]命令: 

1)ADB常用命令:(前面均已提及)
1. 查看设备 
      adb  devices
  这个命令是查看当前连接的设备, 连接到计算机的android设备或者模拟器将会列出显示

2. 安装软件
  adb  install <apk文件路径>
  这个命令将指定的apk文件安装到设备上
参数“-r”,它是更新安装的意思,
参数 -s ,安装到sdcard.
如: adb install com.sina.weibo.apk

3. 卸载软件
  adb  uninstall <包名>
如果加 -k 参数,为卸载软件但是保留配置和缓存文件.
如: adb uninstall com.sina.weibo.apk

4. 登录设备shell
  adb  shell
  adbshell <command命令>
  这个命令将登录设备的shell.
  后面加<command命令>将是直接运行设备命令, 相当于执行远程命令
如:	adb  shell cat /proc/kmsg

5. 从电脑上发送文件到设备
  adb push <本地路径> <远程路径>
  用push命令可以把本机电脑上的文件或者文件夹复制到设备(手机)
如: adb push /local/build.prop  /system/build.prop

6. 从设备上下载文件到电脑
 adb  pull <远程路径> <本地路径>
 用pull命令可以把设备(手机)上的文件或者文件夹复制到本机电脑
如: adb  /system/build.prop  /local/

7. 同步更新
adb sync [ <directory> ]
如果不指定目录,将同时更新 /data 和  /system/
如: adb sync /data/

8. 显示帮助信息
adb  help
 这个命令将显示帮助信息

9.  重新挂载
adb  remount
重新挂载系统  分区 用于读写

10.  启动、关闭ADB Server
adb start-server             - ensure that there is a server running
adb kill-server              - kill the server if it is running

11. 重启设备
adb reboot [bootloader|recovery]      - reboots the device, optionally into the bootloader or recovery program

12. 查看Log
[adb] logcat [<option>] ... [<filter-spec>] ..
-b <buffer> 加载一个可使用的日志缓冲区供查看,比如event 和radio . 默认值是main 。具体查看Viewing 	Alternative Log Buffers.
-c 清楚屏幕上的日志.
-d 输出日志到屏幕上.
-f <filename> 指定输出日志信息的<filename> ,默认是stdout .
-g 输出指定的日志缓冲区,输出后退出.
-n <count> 设置日志的最大数目<count> .,默认值是4,需要和 -r 选项一起使用。
-r <kbytes> 每<kbytes> 时输出日志,默认值为16,需要和-f 选项一起使用.
-s 设置默认的过滤级别为silent.
-v <format> 设置日志输入格式,默认的是brief 格式

where <tag> is a log component tag (or * for all) and priority is:
V    Verbose
D    Debug
I    Info
W    Warn
E    Error
F    Fatal
S    Silent (supress all output)

'*' means '*:d' and <tag> by itself means <tag>:v

13、查看bug报告:
adb bugreport 

2)常用adb shell命令:
1.  按键事件
input text <string>   input a string to device
input keyevent <event_code>   send a Key Event to device
如: adb shell input keyevent 26      (PowerKey)

2.  am命令
am start  <INTENT> : start an Activity
如 :
am start -n com.android.calculator/com.android.calculator2.Calculator

am broadcast <INTENT>

am startservice <INTENT>
am force-stop <PACKAGE>
am kill <PACKAGE>
am kill-all
am broadcast <INTENT>

"<INTENT> specifications include these flags and arguments:\n" +
"    [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]\n" +
"    [-c <CATEGORY> [-c <CATEGORY>] ...]\n" +
"    [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]\n" +
"    [--esn <EXTRA_KEY> ...]\n" +
"    [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]\n" +
"    [--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]\n" +
"    [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]\n" +
"    [--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]\n" +
"    [--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]\n" +
"    [--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]\n"
"    [-n <COMPONENT>] [-f <FLAGS>]\n" +
"    [--grant-read-uri-permission] [--grant-write-uri-permission]\n" +
"    [--debug-log-resolution] [--exclude-stopped-packages]\n" +
"    [--include-stopped-packages]\n" +
"    [--activity-brought-to-front] [--activity-clear-top]\n" +
"    [--activity-clear-when-task-reset] [--activity-exclude-from-recents]\n" +
"    [--activity-launched-from-history] [--activity-multiple-task]\n" +
"    [--activity-no-animation] [--activity-no-history]\n" +
"    [--activity-no-user-action] [--activity-previous-is-top]\n" +
"    [--activity-reorder-to-front] [--activity-reset-task-if-needed]\n" +
"    [--activity-single-top] [--activity-clear-task]\n" +
"    [--activity-task-on-home]\n" +
"    [--receiver-registered-only] [--receiver-replace-pending]\n" +
"    [--selector]\n" +
"    [<URI> | <PACKAGE> | <COMPONENT>]\n"

更多详细用法请见am 使用帮助.

3. pm 命令
pm list packages
-f: see their associated file
-s: filter to only show system packages
-3 ilter to only show third party packages
pm list packages [-f] [-d] [-e] [-s] [-e] [-u] [FILTER]");
pm list permission-groups
pm list permissions [-g] [-f] [-d] [-u] [GROUP]");
pm list instrumentation [-f] [TARGET-PACKAGE]");
pm list features
pm list libraries
pm path PACKAGE
pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH
pm uninstall [-k] PACKAGE
pm clear PACKAGE
pm enable PACKAGE_OR_COMPONENT
pm disable PACKAGE_OR_COMPONENT
pm disable-user PACKAGE_OR_COMPONENT
pm set-install-location [0/auto] [1/internal] [2/external]
pm get-install-location

更多詳细用法,请见pm使用帮助.

4. dumpsys
dumpsys activity
dumpsys activity intents
dumpsys activity broadcasts
dumpsys activity providers
dumpsys activity services
dumpsys activity activities
dumpsys activity processes

dumpsys window
dumpsys window windows
dumpsys window tokens
dumpsys window sessions
dumpsys window policy
dumpsys window input

dumpsys statusbar
dumpsys notification
dumpsys package  [<PACKAGE> ]
dumpsys location
dumpsys alarm
dumpsys connectivity
dumpsys wifi
…....等等

5. ime	输入法管理
ime list [-a] [-s]
list command prints all enabled input methods
-s option to see only a single summary line of each.
-a option to see all input method

ime enable ID
ime disable ID
ime set ID


二、ADB系统主要包含有三个模块:
1、ADB Client   (运行在PC端,比如使用Eclipse开发运行在Windows环境下,则ADB
Client即运行在Windows环境下)
2、ADB Server   (运行在PC端,其用于管理研发机设备与目标设备/模拟器的连接状态(多对多关系);处理ADB Client的连接请求)
     ADB Client与ADB Server用ADB_HOST=1来统一定义,其调用的adb。
3、ADB Daemon(运行在目标设备/模拟器上,用以与ADB Server建立通信连接,并完成一系列ADB命令请求)
     ADB Daemon使用ADB_HOST=0来定义,其调用的是adbd; 

三、ADB源码(源码路径/system/core/adb)

1、先看源码结构Android.mk文件
1)adb(adb host)
LOCAL_PATH:= $(call my-dir)
# adb host tool
# =========================================================
include $(CLEAR_VARS)
# Default to a virtual (sockets) usb interface
USB_SRCS :=
EXTRA_SRCS :=
..............

#涉及到的源码文件
LOCAL_SRC_FILES := \
adb.c \
console.c \
transport.c \
transport_local.c \
transport_usb.c \
commandline.c \
adb_client.c \
adb_auth_host.c \
sockets.c \
services.c \
file_sync_client.c \
$(EXTRA_SRCS) \
$(USB_SRCS) \
usb_vendors.c
LOCAL_C_INCLUDES += external/openssl/include
ifneq ($(USE_SYSDEPS_WIN32),)
LOCAL_SRC_FILES += sysdeps_win32.c
else
LOCAL_SRC_FILES += fdevent.c
endif

#注意这里定义的全局宏 ADB_HOST = 1
LOCAL_CFLAGS += -O2 -g -DADB_HOST=1 -Wall -Wno-unused-parameter -Werror
LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
#模块名称为adb
LOCAL_MODULE := adb
LOCAL_MODULE_TAGS := debug


2)adbd(ADB Daemon)
# adbd device daemon
# =========================================================
include $(CLEAR_VARS)
#使用的源文件,可以看到源代码并未对ADB_HOST与ADB Daemon在文件上严格地界限开来,
#两者共同地使用了许多文件,就使得前面定义的全局宏ADB_HOST显得十分重要
LOCAL_SRC_FILES := \
adb.c \
fdevent.c \
transport.c \
transport_local.c \
transport_usb.c \
adb_auth_client.c \
sockets.c \
services.c \
file_sync_service.c \
jdwp_service.c \
framebuffer_service.c \
remount_service.c \
disable_verity_service.c \
usb_linux_client.c
#这里相区别的定义的全局宏ADB_HOST为0
LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter -Werror
LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE
ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_ROOT=1
endif
ifneq (,$(filter userdebug,$(TARGET_BUILD_VARIANT)))
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
endif
#模块名
LOCAL_MODULE := adbd


3)adbd程序运行在目标设备端(真机或者模拟器),其何时会被启动呢?
   来看init.rc脚本对于adbd的描述:

# path: /system/core/rootdir/init.rc
# adbd is controlled via property triggers in init.<platform>.usb.rc
service adbd /sbin/adbd --root_seclabel=u:r:su:s0
class core
socket adbd stream 660 system system
disabled
seclabel u:r:adbd:s0

# adbd on at boot in emulator
on property:ro.kernel.qemu=1
start adbd

    可以看到adbd是作为一个系统service服务的形式存在的,它的文件路径在/sbin/adbd;class名为core;
disable表示adbd不会自动启动,需要显式地调用服务名对启动该服务。
    后面on <trigger>,表示如果是模拟器(property:ro.kernel.qemu=1)情况,则可以直接启动adbd。

2、程序入口函数main函数在adb.c文件中

/** @path: \system\core\adb\adb.c **/
int main(int argc, char **argv)
{
/** 看到前面定义的ADB_HOST宏即是用来区分ADB Client/Server与ADB Daemon,
* 系统会对其进行分别编译 **/
#if ADB_HOST
adb_sysdeps_init();
adb_trace_init();
D("Handling commandline()\n");
return adb_commandline(argc - 1, argv + 1);
#else
/** 如果adbd运行在模拟器中,它将允许adb在模拟器中追踪via adb-debug qemud service*/
adb_qemu_trace_init();
while(1) {
int c;
int option_index = 0;
static structoption opts[] = {
{"root_seclabel", required_argument, 0, 's' },
{"device_banner", required_argument, 0, 'b' }
};
c = getopt_long(argc, argv, "", opts, &option_index);
if (c == -1)
break;
switch (c) {
case's':
root_seclabel = optarg;
break;
case'b':
adb_device_banner = optarg;
break;
default:
break;
}
}

start_device_log();
D("Handling main()\n");
return adb_main(0, DEFAULT_ADB_PORT);
#endif
}


3、ADB Protocol(ADB通信协议)
    ADB存在两种通信协议:
1)ADB Client与ADB Server之间的通信协议
    ADB Client通过本地IP地址(127.0.0.1)的5037端口进行与ADB Server进行连接
    1、当Client的请求执行成功时,Server会向Client返回“OKAY”
    2、当执行请求时有错误发生时,Server回向Client返回“FAIL”
    3、当Client请求查询版本号时,Server会返回一个4字节、十六进制的数值来表示版本号。

2)ADB Daemon与ADB Server之间的通信协议(称为transport)
    1、usb transport   ADB Server与Daemon通过USB进行连接
    2、local transport ADB Server与Daemon通过TCP端口与其进行连接(不仅限于运行在本机上的模拟器,真机设备同样支持TCP进行连接)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: