安卓基础——adb相关内容

Android调试桥(adb)是一个通用的命令行工具,它可以让你与一个模拟器实例或连接的Android设备上通讯。
它是一个客户机-服务器程序,包括三部分:
  1.  一个客户,你的开发机器上运行。您可以调用客户端通过发出一个adb shell。其他Android工具也如ADT插件和DDMS创建 adb client。
  2. 一个服务器,它作为后台进程运行在您的开发机器上。服务器管理客户端之间的通信和adb进程运行在模拟器或设备。
  3. 一个守护进程,它作为后台进程运行在每个模拟器或设备的实例。
你可以找到adb tool 在 <sdk>/platform-tools/

当你开始一个adb client,client(客户端)首先检查是否有一个adb服务器进程已经运行。如果没有,它将执行启动服务器的过程。当服务器启动时,它绑定到本地TCP端口5037和监听来自adb客户端的命令——所有 使用5037端口 通信adb客户与adb服务器。

然后, adb 服务器对所有正在运行的模拟器/设备实例建立连接。它 通过扫描奇数端口 找查使用端口范围在5555到5585之间的 模拟器/设备实例 。当发现一个adb守护进程,它就设置一个端口的连接。注意,每个模拟器/设备实例获得一对连续的端口——一个偶数控制台连接端口和adb的奇数端口连接。例如:
  • 模拟器控制台1:5554   
  • 模拟器1,adb:5555
  • 模拟器控制台2:5556   
  • 模拟器2,adb:5557   等等……      

如上所示,模拟器实例与adb在端口5555连接与控制台在端口5554上侦听的实例一样。      
一旦服务器建立连接所有模拟器实例,您可以使用adb命令来访问这些实例。因为服务器管理连接到模拟器/设备实例和处理命令从多个adb client,你可以 从任何客户端(或脚本) 控制任何模拟器/设备实例。
使用adb调试   
在USB设备连接为了使用adb,您必须启用USB设备系统调试设置,根据开发人员选项。      
Android 4.2和更高版本中,开发人员选项屏幕默认是隐藏的。可见,去设置>关于手机然后按 Build number 七次。回到前面的屏幕底部发现开发人员选项。在一些设备,开发人员选项屏幕可能位于或有不同的名称。      
注意:当你连接的设备上运行Android 4.2.2或更高版本到你的计算机,系统显示一个对话框询问是否接受一个RSA密钥,允许调试通过这台电脑。这种安全机制保护用户设备,因为它确保了USB调试和其他亚洲开发银行命令无法执行,除非你能解锁设备并承认对话框。这就要求你有1.0.31 adb版本(可以使用r16.0.1或者更高的 SDK平台工具 )为了调试Android 4.2.2或更高的设备。      
更多信息连接到一个USB设备,使用硬件设备读取。
语法   
你可以从命令行或从您的开发机器一个脚本上 发出adb命令
      用法是:adb [- d | - e | - s < serialNumber >] <command>
如果只有一个模拟器运行或只有一个设备连接,adb命令发送到默认设备。如果多个模拟器运行和/或连接多个设备,您需要使用- d,e或s选项指定目标设备运行命令。
命令
Category Command Description Comments
Target Device -d
Direct an adb command to the only attached USB device.
唯一 连接USB设备 运行 一个adb命令
Returns an error if more than one USB device is attached.
-e 唯一连接模拟器运行一个adb命令 Returns an error if more than one emulator instance is running.
-s <serialNumber> 在指定模拟器/设备实例直接运行一个adb命令,通过其adb-assigned序列号(如“ emulator- 5556”)。 用法见下文
General devices 打印所有连接模拟器/设备实例的列表。 用法见下文
help 打印所有adb命令的列表。  
version 打印所有adb版本号。  
Debug logcat [option] [filter-specs] 将日志数据打印到屏幕上。.  
bugreport 打印dumpsysdumpstate, 和 logcat 数据到屏幕上, 为了报告BUG  
jdwp Prints a list of available JDWP processes on a given device.(mark不太懂)
你可以使用  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项目(specified as a full path to an .apk file) to an emulator/device.  
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 exampledev:/dev/omap_csmi_ttyl.
  • [parm]... — zero or more PPP/PPPD options, such asdefaultroutelocal,notty, etc.

Note that you should not automatically start a PPP connection.

 
Scripting get-serialno Prints the adb instance serial number string. See Querying for Emulator/Device Instancesfor more information.
get-state Prints the adb state of an emulator/device instance.
wait-for-device Blocks 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.
Server start-server Checks whether the adb server process is running and starts it, if not.  
kill-server Terminates the adb server process.  
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命令之前,它有助于知道连接到adb服务器的 模拟器/设备实例 。您可以利用生成的 附加模拟器/设备 列表使用设备命令:
adb devices
adb将会打印所有实例的状态信息
  • 序列号——一个由adb用来唯一地标识一个模拟器/设备实例的控制台端口号码的字符串。编号的格式是<type> - < consolePort >。这里有一个例子序列号:emulator- 5554
  • 实例的状态,连接状态可能是下列之一:
      • offline — the instance is not connected to adb or is not responding离线状态:实例例没有连接到adb或者没有响应.
      • device — the instance is now connected to the adb server. Note that this state does not imply that the Android system is fully booted and operational, since the instance connects to adb while the system is still booting. However, after boot-up, this is the normal operational state of an emulator/device instance启动状态,可以进行各种操作.
      • no device — there is no emulator/device connected.没有模拟器/设备连接
  • 每个实例的输出格式如下:
    [serialNumber] [state]
    

    Here's an example showing the devices command and its output:

    adb devices
    List of devices attached
    emulator-5554  device
    emulator-5556  device
    emulator-5558  device
在一个特定的模拟器/设备实例运行命令
If multiple emulator/device instances are running, you must specify a target instance when issuing adb commands. To do so, use the  -s  option in the commands. The usage for the  -s  option is:
adb -s <serialNumber> <command> 

As shown, you specify the target instance for a command using its adb-assigned serial number. You can use the devicescommand to obtain the serial numbers of running emulator/device instances. For example:

adb -s emulator-5556 install helloWorld.apk
注意,如果你发出一个命令没有指定目标模拟器/设备实例,多个设备可用,adb生成一个错误。
如果您有多个设备(USB插口插了很多)(硬件或模拟),但只有一个是可操作模拟器,只需使用- e选项来发送命令到模拟器。同样如果有多个设备,但只有一个硬件设备(使用USB连接的设备),使用-d选项来发送命令到硬件设备。
安装一个APP

You can use adb to copy an application from your development computer and install it on an emulator/device instance. To do so, use the install command. With the command, you must specify the path to the .apk file that you want to install:

adb install <path_to_apk>
注意,如果您使用Eclipse IDE和安装了ADT插件,您不需要直接使用adb(或aapt)模拟器/设备上安装应用程序。相反,ADT插件处理应用程序的打包和安装。
端口转发
You can use the  forward  command to set up arbitrary port forwarding — forwarding of requests on a specific host port to a different port on an emulator/device instance. Here's how you would set up forwarding of host port 6100 to emulator/device port 7100:
你可以使用命令设置任意端口转发,请求从一个特定的主机端口转发到另一个端口指定模拟器/设备实例。这是设置如何从主机端口6100转发到模拟器/设备端口7100
adb forward tcp:6100 tcp:7100
您还可以使用adb设置转发抽象UNIX域套接字,如这里所示:
adb forward tcp:6100 local:logd 
从模拟器/设备上导入/导出文件
你可以使用adb命令  pull  和 push去复制文件 to and from an emulator/device instance. 与 install命令不同 , 这是复制一个APK文件到指定位置, the  pull  and  push  命令让你从任意目录和文件复制到模拟器/设备实例中任何位置。

To copy a file or directory (and its sub-directories) from the emulator or device, use

adb pull <remote> <local>

To copy a file or directory (and its sub-directories) to the emulator or device, use

adb push <local> <remote>

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服务

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.
无线调试
adb is usually used over USB. However, it is also possible to use over Wi-Fi, as described here.
  1. 把你的Android设备和adb主机连接到共同的wifi网络访问。我们已经发现,并不是所有的接入点都合适,您可能需要使用一个接入点的防火墙支持配置adb。
    Note:  如果你正试图连接到一个可穿戴装置,迫使其关闭wi - fi连接的蓝牙手机连接到它。
  2. 将设备连接到主机的USB接口
  3. 设置目标设备在端口5555上监听TCP / IP连接.
    $ adb tcpip 5555
  4. 将目标设备断开连接USB接口.
  5. 找到设备ip地址. For example, on a Nexus device, you can find the IP address at Settings >About tablet (or About phone) > Status > IP address. Or, on an Android Wear device, you can find the IP address atSettings > Wi-Fi Settings > Advanced > IP address.
  6. 连接到设备,确定它的IP地址
    $ adb connect <device-ip-address>
    
  7. 确认你的主机连接到目标设备:
    $ adb devices
    List of devices attached
    <device-ip-address>:5555 device
    

You're now good to go!

If the adb connection is ever lost:

  1. Make sure that your host is still connected to the same Wi-Fi network your Android device is.
  2. Reconnect by executing the "adb connect" step again.
  3. Or if that doesn't work, reset your adb host:
    adb kill-server
    

and then start over from the beginning.

附上无线调试图片  挺好玩的 还有更多adbshell 通过shell对手机进行裁图,录制屏幕 很有用的

shell有关操作
录制屏幕
$ adb shell
shell@ $ screenrecord 
-- verbose  / sdcard / demo . mp4
( press  Ctrl - C to stop )
shell@ $ 
exit
$ adb pull 
/ sdcard / demo . mp4
以前参加比赛找录制手机屏幕的应用找了好久 现在无线调试+录制屏幕+pull到电脑一气呵成  真棒!

screenrecord options

Options Description
--help Displays command syntax and options
--size <WIDTHxHEIGHT> Sets the video size: 1280x720. The default value is the device's native display resolution (if supported), 1280x720 if not. For best results, use a size supported by your device's Advanced Video Coding (AVC) encoder.
--bit-rate <RATE> Sets the video bit rate for the video, in megabits per second. The default value is 4Mbps. You can increase the bit rate to improve video quality, but doing so results in larger movie files. The following example sets the recording bit rate to 6Mbps:
screenrecord --bit-rate 6000000 /sdcard/demo.mp4
--time-limit <TIME> Sets the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes).
--rotate Rotates the output 90 degrees. This feature is experimental.
--verbose Displays log information on the command-line screen. If you do not set this option, the utility does not display any information while running.

裁图

The screencap command is a shell utility for taking a screenshot of a device display. While in a shell, the syntax is:

screencap <filename>

To use the screencap from the command line, type the following:

$ adb shell screencap /sdcard/screen.png

Here's an example screenshot session, using the adb shell to capture the screenshot and the pull command to download the file from the device:

$ adb shell
shell@ $ screencap 
/ sdcard / screen . png
shell@ $ 
exit
$ adb pull 
/ sdcard / screen . png

下面的了解一下 挺好玩的


利用PackageManager写shell操作

Within an adb shell, you can issue commands with the package manager ( pm) tool to perform actions and queries on application packages installed on the device. While in a shell, the syntax is:
pm <command>

You can also issue a package manager command directly from adb without entering a remote shell. For example:

adb shell pm uninstall com.example.MyApp

Table 3. Available package manager commands.

Command Description
list packages [options] <FILTER> Prints all packages, optionally only those whose package name contains the text in <FILTER>.

Options:

  • -f: See their associated file.
  • -d: Filter to only show disabled packages.
  • -e: Filter to only show enabled packages.
  • -s: Filter to only show system packages.
  • -3: Filter to only show third party packages.
  • -i: See the installer for the packages.
  • -u: Also include uninstalled packages.
  • --user <USER_ID>: The user space to query.
list permission-groups Prints all known permission groups.
list permissions [options] <GROUP> Prints all known permissions, optionally only those in<GROUP>.

Options:

  • -g: Organize by group.
  • -f: Print all information.
  • -s: Short summary.
  • -d: Only list dangerous permissions.
  • -u: List only the permissions users will see.
list instrumentation List all test packages.

Options:

  • -f: List the APK file for the test package.
  • <TARGET_PACKAGE>: List test packages for only this app.
list features Prints all features of the system.
list libraries Prints all the libraries supported by the current device.
list users Prints all users on the system.
path <PACKAGE> Print the path to the APK of the given <PACKAGE>.
install [options] <PATH> Installs a package (specified by <PATH>) to the system.

Options:

  • -l: Install the package with forward lock.
  • -r: Reinstall an exisiting app, keeping its data.
  • -t: Allow test APKs to be installed.
  • -i <INSTALLER_PACKAGE_NAME>: Specify the installer package name.
  • -s: Install package on the shared mass storage (such as sdcard).
  • -f: Install package on the internal system memory.
  • -d: Allow version code downgrade.
  • -g: Grant all permissions listed in the app manifest.
uninstall [options] <PACKAGE> Removes a package from the system.

Options:

  • -k: Keep the data and cache directories around after package removal.
clear <PACKAGE> Deletes all data associated with a package.
enable <PACKAGE_OR_COMPONENT> Enable the given package or component (written as "package/class").
disable <PACKAGE_OR_COMPONENT> Disable the given package or component (written as "package/class").
disable-user [options] <PACKAGE_OR_COMPONENT>

Options:

  • --user <USER_ID>: The user to disable.
grant <PACKAGE_NAME> <PERMISSION> Grant a permission to an app. On devices running Android 6.0 (API level 23) and higher, may be any permission declared in the app manifest. On devices running Android 5.1 (API level 22) and lower, must be an optional permission defined by the app.
revoke <PACKAGE_NAME> <PERMISSION> Revoke a permission from an app. On devices running Android 6.0 (API level 23) and higher, may be any permission declared in the app manifest. On devices running Android 5.1 (API level 22) and lower, must be an optional permission defined by the app.
set-install-location <LOCATION> Changes the default install location. Location values:
  • 0: Auto—Let system decide the best location.
  • 1: Internal—install on internal device storage.
  • 2: External—install on external media.

Note: This is only intended for debugging; using this can cause applications to break and other undesireable behavior.

get-install-location Returns the current install location. Return values:
  • 0 [auto]: Lets system decide the best location
  • 1 [internal]: Installs on internal device storage
  • 2 [external]: Installs on external media
set-permission-enforced <PERMISSION> [true|false] Specifies whether the given permission should be enforced.
trim-caches <DESIRED_FREE_SPACE> Trim cache files to reach the given free space.
create-user <USER_NAME> Create a new user with the given <USER_NAME>, printing the new user identifier of the user.
remove-user <USER_ID> Remove the user with the given <USER_IDENTIFIER>, deleting all data associated with that user
get-max-users Prints the maximum number of users supported by the device.
利用ActivityManager

Within an adb shell, you can issue commands with the activity manager (am) tool to perform various system actions, such as start an activity, force-stop a process, broadcast an intent, modify the device screen properties, and more. While in a shell, the syntax is:

am <command>

You can also issue an activity manager command directly from adb without entering a remote shell. For example:

adb shell am start -a android.intent.action.VIEW

Table 2. Available activity manager commands

Command Description
start [options] <INTENT>
(选项里面没有-a  经试验就是打开一个activity)
Start an Activity specified by <INTENT>.

See the Specification for <INTENT> arguments.

Options are:

  • -D: Enable debugging.
  • -W: Wait for launch to complete.
  • --start-profiler <FILE>: Start profiler and send results to<FILE>.
  • -P <FILE>: Like --start-profiler, but profiling stops when the app goes idle.
  • -R: Repeat the activity launch <COUNT> times. Prior to each repeat, the top activity will be finished.
  • -S: Force stop the target app before starting the activity.
  • --opengl-trace: Enable tracing of OpenGL functions.
  • --user <USER_ID> | current: Specify which user to run as; if not specified, then run as the current user.
startservice [options] <INTENT> Start the Service specified by <INTENT>.

See the Specification for <INTENT> arguments.

Options are:

  • --user <USER_ID> | current: Specify which user to run as; if not specified, then run as the current user.
force-stop <PACKAGE> Force stop everything associated with <PACKAGE> (the app's package name).
kill [options] <PACKAGE> Kill all processes associated with <PACKAGE> (the app's package name). This command kills only processes that are safe to kill and that will not impact the user experience.

Options are:

  • --user <USER_ID> | all | current: Specify user whose processes to kill; all users if not specified.
kill-all Kill all background processes.
broadcast [options] <INTENT> Issue a broadcast intent.

See the Specification for <INTENT> arguments.

Options are:

  • [--user <USER_ID> | all | current]: Specify which user to send to; if not specified then send to all users.
instrument [options] <COMPONENT> Start monitoring with an Instrumentation instance. Typically the target <COMPONENT> is the form <TEST_PACKAGE>/<RUNNER_CLASS>.

Options are:

  • -r: Print raw results (otherwise decode<REPORT_KEY_STREAMRESULT>). Use with [-e perf true] to generate raw output for performance measurements.
  • -e <NAME> <VALUE>: Set argument <NAME> to <VALUE>. For test runners a common form is -e <testrunner_flag> <value>[,<value>...].
  • -p <FILE>: Write profiling data to <FILE>.
  • -w: Wait for instrumentation to finish before returning. Required for test runners.
  • --no-window-animation: Turn off window animations while running.
  • --user <USER_ID> | current: Specify which user instrumentation runs in; current user if not specified.
profile start <PROCESS> <FILE> Start profiler on <PROCESS>, write results to <FILE>.
profile stop <PROCESS> Stop profiler on <PROCESS>.
dumpheap [options] <PROCESS> <FILE> Dump the heap of <PROCESS>, write to <FILE>.

Options are:

  • --user [<USER_ID>|current]: When supplying a process name, specify user of process to dump; uses current user if not specified.
  • -n: Dump native heap instead of managed heap.
set-debug-app [options] <PACKAGE> Set application <PACKAGE> to debug.

Options are:

  • -w: Wait for debugger when application starts.
  • --persistent: Retain this value.
clear-debug-app Clear the package previous set for debugging with set-debug-app.
monitor [options] Start monitoring for crashes or ANRs.

Options are:

  • --gdb: Start gdbserv on the given port at crash/ANR.
screen-compat [on|off] <PACKAGE> Control screen compatibility mode of <PACKAGE>.

display-size [reset|<WxH>] Override emulator/device display size. This command is helpful for testing your app across different screen sizes by mimicking a small screen resolution using a device with a large screen, and vice versa.

Example:
am display-size 1280x800

display-density <dpi> Override emulator/device display density. This command is helpful for testing your app across different screen densities on high-density screen environment using a low density screen, and vice versa.

Example:
am display-density 480

to-uri <INTENT> Print the given intent specification as a URI.

See the Specification for <INTENT> arguments.

to-intent-uri <INTENT> Print the given intent specification as an intent: URI.

See the Specification for <INTENT> arguments.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值