adb常用命令集和原理介绍

adb介绍

  • adb简介
  • adb原理
  • adb常用指令集

adb简介

adb译为Android设备的桥梁,可以用于快速更新设备中代码,应用或者Android系统升级,在设备上运行shell命令,管理设备或手机模拟器上的端口。


adb原理

Android设备adb授权的原理

在我们的PC机(以windows为例)上启动了adb.exe进程时,adb会在本地生成一对密钥adbkey(私钥)与adbkey.pub(公钥);
根据弹框提示“The computer’s RSA key fingerprint is:xxxx”,可以看出是一对RSA算 法的密钥,其中公钥是用来发送给手机的;
当执行“adb shell”时,adb.exe会将当前PC的公钥(或者公钥的hash值)(fingerprint)发送给android设备;这时,如果android上已经保存了这台PC的公钥,则匹配出对应的公钥进行认证,建立adb连接;如果android上没有保存这台PC的公钥,则会弹出提示框,让你确认是否允许这台机器进行adb连接,当你点击了允许授权之后,android就会保存了这台PC的adbkey.pub(公钥);


这里写图片描述
adb分为三个部分:PC上的adb client,adb server和手机(也可能是PC上的模拟器)上的adb daemon。
这是一个多连接的设计。即一台PC可以连接多个手机,一台手机也可以连接多个PC。

ADB Client:

ADB Client:注意这里的Client和Daemon不是一一对应的,Client比如Eclipse DDMS,Windows终端命令行,豌豆荚等。Client本质上就是Shell,用来发送命令给Server。发送命令时,首先检测PC上有没有启动Server,如果没有Server,则自动启动一个Server,然后将命令发送到Server,并不关心命令发送过去以后会怎样。

ADB Server:

ADB Server:运行在PC上的后台程序,目的是检测USB接口何时连接或者移除设备。ADB
Server维护着一个“已连接的设备的链表”,并且为每一个设备标记了一个状态:offline,bootloader,recovery或者online;Server一直在做一些循环和等待,以协调client和Server还有daemon之间的通信。offline说明Server发现了一个设备,但是不能成功连接到Daemon—-这样最好重启一下手机。

ADB Daemon:

ADB Daemon:运行在手机上的一个进程,作用是连接到adb server(通过usb或tcp-ip),并且为client提供一些服务
这里写图片描述

Adb的运行机制和架构

这里写图片描述

运行的流程可见之下:

1:PC端adb server发送cmd到小机端
2:adbd(adb daemon)的output thread从/dev/android_usb读取到cmd并解析
(adbd和内核usb driver通过/dev/android_usb进行数据交互)
3:output_thread将解析的cmd写到socketpair的A端
4:fdevent_loop通过select socketpair的B端,得知A端有数据写入,B端可读,然后读取cmd,进行处理(handle_packet)
5:处理完后将结果写到socketpair的B端
6:Input_thread阻塞式读取socketpair的A端
7:Input_thread读取到数据后,写入/dev/android_usb
8:由usb driver将response发给pc端
output_thread负责读取/dev/android_usb,Input_thread负责写入

adb客户端、服务端、守护进程之间的沟通:
1、client调用某个adb命令。
2、adb进程fork出一个子进程作为server。
3、server查找当前连接的emulator/device。
4、server接收到来自client请求。
5、server处理请求,将本地处理不了的请求发给emulator/device。
6、位于emulator/device的adbd拿到请求后交给对应的java虚拟机进程。
7、adbd将结果发回给server。
8、server讲结果发回给client。

adb常用指令集

C:\Users\admin>adb help
Android Debug Bridge version 1.0.32

 -a                            - directs adb to listen on all interfaces for a connection
 -d                            - directs command to the only connected USB device
                                 returns an error if more than one USB device is present.
 -e                            - directs command to the only running emulator.
                                 returns an error if more than one emulator is running.
 -s <specific device>          - directs command to the device or emulator with the given
                                 serial number or qualifier. Overrides ANDROID_SERIAL
                                 environment variable.
 -p <product name or path>     - simple product name like 'sooner', or
                                 a relative/absolute path to a product
                                 out directory like 'out/target/product/sooner'.
                                 If -p is not specified, the ANDROID_PRODUCT_OUT
                                 environment variable is used, which must
                                 be an absolute path.
 -H                            - Name of adb server host (default: localhost)
 -P                            - Port of adb server (default: 5037)
 devices [-l]                  - list all connected devices
                                 ('-l' will also list device qualifiers)
 connect <host>[:<port>]       - connect to a device via TCP/IP
                                 Port 5555 is used by default if no port number is specified.
 disconnect [<host>[:<port>]]  - disconnect from a TCP/IP device.
                                 Port 5555 is used by default if no port number is specified.
                                 Using this command with no additional arguments
                                 will disconnect from all connected TCP/IP devices.

device commands:
  adb push [-p] <local> <remote>
                               - copy file/dir to device
                                 ('-p' to display the transfer progress)
  adb pull [-p] [-a] <remote> [<local>]
                               - copy file/dir from device
                                 ('-p' to display the transfer progress)
                                 ('-a' means copy timestamp and mode)
  adb sync [ <directory> ]     - copy host->device only if changed
                                 (-l means list but don't copy)
                                 (see 'adb help all')
  adb shell                    - run remote shell interactively
  adb shell <command>          - run remote shell command
  adb emu <command>            - run emulator console command
  adb logcat [ <filter-spec> ] - View device log
  adb forward --list           - list all forward socket connections.
                                 the format is a list of lines with the following format:
                                    <serial> " " <local> " " <remote> "\n"
  adb forward <local> <remote> - forward socket connections
                                 forward specs are one of:
                                   tcp:<port>
                                   localabstract:<unix domain socket name>
                                   localreserved:<unix domain socket name>
                                   localfilesystem:<unix domain socket name>
                                   dev:<character device name>
                                   jdwp:<process pid> (remote only)
  adb forward --no-rebind <local> <remote>
                               - same as 'adb forward <local> <remote>' but fails
                                 if <local> is already forwarded
  adb forward --remove <local> - remove a specific forward socket connection
  adb forward --remove-all     - remove all forward socket connections
  adb reverse --list           - list all reverse socket connections from device
  adb reverse <remote> <local> - reverse socket connections
                                 reverse specs are one of:
                                   tcp:<port>
                                   localabstract:<unix domain socket name>
                                   localreserved:<unix domain socket name>
                                   localfilesystem:<unix domain socket name>
  adb reverse --norebind <remote> <local>
                               - same as 'adb reverse <remote> <local>' but fails
                                 if <remote> is already reversed.
  adb reverse --remove <remote>
                               - remove a specific reversed socket connection
  adb reverse --remove-all     - remove all reversed socket connections from device
  adb jdwp                     - list PIDs of processes hosting a JDWP transport
  adb install [-lrtsd] <file>
  adb install-multiple [-lrtsdp] <file...>
                               - push this package file to the device and install it
                                 (-l: forward lock application)
                                 (-r: replace existing application)
                                 (-t: allow test packages)
                                 (-s: install application on sdcard)
                                 (-d: allow version code downgrade)
                                 (-p: partial application install)
  adb uninstall [-k] <package> - remove this app package from the device
                                 ('-k' means keep the data and cache directories)
  adb bugreport                - return all information from the device
                                 that should be included in a bug report.

  adb backup [-f <file>] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]
                               - write an archive of the device's data to <file>.
                                 If no -f option is supplied then the data is written
                                 to "backup.ab" in the current directory.
                                 (-apk|-noapk enable/disable backup of the .apks themselves
                                    in the archive; the default is noapk.)
                                 (-obb|-noobb enable/disable backup of any installed apk expansion
                                    (aka .obb) files associated with each application; the default
                                    is noobb.)
                                 (-shared|-noshared enable/disable backup of the device's
                                    shared storage / SD card contents; the default is noshared.)
                                 (-all means to back up all installed applications)
                                 (-system|-nosystem toggles whether -all automatically includes
                                    system applications; the default is to include system apps)
                                 (<packages...> is the list of applications to be backed up.  If
                                    the -all or -shared flags are passed, then the package
                                    list is optional.  Applications explicitly given on the
                                    command line will be included even if -nosystem would
                                    ordinarily cause them to be omitted.)

  adb restore <file>           - restore device contents from the <file> backup archive

  adb disable-verity           - disable dm-verity checking on USERDEBUG builds
  adb keygen <file>            - generate adb public/private key. The private key is stored in <file>,
                                 and the public key is stored in <file>.pub. Any existing files
                                 are overwritten.
  adb help                     - show this help message
  adb version                  - show version num

scripting:
  adb wait-for-device          - block until device is online
  adb start-server             - ensure that there is a server running
  adb kill-server              - kill the server if it is running
  adb get-state                - prints: offline | bootloader | device
  adb get-serialno             - prints: <serial-number>
  adb get-devpath              - prints: <device-path>
  adb status-window            - continuously print device status for a specified device
  adb remount                  - remounts the /system and /vendor (if present) partitions on the device read-write
  adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
  adb reboot-bootloader        - reboots the device into the bootloader
  adb root                     - restarts the adbd daemon with root permissions
  adb usb                      - restarts the adbd daemon listening on USB
  adb tcpip <port>             - restarts the adbd daemon listening on TCP on the specified port
networking:
  adb ppp <tty> [parameters]   - Run PPP over USB.
 Note: you should not automatically start a PPP connection.
 <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
 [parameters] - Eg. defaultroute debug dump local notty usepeerdns

adb sync notes: adb sync [ <directory> ]
  <localdir> can be interpreted in several ways:

  - If <directory> is not specified, /system, /vendor (if present), and /data partitions will be updated.

  - If it is "system", "vendor" or "data", only the corresponding partition
    is updated.

environmental variables:
  ADB_TRACE                    - Print debug information. A comma separated list of the following values
                                 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
  ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.
  ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.

重要的连接:
http://blog.csdn.net/Clovelegent/article/details/52515889
http://blog.csdn.net/sunjing_/article/details/51829957
http://blog.csdn.net/liuweihhhh/article/details/73242497
http://blog.csdn.net/wlwl0071986/article/details/50935496
http://blog.csdn.net/c465869935/article/details/52084643
http://blog.csdn.net/stpeace/article/details/24933813
http://blog.csdn.net/ffmpeg4976/article/details/47911817
http://blog.csdn.net/ysh149216447/article/details/53334015
http://blog.csdn.net/xing1716263268/article/details/8998192

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值