Android ADB use detailed article

Source: Internet
Author: User
<span id="Label3"></p><p><p>In the Android development environment, ADB is our most frequently used debugging tool for Android development, and its use is of course what our Android developers have to Master.</p></p><p><p><span style="font-size: medium;"><strong>ADB overview</strong></span></p></p><p><p>Android Debug Bridge,android Debug bridge, abbreviated as adb, is a universal tool for managing emulator or real state, using the Client-server model, consisting of three parts:</p></p> <ol> <ol> <li>Client part, running on a development computer, you can run ADB commands on the command line to invoke the client, and Android tools such as the ADB plugin and DDMS can also invoke the ADB Client.</li> <li>The Server-side section is a background process running on a development computer that manages the Client's communication with the daemon running in the emulator or the real Machine.</li> <li>Daemon section, running in the background of the emulator or PHONE.</li> </ol> </ol><p><p>When the ADB client is started, the client first detects whether the ADB server process is running and, if not, starts the Server. When the server starts, it binds to the local TCP5037 port and listens for commands sent from the ADB client-all ADB clients use port 5037 to communicate with the ADB server.</p></p><p><p>next, the server is connected to all running emulators or Phones. It searches for an emulator or phone by scanning an odd number port between 5555-5585, and once the ADB daemon is discovered, it connects through this port. It is necessary to note that each simulator or phone uses a pair of ordered ports, an even number port for the console connection, an odd number port for the ADB connection, for example:</p></p><p><p><span><span>Emulator 1, console:5554<br><span><span>Emulator 1, adb:5555<br><span><span>Emulator 2, console:5556<br><span><span>Emulator 2, adb:5557</span> ...</span></span></span></span></span></span></span></p></p><p><p>That is, if the emulator is connected to ADB on port 5555, its connection to the console is 5554 ports.</p></p><p><p>When the server is connected to all the emulators, it can be controlled or accessed using the ADB command. Because the server manages the connection and can receive commands from multiple adb clients, any emulator or mobile device can be controlled from any client or script.</p></p><p><p>These ADB commands that you can use to manage emulators or phones are described below. If you are developing an Android application in Eclipse and with an ADT plugin installed, you do not need to use ADB from the command line, and the ADT plugin has provided transparent Integration. however, you can use ADB directly when you need to debug, and so On.</p></p><p><p><span style="font-size: medium;"> <strong>using the ADB command</strong></span></p></p><p><p>The use of the ADB command from the command line or script file of the development computer is:</p></p><p><p><span><span>adb [-d|-e|-s <serialnumber>] <command></span></span></p></p><p><p>When used, the program calls the ADB Client. Because the ADB client does not need to be associated with any emulator, if more than one emulator or phone is running, you need to use the-d parameter to specify which one to Manipulate.</p></p><p><p><span style="font-size: medium;"><strong>Query emulator or Phone status</strong></span></p></p><p><p>Understanding the ADB server-side Connection Simulator or phone can help to better use the ADB command, which can be enumerated by the devices Command:</p></p><p><p><span>ADB devices</span></p></p><p><p>The result of the execution is that ADB outputs the following status information for each device:</p></p><p><p>Serial number (serialnumber)-a string created by ADB using the console number that uniquely identifies an emulator or mobile device, in the format < device type >-< port number, for example: emulator-5554</p></p><p><p>State-the connection state, whose value is:</p></p><p><p>Offline-not connected or not responding</p></p><p><p>Device-has been connected to a service provider. Note that this status does not mean that the Android system has been fully booted up, the system is ready to connect to the adb, but this state is normal operational status.</p></p><p><p>The output form of each device is as Follows:</p></p><p><p><span>[serialnumber] [state]</span></p></p><p><p>The following is the devices command and its execution result:</p></p><p><p><span>$ adb devices<br><span>List of devices attached<br><span>emulator-5554 Device<br><span>emulator-5556 Device<br><span>emulator-5558 Device</span></span></span></span></span></p></p><p><p>If no emulator or phone is running, the status returns no Device.</p></p><p><p><span style="font-size: medium;"> <strong>operation of the specified simulator or phone</strong></span></p></p><p><p>If you have more than one emulator or phone running, you need to specify the target device when using the ADB command, which can be achieved by using THE-S option parameter, which is:</p></p><p><p><span>adb-s <serialNumber> <command></span></p></p><p><p>That is, you can specify a specific target using the serial number in the ADB command, and the Devices command mentioned earlier can implement the query Device's serial number Information.</p></p><p><p>For example:</p></p><p><p><span>adb-s emulator-5556 Install helloworld.apk</span></p></p><p><p>Note that if you use-s and do not specify a device, the ADB will Error.</p></p><p><p><span style="font-size: medium;"> <strong>Install the application</strong></span></p></p><p><p>You can use ADB to copy applications from your development computer and install them on your emulator or phone, using the install command, where you must specify the path to the. apk file to Install:</p></p><p><p><span>adb install <path_to_apk></span></p></p><p><p>Note that if you are using an Eclipse development environment that has an ADT plug-in installed, you do not need to use ADB or the AAPT command to install the application directly, and the ADT plug-in can do this automatically.</p></p><p><p><span style="font-size: medium;"> <strong>Forwarding Port</strong></span></p></p><p><p>You can use the forward command to go to the Port-forward requests on a specific port to different ports on the emulator or PHONE. The following example goes from Port 6100 to port 7100:</p></p><p><p><span>adb forward tcp:6100 tcp:7100</span></p></p><p><p>You can also use the unix-named socket id:</p></p><p><p><span>adb forward tcp:6100 local:logd</span></p></p><p><p><span style="font-size: medium;"> <strong>transfer files with simulator or phone</strong></span></p></p><p><p>You can use the pull and push commands of ADB to copy files from an emulator or phone, or to copy files to an emulator or PHONE. Unlike the install command, it can only copy. apk files to a specific location, and the pull and push commands can copy any folder and file to any location on the emulator or PHONE.</p></p><p><p>Copy a file or folder (recursive) from the emulator or phone using:</p></p><p><p><span>adb pull <remote> <local></span></p></p><p><p>Copy a file or folder (recursive) to the emulator or phone using:</p></p><p><p><span>adb push <local> <remote></span></p></p><p><p>In this command <local> and <remote> refer to the path of the file or folder, on the development computer is local, on the simulator or mobile phone is Remote.</p></p><p><p>For example:</p></p><p><p><span>adb push Foo.txt/sdcard/foo.txt</span></p></p><p><p><span style="font-size: medium;"> <strong>Execute shell command</strong></span></p></p><p><p>The ADB provides a shell to run a variety of commands on the emulator or phone, the binary form of these commands in this path:</p></p><p><p>/system/bin/...</p></p><p><p>You can use the shell command to execute regardless of whether you enter the ADB remote Shell.</p></p><p><p>A single command can be executed in the following format without entering the remote Shell:</p></p><p><p><span>adb [-d|-e|-s {<serialnumber>}] Shell <shellCommand></span></p></p><p><p>Start the remote shell using the following format:</p></p><p><p><span>adb [-d|-e|-s {<serialnumber>}] Shell</span></p></p><p><p>Use Ctrl+d or exit to terminate the session when exiting the remote Shell.</p></p><p><p>The following is more information about the shell commands that you can Use.</p></p><p><p><span style="font-size: medium;"> <strong>checking the Sqlite3 database from a remote shell</strong></span></p></p><p><p>With the remote shell, you can use the Sqlite3 command-line program to manage the SQLite database created by your Application. The Sqlite3 tool contains a number of useful commands, such as. dump for outputting the contents of the TABLE,. schema is used to output SQL CREATE statements for tables that already exist. And the tool also provides the ability to perform SQLite commands Online.</p></p><p><p>When using sqlite3, enter the remote shell of the emulator as described by Wen, and then use the Sqlite3 command. You can also specify the full path of the database when calling sqlite3. The SQLite3 database is stored under The/data/data/<package_name>/databases/path.</p></p><p><p>Example:</p></p><p><p><span>$ adb-s emulator-5554 Shell<br><span># Sqlite3/data/data/com.example.google.rss.rssexample/databases/rssitems.db<br><span>SQLite version 3.3.12<br><span>Enter '. help ' for instructions<br><span>.... enter commands, then quit ...<br><span>sqlite>. Exit</span></span></span></span></span></span></p></p><p><p>Once Sqlite3 is run, you can use the Sqlite3 command to exit and return to the remote shell to use Exit or Ctrl+d.</p></p><p><p><span style="font-size: medium;"> <strong>using Monkey for UI or application testing</strong></span></p></p><p><p>Monkey is a program that runs on a simulator or phone, simulating operations by generating pseudo-random, numerous System-level user event streams, including clicks, touches, gestures, and so On. This allows for stress testing of applications that are under development through random responses.</p></p><p><p>The simplest way to use monkey is through the following command line, which can run a specified application and send 500 pseudo-random events to it.</p></p><p><p><span>$ adb shell monkey-v-p your.package.name</span></p></p><p><p><span style="font-size: medium;"> <strong>Other shell commands</strong></span></p></p><p><p>The following table lists a number of valid ADB shell commands, and the complete list can be obtained by starting the emulator and using the Adb–help command.</p></p><p><p><span>adb shell Ls/system/bin</span></p></p><p><p>Help is valid for most COMMANDS.</p></p> <table style="width: 400px; height: 374px;" border="1" cellspacing="1" cellpadding="1" align="center"> <tbody> <tr> <td><p>Shell <span style="font-family: 宋体;">Command</span></p></td> <td><p><span style="font-family: 宋体;">Describe</span></p></td> <td>Note</td> </tr> <tr> <td><p>Dumpsys</p></td> <td><p><span style="font-family: 宋体;">Display system data on the screen</span></p></td> <td>The Dalvik debug Monitor Service (DDMS) tool provides an intelligent debugging environment that is easier to use.</td> </tr> <tr> <td><p>Dumpstate</p></td> <td><p><span style="font-family: 宋体;">Output status to a file</span></p></td> <td></td> </tr> <tr> <td><p>Logcat [<option>] ... [<filter-spec>] ...</p></td> <td><span style="font-family: 宋体;">Output log Information</span></td> <td></td> </tr> <tr> <td><p>Dmesg</p></td> <td><p><span style="font-family: 宋体;">Output core debug information on the screen</span></p></td> <td></td> </tr> <tr> <td><p>Start</p></td> <td><p><span style="font-family: 宋体;">Start or restart the emulator or phone</span></p></td> <td></td> </tr> <tr> <td><p>Stop</p></td> <td><p><span style="font-family: 宋体;">Stop the simulator or phone</span></p></td> <td></td> </tr> </tbody> </table><p><p><br><span style="font-size: medium;"> <strong>viewing logs using Logcat</strong></span></p></p><p><p>The Android log system provides a mechanism for collecting and viewing debug information from numerous applications and system programs that are collected into a system loop buffer that can be logcat and filtered by commands.</p></p><p><p><span style="font-size: medium;"> <strong>using the Logcat command</strong></span></p></p><p><p>The general usage of the command Logcat to view and track the system log buffers Is:</p></p><p><p><span>[adb] logcat [<option>] ... [<filter-spec>] ...</span></p></p><p><p>You can use the Logcat command to view the log output in the development machine by using the remote Shell:</p></p><p><p><span>$ adb logcat</span></p></p><p><p>If you are using the command directly in the remote Shell:</p></p><p><p><span># Logcat</span></p></p><p><p><span style="font-size: medium;"> <strong>filtering log output</strong></span></p></p><p><p>Each log message has a tag and priority associated with it.</p></p><p><p>A token is a short string that identifies the source of the original message (for example, "View" originates from the display system).</p></p><p><p>The priority is the following character, in order from low to High:</p></p><p><p>V-detail (lowest Priority)</p></p><p><p>D-debug</p></p><p><p>I-information</p></p><p><p>W-warning</p></p><p><p>E-error</p></p><p><p>F-critical Error</p></p><p><p>S-no record (highest priority, nothing will be recorded)</p></p><p><p>By running logcat, You can get a list of tags and priorities used in a system, the first two columns of the observation list, and the format given is <priority>/<tag>.</p></p><p><p>Here is a log output message, the priority is "I", the token is "activitymanager":</p></p><p><p>I/activitymanager (585): starting activity:intent {action=android.intent.action ...}</p></p><p><p>If you want to reduce the content of the output, you can add filter expression restrictions, the filter can limit the system to output only the tag-priority combination of Interest.</p></p><p><p>The format of the filter expression is tag:priority ..., where tag is the token, priority is the minimum precedence, and the token identifies all messages that are greater than or equal to the specified priority level to be written to the log. You can also provide several such filters in a filter expression, separated by a space.</p></p><p><p>The example given below is only the output labeled "activitymanager" and the priority is greater than or equal to "Info" and the log labeled "MyApp" with a priority greater than or equal to "Debug":</p></p><p><p>ADB logcat activitymanager:i Myapp:d *:s</p></p><p><p>The last *:s of the above expression is used to set the log priority of all tags to S, which ensures that only the logs labeled "View" (the Translator should be activitymanager, the original text may be a clerical error) and "MyApp" are output, using *:s is a recommended way to ensure that the output conforms to the specified filter settings, so that the filter becomes a "whitelist" of the log Output.</p></p><p><p>The following expression shows all logs with priority greater than or equal to "warning":</p></p><p><p>ADB logcat *:w</p></p><p><p>If you are running LOGCAT on a development computer (as opposed to running a transport shell), you can also set the default filter expression through the Android_log_tags environment variable:</p></p><p><p>Export android_log_tags= "activitymanager:i Myapp:d *:s "</p></p><p><p>Note that if you run logcat in a remote shell or using the ADB shell Logcat command, android_log_tags will not be exported to the emulator or mobile Device.</p></p><p><p><span style="font-size: medium;"> <strong>Control Log Format</strong></span></p></p><p><p>Log messages have a number of metadata fields beyond the mark and priority, which can be controlled by modifying the output format to control the output, and The-v option, together with the contents listed below, controls the output field:</p></p><p><p>Brief-show Priority/tag and PID of the original process (default Format)</p></p><p><p>Process-show only Process PID</p></p><p><p>Tag-only Show Priority/tag</p></p><p><p>Thread-show only processes: Threads and Priorities/tags</p></p><p><p>Raw-display the original log information, no other metadata fields</p></p><p><p>Time-display date, call time, priority/tag, PID</p></p><p><p>Long-display all metadata fields and separate message contents with blank lines</p></p><p><p>You can use-v to start Logcat to control the log format:</p></p><p><p><span>[adb] logcat [-v <format>]</span></p></p><p><p>For example, use the thread output format:</p></p><p><p><span>adb logcat-v thread</span></p></p><p><p>Note You can specify only one format in The-v Option.</p></p><p><p><span style="font-size: medium;"> <strong>viewing alternative Log buffers</strong></span></p></p><p><p>The Android log system maintains multiple loop buffers for log messages, and not all messages are sent to the default buffers, and to view these additional buffers, you can use The-b option, which is the buffer you can specify:</p></p><p><p>Radio-viewing buffer messages that are contained in a wireless/phone-related</p></p><p><p>Events-viewing event-related Messages</p></p><p><p>Main-viewing the primary buffer (default Buffer)</p></p><p><p>Use of The-b option:</p></p><p><p><span>[adb] logcat [-b <buffer>]</span></p></p><p><p>For example, View the radio Buffer:</p></p><p><p><span>ADB logcat-b Radio</span></p></p><p><p><span style="font-size: medium;"> <strong>View stdout and stderr</strong></span></p></p><p><p>By default, the Android system sends stdout and STDERR (system.out and System.err) output To/dev/null. In the Dalvik VM process, the output can be copied to a log file, in which case the system uses stdout and stderr tags to write to the log with priority I.</p></p><p><p>To get the output in this way, you need to stop the running emulator or phone, and then use the command SetProp to allow output relocation, as shown in the following example:</p></p><p><p><span>$ adb shell stop<br><span>$ adb shell SetProp Log.redirect-stdio true<br><span>$ adb shell start</span></span></span></p></p><p><p>The system retains this setting until the emulator or phone exits, or you can add/data/local.prop to the device to make the device a default Configuration.</p></p><p><p><span style="font-size: medium;"> <strong>logcat List of command options</strong></span></p></p> <table style="width: 400px; height: 473px;" border="1" cellspacing="1" cellpadding="1" align="center"> <tbody> <tr> <td><p><span style="font-family: 宋体;">Options</span></p></td> <td><p><span style="font-family: 宋体;">Describe</span></p></td> </tr> <tr> <td><p style="text-align: left;"><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-b <buffer></span></code></p></td> <td>Load a different buffer log, for example <code><span style="font-size: 12pt;">event</span></code> or <code><span style="font-size: 12pt;">radio</span></code> <span style="font-family: 宋体;">. The <code><span style="font-size: 12pt;">main</span></code> buffer is the default entry, see viewing alternative Log Buffers.</span></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-c</span></code></p></td> <td><p><span style="font-family: 宋体;">Empty (refresh) all logs and exit</span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-d</span></code></p></td> <td><p><span style="font-family: 宋体;">Output the log on the screen and exit</span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-f <filename></span></code></p></td> <td><p><span style="font-family: 宋体;">Output the log to file <filename><span style="font-family: 宋体;">, the default output is Stdout.</span></span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-g</span></code></p></td> <td><p><span style="font-family: 宋体;">The size of the output log</span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-n <count></span></code></p></td> <td><p><span style="font-family: 宋体;">Set the maximum cyclic data <count><span style="font-family: 宋体;">, default is 4<span style="font-family: 宋体;">, requires the-r <span style="font-family: 宋体;">option</span> </span> </span></span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-r <kbytes></span></code></p></td> <td><p><span style="font-family: 宋体;">Per <kbytes> <span style="font-family: 宋体;">Circular log file, default is 16, requires <code><span style="font-family: ‘Courier New‘; font-size: 12pt;">-f</span></code> <span style="font-family: 宋体;">option</span> </span></span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-s</span></code></p></td> <td><p><span style="font-family: 宋体;">Set the default filter to no output</span></p></td> </tr> <tr> <td><p><code><span style="font-family: ‘Calibri‘, ‘sans-serif‘; font-size: 12pt;">-v <format></span></code></p></td> <td>To set the output format, the default is brief, a list of supported formats see controlling LOG output Format.</td> </tr> </tbody> </table><p><p><span style="font-size: medium;"> <strong>stop adb service</strong></span></p></p><p><p>In some cases, you may need to terminate and then restart the server-side process, such as when the ADB does not respond to commands, and can resolve the issue by restarting.</p></p><p><p>Use ADB kill-server to terminate the server and then restart using the ADB start-server command.</p></p><p><p>Android ADB use detailed article</p></p></span>

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.