Android tool-Use of ADB

Source: Internet
Author: User
Tags system log unix domain socket
Android debugging Bridge

Http://www.androidin.com/docs/reference/adb.html#sqlite

 

Android debugging bridge (ADB) is a versatile tool that helps you manage the status of devices or simulators.

You can add ADB using the following methods:

  • Run shell commands on the device
  • Manage simulators or devices through port forwarding
  • Copy files from simulators or devices

The following describes the common use of ADB.

Contents
Summary
Issue ADB command
Query Simulators/device instances
Send commands to specific Simulators/device instances
Install software
Forwarding Port
Import or copy files from Simulators/devices
ADB command list
Start shell commands
Connect to the database by running sqllite3 on a remote shell Client
UI/Software Test Procedure monkey
Other shell commands
Enable logcat logs
Use the logcat command
Filter log output
Control log output format
View available log Buffers
View stdout and stderr
Logcat command list
Stop ADB Service

Summary

The android debugging system is a customer service system that consists of three components:

  • A client that runs on the computer you use to develop programs. You can use the ADB command on the shell to start the client. Other Android tools such as ADT plug-in and ddms can also generate ADB clients.
  • The server that runs as the background process on the machine you use for sending. The server is responsible for managing the communication between the client and the ADB daemon running on the simulator or device ..
  • A daemon that runs on a simulator or device in the form of a subsequent process )..

When you start an ADB client, the client first checks whether an ADB service process is running. If not, start the service process. When the server runs, the ADB server binds the local TCP port 5037 and listens for commands sent from the ADB client.-All the ADB clients use port 5037 to communicate with the ADB server.

Then the server establishes a connection to all running simulators or device instances. It locates all simulators or devices by scanning all the odd ports in the range of 5555 to 5585. Once the server finds the ADB daemon, it establishes a connection to the port. Note that any simulator or device instance will obtain two consecutive ports-an even port is used to connect to the corresponding console, and an odd port is used to respond to the ADB connection. For example:

Simulator 1. Console: port 5554
Simulator 1 and ADB port 5555
Console: port 5556
ADB port 5557...

As shown above, the simulator instance connects to ADB through port 5555, just like connecting to the console using port 5554.

Once the server establishes a connection with all simulator instances, you can use the ADB command to control and access the instance. Because the server manages connections to simulators/device instances, and controls the processing of commands from multiple ADB clients, you can control any simulator or device instance through any client (or script.

The following sections describe how to use ADB through commands and manage the status of simulators/devices. Note that if you use eclipse with the ADT plug-in to develop Android programs, you do not need to use ADB through the command line. The ADT plug-in has transparently integrated ADB into eclipse. Of course, if necessary, you can still directly use ADB, such as debugging.

Issue ADB command

Android command: You can publish the android command on the command line or script on your development machine. The usage is as follows:

adb [-d|-e|-s <serialNumber>] <command> 

When you issue a command, the system enables the android client. The client is not related to the simulator instance, so if the dual server/device is running, you need to use-dOption to determine the target instance for the command to be controlled. For more information about using this option, you can view the terms control commands of the simulator/device instance.

Query Simulators/device instances

Before releasing the ADB command, it is necessary to know what kind of simulator/device instance is connected to the ADB server. You can usedevicesCommand to obtain a series of associated Simulators/devices:

adb devices

• In response, ADB provides the following status information for each instance:

  • • Serial number-a string created by ADB, which uses its own control port<type>-<consolePort>Uniquely identifies a simulator/device instance. The following is an example of a serial number:emulator-5554
  • There are three instance connection statuses:
    • offline-This instance is not connected to ADB or cannot respond.
    • device-The instance is connected to the ADB server. Note that this status does not indicate that the system is running and operating the Android system. Therefore, this instance is connected to ADB when the system is running. However, after the system is started, it is a normal running status of the simulator/device.

The output format of each instance is fixed as follows:

[serialNumber] [state]

Below is a demonstrationdevicesCommand and output example:

$ adb devices
List of devices attached
emulator-5554  device
emulator-5556  device
emulator-5558  device

If no simulator/device is running, ADB returnsno device.

Send commands to specific Simulators/device instances

If multiple Simulators/device instances are running, you must specify a target instance when releasing the ADB command. Use-sCommand. In use-sOption is

adb -s <serialNumber> <command> 

As shown above, the target instance is specified for a command, which uses the serial number allocated by ADB. You can usedevicesCommand to obtain the serial number of the running simulator/device instance

Example:

adb -s emulator-5556 install helloWorld.apk

Note that if no target simulator/device instance is specified-sIf this command is used, ADB will generate an error.

Install software

You can use ADB to copy an application from your development computer and install it on a simulator/device instance. UseinstallCommand. ThisinstallThe Command requires you to specify the path of the. APK file you want to install:

adb install <path_to_apk>

For more information about how to create a .apk file that can be installed on the simulator/device, see Android asset packaging tool (aapt ).

Note that if you are using Eclipse IDE and have installed the ADT plug-in, you do not need to directly use ADB (or aapt) to install the application on the simulator/device. Otherwise, the ADT plug-in handles application packaging and installation on your behalf.

Forwarding Port

AvailableforwardCommand to forward any port-a specific host port of a simulator/device instance to another port. The following demonstrates how to establish forwarding from host port 6100 to simulator/device port 7100.

adb forward tcp:6100 tcp:7100

Similarly, you can use ADB to create an abstract Unix domain interface. The process is as follows:

adb forward tcp:6100 local:logd 

Import or copy files from Simulators/devices

You can use ADBpull,pushCommand to copy a file to or from a data file of a simulator/device instance.installThe command only copies a .apk file to a specific location,pullAndpushCommands allow you to copy arbitrary directories and files to any location of a simulator/device instance.

Copy a file or directory from a simulator or device and use the following command ):

adb pull <remote> <local>

Copy the file or directory to the simulator or device and run the following command)

adb push <local> <remote>

In these commands,<local>And<remote>The path to the target file/directory on the development machine (local) and the simulator/device instance (remote) respectively.

The following is an example ::

adb push foo.txt /sdcard/foo.txt

ADB command list

The following table lists all commands supported by ADB and describes their meanings and usage.

Category Command Description Comments
Options -d Manage Abd only through USB interface. An error is returned if it is not just managed using a USB interface.
-e The ADB is managed only through the simulator instance. If it is not just managed by the simulator instance, an error is returned.
-s <serialNumber> Use the allowed command numbers of the simulator/device to send commands to manage the ADB (for example, "emulator-5556 "). If no number is specified, an error is returned.
General devices View the list of all devices connected to simulators/devices. View querying for emulator/device instances for more information.
help View All commands supported by ADB ..  
version View the version serial number of ADB.  
Debug logcat [<option>] [<filter-specs>] Output log data to the screen.  
bugreport View the bug report, as shown in figuredumpsys,dumpstate, AndlogcatInformation.  
jdwp View the available jdwp information of the specified facility. Availableforward jdwp:<pid>Port ing information to connect to the specified jdwp process. For example:
adb forward tcp:8000 jdwp:472
jdb -attach localhost:8000
Data install <path-to-apk> Install android as (you can specify the complete path for the simulator/idea data file .apk ).  
pull <remote> <local> Copy the specified file from the simulator/facility to the computer.  
push <local> <remote> Copy the specified file from the computer to the simulator/device.  
Ports and networking forward <local> <remote> Remotely connect to the simulator/facility through the socket method using the locally specified port The following information is required:

  • tcp:<portnum>
  • local:<UNIX domain socket name>
  • dev:<character device name>
  • jdwp:<pid>
ppp <tty> [parm]... Run PPP through USB:

  • <tty>-The tty for PPP stream. For exampledev:/dev/omap_csmi_ttyl.
  • [parm]... & Mdash zero or more PPP/pppd options, suchdefaultroute,local,notty, Etc.

You must be reminded that you cannot automatically start the PDP connection.

 
Scripting get-serialno View the serial number of the ADB instance. View querying for emulator/device instances for more information.
get-state View the current status of the simulator/facility.
wait-for-device If the device is offline, it will not be executed, that is, the instance status isdevice. You can reprint the command in the ADB command in advance. The command in the command is not executed before the simulator/device is connected. The example is as follows:

adb wait-for-device shell getprop

Note that these commands do not start ADB before all systems start up. Therefore, you cannot execute other commands before all systems start up. For example: UseinstallAndroid packages are required only when the system is fully started. For example:

adb wait-for-device install <app>.apk

The preceding command is executed only when the simulator/device is connected to the ADB service, and an error occurs before the Android system is fully started.

Server start-server Select whether the service starts the ADB service process.  
kill-server Terminate the ADB service process.  
Shell shell Use remote shell commands to Control Simulators/device instances. View more information for more information.
shell [<shellCommand>] Connect to the simulator/facility and execute shell commands. After the command is executed, exit the remote shell terminal.

Start shell commands

ADB provides the shell end, through which you can run various commands on the simulator or device. These commands are saved in binary format in the file system of the local simulator or device:

/system/bin/...

Whether or not you are fully connected to the ADB remote shell of the simulator/device, you canshellCommand to execute the command.

When the remote shell is not fully accessedshellCommand to execute a command:

adb [-d|-e|-s {<serialNumber>}] shell <shellCommand>

This is used when remote shell is not used in simulators/devices.shellLife:

adb [-d|-e|-s {<serialNumber>}] shell

OperationCTRL+DOrexitYou can exit the shell remote connection.

The following sections will show you more about shell commands.

Connect to the database by running sqllite3 on a remote shell Client

Through the remote shell end of ADB, you can use the android soft sqlite3 command program to manage the database.sqlite3The tool contains many commands, such:.dumpDisplays the table content,.schemaDisplays the SQL create result set of an existing tablespace. Sqlite3 also allows you to remotely execute SQL commands.

Passsqlite3Log on to the remote shell of the simulator according to the methods in the previous sections, and then start the tool to usesqlite3Command. Whensqlite3After startup, you can specify the full path of the database you want to view. The simulator/device instance stores the sqlite3 database in the folder./data/data/<package_name> /databases /.

Example:

$ adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...

sqlite> .exit

When you start sqlite3, you can sendsqlite3Command. UseexitOrCTRL+DLog out of the remote shell client of ADB.

UI/Software Test Procedure monkey

When a monkey program is running on a simulator or device, if the user starts a system-level event, such as a click, touch, gesture, or system-level event, it generates a random pulse, therefore, you can use Monkey to load and test your software with random repetition.

The simplest method is to use the following command to use Monkey. This command will start your software and trigger 500 events.

$ adb shell monkey -v -p your.package.name 500

For more information about the monkey command, see UI/Application Exerciser monkey documentation page.

Document page

Other shell commands

The following table lists some adbshell commands. If you need all the commands and programs, you can start the simulator instance and useadb -helpCommand.

adb shell ls /system/bin

For most departments, help is available.

Shell Command Description Comments
dumpsys Clear system data N on the screen. The Dalvik debug monitor service (ddms) tool provides complete debugging and ,.
dumpstate Clears the status of a file.
logcat [<option>]... [<filter-spec>]... Start the information log and output it to the screen.
dmesg Output The main debugging information to the screen.
start Start or restart a simulator/device instance.  
stop Disable a simulator/device instance.  

Enable logcat logs

The android logging system allows you to record and view system debugging information. Logs are recorded from various software and system buffers.logcatCommand to view and use.

Use the logcat command

You can uselogcatCommand to view the content of the System Log Buffer:

[adb] logcat [<option>] ... [<filter-spec>] ...

Please refer to the listing of logcat command options, which provides a detailed description of the logcat command.

You can also use it on your computer or on the remote ADB shell terminal running on the simulator/device.logcatCommand, you can also view the log output on your computer.

$ adb logcat

You can also use the following method:

# logcat

Filter log output

Each Android log output has a tag and its priority.

  • The log tag is a brief identifier of the original information of the system component. (For example, "View" is to view the system tag ).
  • The priority has the following sets, which are arranged smoothly from low to high:
    • V-Verbose (lowest priority)
    • D-Debug
    • I-Info
    • W-Warning
    • E-Error
    • F-Fatal
    • S-Silent (highest priority, on which nothing is ever printed)

You can see the information in the first two columns when running logcat.logcatLabel list and priority level, which are marked as follows:<priority>/<tag>.

The following is an example of logcat output. Its priority is I, and the label is activitymanage:

I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}

To enable log output to reflect the management level, you can also use filters to control log output. Filters can help you describe the system's tag level.

The filter statement is described in the following format:tag:priority ...,tagIndicates a tag,priorityIs the lowest level of the label report. The log priority can be obtained from the above tag. You can write it multiple times in the filter.tag:priority.

All of these instructions end with a blank space. The following example shows a column that supports all log information, except for those with "activitymanager" and "info" or above, and those with "MyApp" and "debug" or higher priorities. Level. The priority report is tag.

adb logcat ActivityManager:I MyApp:D *:S

Last element of the above expression*:S, Is to set all labels to "silent", all logs only show "View" and "MyApp",*:SAnother use is to ensure that the log output is restricted according to the instructions of the filter, and make the filter output to the log as an item.

The following filter statement shows the log information with a priority of warning or higher:

adb logcat *:W

If you runlogcatCompared with remote adbshell, you can also use environment variablesANDROID_LOG_TAGS: Enter a parameter to set the default filter.

export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

Note thatANDROID_LOG_TAGSIf the filter runs through a remote shelllogcatOr useadb shell logcatThe simulator/device cannot output logs.

Control log output format

Log information includes many metadata fields, including tags and priorities. You can modify the log output format to display a specific metadata domain. You can use-vOption to obtain information about the formatted output log.

  • brief-Display priority/Tag and PID of originating process (the default format ).
  • process-Display PID only.
  • tag-Display the priority/tag only.
  • thread-Display process: thread and priority/tag only.
  • raw-Display the raw Log message, with no other metadata fields.
  • time-Display the date, invocation time, priority/tag, and PID of the originating process.
  • long-Display all metadata fields and separate messages with a blank lines.

Whenlogcat, You can-vTo specify the output format:

[adb] logcat [-v <format>]

UsethreadTo generate the log format:

adb logcat -v thread

Note that you can only-vSpecifies the output format.

View available log Buffers

The android log system has a circular buffer, and not all log systems have a default circular buffer. To obtain the log information, you need-bOption to startlogcat. To use the cyclic buffer, you need to check the remaining cyclic buffer period:

  • radio-View information about the buffer.
  • events-View the Event-related buffer.
  • main-View major log Buffers

-bOption usage:

[adb] logcat [-b <buffer>]

The following example shows how to view the log buffer that contains radio and telephony information:

adb logcat -b radio

View stdout and stderr

By default, the android system hasstdoutAndstderr(System.outAndSystem.err) Output/dev/nullIn the process of running Dalvik Vm, a system can back up log files. In this case, the system will usestdoutAndstderrAnd priority I. To record log information

In this way, specify the output path, stop the running simulator/device, and then usesetpropCommand Remote input log

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start

The system is retained until you disable the simulator/device. You can add/data/local.propYou can use the default settings on the simulator/device.

Logcat command list
Option Description
-b <buffer> Load an available log buffer for viewing, sucheventAndradio. The default value ismain. For details, see viewing alternative log buffers.
-c Clear the logs on the screen.
-d Output logs to the screen.
-f <filename> Specify<filename>The default value isstdout.
-g Output the specified log buffer and exit after the output.
-n <count> Set the maximum number of logs<count>. The default value is 4.-rOption.
-r <kbytes> Each<kbytes>Logs are output. The default value is 16.-fOption.
-s Set the default filtering level to silent.
-v <format> Set the log input format. The default format isbriefFor more supported formats, see controlling log output format.

Stopping the ADB Server

In some cases, you may need to terminate the android debugging system and restart it. For example, if the android debugging system does not respond to a command, you can terminate the server and restart it. This may solve this problem.

Usekill-serverThe ADB server can be terminated. You can use any commands issued by ADB to restart the server.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.