Android 工具:adb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 工具:adb相关的知识,希望对你有一定的参考价值。

android 工具:adb

引言

    Android 开发中, adb 是开发者经常使用的工具,是 Android 开发者必须掌握的。
    Android Debug Bridge,Android 调试桥接器,简称 adb ,该工具位于 <sdk>/platform-tools/ 目录下,其源码位于 <path-to-sourcecode>/system/core/adb 目录下,是用于管理模拟器或真机状态的万能工具。 adb 采用了客户端-服务器模型,包括三个部分:
  1. 客户端部分,运行在开发用的电脑上,可以在命令行中运行adb命令来调用该客户端,像 DDMS 这样的 Android 工具也可以调用 adb 客户端。
  2. 守护进程部分,运行于模拟器或手机的后台。
  3. 服务端部分,是运行在开发用电脑上的后台进程,用于管理客户端与运行在模拟器或真机的守护进程通信。
    adb 工具模型示意图如下:
技术分享

adb 的工作原理

    当启动 adb 客户端时,客户端首先检查 adb 服务端进程是否运行,如果没有运行,则启动服务端。当服务端启动时,它会绑定到本地的 TCP 5037 端口,并且监听从 adb 客户端发来的命令——所有 adb 客户端都使用 5037 端口与 adb 服务端通信。
    接下来服务端与所有正在运行的模拟器或手机连接。它通过扫描 5555-5585 之间的奇数号端口来搜索模拟器或手机,一旦发现 adb 守护进程,就通过此端口进行连接。需要说明的是,每一个模拟器或手机使用一对有序的端口,偶数号端口用于控制台连接,奇数号端口用于 adb 连接,例如:
Emulator 1, console: 5554
Emulator 1, adb: 5555
Emulator 2, console: 5556
Emulator 2, adb: 5557
    即如果模拟器与 adb 在 5555 端口连接,则其与控制台的连接就是 5554 端口。
    当服务端与所有的模拟器建立连接之后,就可以使用 adb 命令来控制或者访问了。因为服务端管理着连接并且可以接收到从多个 adb 客户端的命令,所以可以从任何一个客户端或脚本来控制任何模拟器或手机设备。

USB调试

    使用 adb 命令调试需要手机开启 开发者模式 下的 USB 调试,在 Android 4.2 及更高的版本中,开发者选项默认是隐藏的,你可以去 设置——>关于手机 ,然后连续点击七次版本号即可。返回上层就可以在底部看见 开发者模式 了。当然有些定制的 ROM 开启方式会有点不一样,这个就要去问 ROM 的开发者了。
    关于 USB 调试与计算机的 RSA 密钥指纹配对。Google 官方原文如下:
    When you connect a device running Android 4.2.2 or higher to your computer, the system shows a dialog asking whether to accept an RSA key that allows debugging through this computer. This security mechanism protects user devices because it ensures that USB debugging and other adb commands cannot be executed unless you’re able to unlock the device and acknowledge the dialog. This requires that you have adb version 1.0.31 (available with SDK Platform-tools r16.0.1 and higher) in order to debug on a device running Android 4.2.2 or higher.
    关于使用真机调试的更多信息可参考:在硬件设备上运行应用

Syntax

    You can issue adb commands from a command line on your development machine or from a script. The usage is:

adb [-d|-e|-s <serialNumber>] <command>
    If there‘s only one emulator running or only one device connected, the adb command is sent to that device by default. If multiple emulators are running and/or multiple devices are attached, you need to use the -d, -e, or -s option to specify the target device to which the command should be directed.

Commands

    The table below lists all of the supported adb commands and explains their meaning and usage.
CategoryCommandDescriptionComments
Target Device
-dDirect an adb command to the only attached USB device.Returns an error if more than one USB device is attached.
-eDirect an adb command to the only running emulator instance.Returns an error if more than one emulator instance is running.
-s <serialNumber>Direct an adb command a specific emulator/device instance, referred to by its adb-assigned serial number (such as "emulator-5556").See Directing Commands to a Specific Emulator/Device Instance.
GeneraldevicesPrints a list of all attached emulator/device instances.See Querying for Emulator/Device Instances for more information.

helpPrints a list of supported adb commands.

versionPrints the adb version number.
Debuglogcat [option] [filter-specs]Prints log data to the screen.

bugreportPrints dumpsys, dumpstate, and logcat data to the screen, for the purposes of bug reporting.

jdwpPrints a list of available JDWP processes on a given device.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
Datainstall <path-to-apk>Pushes an Android application (specified as a full path to an .apk file) to an emulator/device.

pull <remote> <local>Copies a specified file from an emulator/device instance to your development computer.

push <local> <remote> Copies a specified file from your development computer to an emulator/device instance.
Ports and Networkingforward <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.

Scriptingget-serialnoPrints the adb instance serial number string.See Querying for Emulator/Device Instances for more information.

get-statePrints the adb state of an emulator/device instance.

wait-for-deviceBlocks execution until the device is online — that is, until the instance state is 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.
Serverstart-serverChecks whether the adb server process is running and starts it, if not.

kill-serverTerminates the adb server process.
ShellshellStarts 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.
Table 1. Available adb commands

adb 用法

查询模拟器或手机状态

    了解 adb 服务端连接的模拟器或手机可以帮助更好的使用 adb 命令,这可以通过 devices 命令来列举当前连接的设备:
adb devices
    执行结果是 adb 为每一个设备输出以下状态信息:
    1) 序列号(serialNumber):由 adb 创建用于唯一标识设备的字符串,格式是 <设备类型>-<端口号>,例如: emulator-5554
    2) 连接状态(state),其值是可能是下面的任意一种:
  • offline — 未连接或未响应
  • device — 表示设备已经连接到服务端。但需要注意的是,这个状态并不表示 Android 系统已经完全启动起来并且可操作,因为系统在启动的过程中就已经连接 adb ,但这个状态是正常的可操作状态。
  • no device – 表示没有任何设备连接(楼主测试过程中没有碰到过 no device 的情况,没连接设备就没任何输出)
    每一个设备的输出形如:
[serialNumber] [state]
    下面是 adb devices 命令和其执行结果:
$ adb devices
List of devices attached 
emulator-5554  device
emulator-5556  device
emulator-5558  device
    如果没有模拟器或手机在运行,运行 adb devices 命令的执行结果如下:
$ adb devces
List of devices attached

操作指定模拟器或手机

    如果有多个模拟器或手机正在运行,当使用 adb 命令的时候就需要指定目标设备,这可以通过使用 -s 选项参数实现,用法如下:
adb -s <serialNumber> <command>
    你可以使用 adb 命令指定序列号在特定的设备上执行命令,这里可以先使用前面提到的 adb devices 命令查询设备的序列号信息。
    例如:
adb -s emulator-5556 install helloWorld.apk
    需要注意的是,如果使用了 -s 而没有指定设备的话, adb 会报错。
    If you have multiple devices available (hardware or emulated), but only one is an emulator, simply use the -e option to send commands to the emulator. Likewise if there’s multiple devices but only one hardware device attached, use the -d option to send commands to the hardware device.
$ adb -e install helloWorld.apk
//同理,如果有多个设备,但只有一个真机,可以使用如下命令快速发送命令
$ adb -d install helloWorld.apk

安装应用

    使用 adb install 命令可以从开发用电脑中复制应用程序并且安装到模拟器或手机上,adb install 命令必须指定待安装的.apk文件的路径:
adb install [-lrtsdg] <path_to_apk>
(-l: 锁定该程序)
(-r: 重新安装该程序,保留应用数据)
(-t: allow test packages)
(-s: 将应用安装到 SD卡,不过现在手机好像都没有 SD卡 了吧)
(-d: 允许降版本号安装,当然只有 debug 包才能使用)
(-g: 安装完默认授予所有运行时权限,这个应该对 Android 6.0 及之后的版本才有效吧)
    更多关于创建 apk 文件可参考:Build And Running

卸载应用

    既然有安装应用的命令,那当然有卸载应用的命令。卸载应用命令的格式如下:
//<package> 表示要卸载应用的包名
adb uninstall [-k] <package>
(-k:不删除程序运行所产生的数据和缓存目录)

端口转发

    使用 adb forward 命令转发端口 — 将特定端口上的请求转发到模拟器或手机的不同的端口上。下例是从 6100端口 转到 7100端口 :
adb forward tcp:6100 tcp:7100
    也可以使用UNIX命名的socket标识:
adb forward tcp:6100 local:logd

Copying Files to or from an Emulator/Device Instance

    You can use the adb commands pull and push to copy files to and from an emulator/device instance. Unlike the install command, which only copies an APK file to a specific location, the pull and push commands let you copy arbitrary directories and files to any location in an emulator/device instance.
    To copy a file or directory (and its sub-directories) from the emulator or device, use
adb pull <remote> <local>
In the commands, <local> and <remote> refer to the paths to the target files/directory on your development machine (local) and on the emulator/device instance (remote). For example:
adb push foo.txt /sdcard/foo.txt

adb shell

    请参考: ADB Shell Commands

Stopping the adb Server

    In some cases, you might need to terminate the adb server process and then restart it to resolve the problem (e.g., if adb does not respond to a command).
    To stop the adb server, use the kill-server command. You can then restart the server by issuing any other adb command.

Wireless usage

    (略)

参考


 
















































以上是关于Android 工具:adb的主要内容,如果未能解决你的问题,请参考以下文章

adb的使用

Android 命令行工具Android 命令行工具简介 ( 官方文档 | SDK 命令行工具 | SDK 构建工具 | SDK 平台工具 | 模拟器工具 | Jetifier 工具 )

Android开发调试工具ADB

Android 逆向Android 逆向通用工具开发 ( Android 逆向通用工具组成部分 | 各模块间的关联 )

Android 逆向Android 进程注入工具开发 ( 总结 | 源码编译 | 逆向环境搭建使用 | 使用进程注入工具进行逆向操作 ) ★★★

Android工具与其它