Android ADB命令教程二——ADB命令详解

时间:2023-03-08 17:04:25

Android ADB命令教程二——ADB命令详解

    我们使用 adb -h 来看看,adb命令里面包含了多少子命令:
C:\Windows\System32>adb -h
Android Debug Bridge version 1.0.31
 -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 - directs command to the device or emulator with the given
                                 serial number or qualifier. Overrides ANDROID_SERIAL
                                 environment variable.
 -p - 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 [:] - connect to a device via TCP/IP
                                 Port 5555 is used by default if no port number is specified.
 disconnect [[:]] - 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]
                               - copy file/dir to device
                                 ('-p' to display the transfer progress)
  adb pull [-p] [-a] []
                               - copy file/dir from device
                                 ('-p' to display the transfer progress)
                                 ('-a' means copy timestamp and mode)
  adb sync [ ] - 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 - run remote shell command
  adb emu - run emulator console command
  adb logcat [ ] - View device log
  adb forward --list - list all forward socket connections.
                                 the format is a list of lines with the following format:
                                    " " " " "\n"
  adb forward - forward socket connections
                                 forward specs are one of:
                                   tcp:
                                   localabstract:
                                   localreserved:
                                   localfilesystem:
                                   dev:
                                   jdwp: (remote only)
  adb forward --no-rebind
                               - same as 'adb forward ' but fails
                                 if is already forwarded
  adb forward --remove - remove a specific forward socket connection
  adb forward --remove-all - remove all forward socket connections
  adb jdwp - list PIDs of processes hosting a JDWP transport
  adb install [-l] [-r] [-d] [-s] [--algo --key --iv ]
                               - push this package file to the device and install it
                                 ('-l' means forward-lock the app)
                                 ('-r' means reinstall the app, keeping its data)
                                 ('-d' means allow version code downgrade)
                                 ('-s' means install on SD card instead of internal storage)
                                 ('--algo', '--key', and '--iv' mean the file is encrypted already)
  adb uninstall [-k] - 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 ] [-apk|-noapk] [-obb|-noobb] [-shared|-noshared] [-all] [-system|-nosystem] []
                               - write an archive of the device's data to .
                                 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) 
                                 ( 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 - restore device contents from the backup archive
  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:
  adb get-devpath - prints:
  adb status-window - continuously print device status for a specified device
  adb remount - remounts the /system partition 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 - restarts the adbd daemon listening on TCP on the specified port
networking:
  adb ppp [parameters] - Run PPP over USB.
 Note: you should not automatically start a PPP connection.
  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 [ ]
  can be interpreted in several ways:
  - If is not specified, both /system and /data partitions will be updated.
  - If it is "system" 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.
一、列出所有的可连接Android设备
C:\Windows\System32>adb devices
List of devices attached
emulator-5556 device
    第一列返回的就是设备的serialNumber,如果我们的机器上有多台android设备,可以使用该serialNumber去指定对应的唯一的Android设备去执行。
    使用adb get-serialno也可以获取得到serialNumber。
    执行结果是adb为每一个设备输出以下状态信息: 
    序列号(serialNumber) — 由adb创建的使用控制台端口号的用于唯一标识一个模拟器
或手机设备的字符串,格式是 <设备类型>-<端口号>,例如: emulator-5554
    状态(state) — 连接状态,其值是: 
offline — 未连接或未响应 
device —已经连接到服务商。注意这个状态并不表示Android系统已经完全启动起来,
    系统启动的过程中已经可以连接adb,但这个状态是正常的可操作状态。
二、操作指定的模拟器或手机 
    如果有多个模拟器或手机正在运行,当使用adb命令的时候就需要指定目标设备,这可以通过使用-s选项参数实现,用法是:
adb -s  
    即可以在adb命令中使用序列号指定特定的目标,前文已经提到的devices命令可以实现查询设备的序列号信息。 
    例如: 
adb -s emulator-5556 install helloWorld.apk 
    需要注意的是,如果使用了-s而没有指定设备的话,adb会报错。
三、安装应用程序 
    可以使用adb从开发用电脑中复制应用程序并且安装到模拟器或手机上,使用install命令即可,在这个命令中,必须指定待安装的.apk文件的路径: 
    adb install
    关于创建可安装的应用的更多信息,请参见Android Asset Packaging Tool (aapt). 注意,如果使用了安装有ADT插件的Eclipse开发环境,就不需要直接使用adb或aapt命令来安装应用程序了,ADT插件可以自动完成这些操作。
    对应的卸载命令是adb uninstall
四、转发端口 
    可以使用forward 命令转发端口 — 将特定端口上的请求转发到模拟器或手机的不同的端口上。下例是从6100端口转到7100端口:
    adb forward tcp:6100 tcp:7100 
    也可以使用UNIX命名的socket标识:
     adb forward tcp:6100 local:logd
五、与模拟器或手机传输文件 
    可以使用adb的 pull 和 push 命令从模拟器或手机中复制文件,或者将文件复制到模拟器或手机中。与 install 命令不同,它仅能复制.apk文件到特定的位置, pull 和 push 命令可以复制任意文件夹和文件到模拟器或手机的任何位置。 从模拟器或手机中复制一个文件或文件夹(递归的)使用: 
    adb pull  
    复制一个文件或文件夹(递归的)到模拟器或手机中使用: 
    adb push  
    在这个命令中和引用的是文件或文件夹的路径,在开发用电脑上的是local,在模拟器或手机上的是remote。 例如: 
    adb push foo.txt /sdcard/foo.txt
六、同步备份adb sync
    使用步骤:
    1、将Android设备上的/system或者/data目录拷贝到本地的一个路径,例如我本地是E:/Android
    2、如果Android设备上的/system或者/data目录修改了,想还原这两个文件夹,那么使用adb sync即可
    感觉这个命令没有多大用处,就是一个备份。
七、查看Android的运行日记信息
adb logcat