4th Chapter 4 "Monkeyrunner Source Analysis" ADB protocol and Services: Introduction to ADB command line client use (original)

Source: Internet
Author: User
Tags system log

Heaven Zhuhai Branch Rudder note : Originally this series is to prepare a book, details please see the earlier blog "to seek partners to write the deep understanding of Monkeyrunner" books. But for many reasons, there is no wish. So the draft is shared here, so mistakes are unavoidable. If necessary, please refer to the following, the forwarding of the word also keep the end of each article source and other information.

From the previous sections we know that the ADB command line client is present with a command on the host side that the user can use to send a service request to the ADB server, The ADB server then determines whether the service request is a host service request or a local service request to determine if the request should be routed to the remote ADBD daemon for execution.

During the testing process, we often use the ADB command-line client to aid testing, so we will learn and practice the commands we often use.

ADB command-Lists all device information: "ADB devices-l".

The purpose of this command is to list the basic information of all connected Android devices, such as the device ID, With this ID we can specify the ID in the waitforconnection of the Monkeyrunner class to establish the connection between the Monkeyrunner background and the target Android machine when we write the script.

Figure 4-4-1 List all device information

ADB command-Specifies a device for communication: "Adb-s".

As the information listed above shows that there are multiple devices attached to the host, how can we send a command to the desired device, such as the interactive shell into the target device. At this point we can specify the device ID of the target device with the "-S" option to specify the target device you want to connect to to send the desired command, such as "Adb-s 192.168.1.103:5555 Shell". Shows the result of sending the command, and the red-circled part represents the interactive shell that has entered the target Android machine.

Figure 4-4-2 Sending a command to a specified target device

Of course, if there is only one device currently mounted, you may not know that any of ING devices will be able to send commands. However, if there is more than one device and no target device is specified, the command will be error-free because it does not know which device to send:

Figure 4-4-3 Sending a command to an error output without a specified device

ADB command-connects the target device via TCP.

Sometimes you do not want to connect directly to your console via USB cable, because the USB connection is physically connected, and during the test run you may accidentally move the USB connection and the entire test will need to start over. At this point you can consider using the TCP protocol to connect your target Android device. But before connecting, you need to first on the basis of USB cable connection to ensure that the target Android machine ADBD on the default TCP port 5555, otherwise the host side is not connected to the ADBD (of course you can also specify ADBD to listen to other TCP ports, just so, The host side needs to specify that the port is connected). By default, ADBD does not turn on this TCP port for monitoring.

We can send the "adb shell getprop service.adb.tcp.port" command to the target Android to check if ADBD has settings to listen to any TCP port, and the GETPROPR command of Android is to get a system property. As we have described in the previous chapter, the "Service.adb.tcp.port" System attribute specified here refers to the TCP port that the ADBD listens to. Of course, if you are familiar with Linux commands, you can also send the ' adb shell netstat|grep 5555 ' command to check if any of the services are currently listening on the port. Here are the output of the two commands when no listener is set:

Figure 4-4-4 Determine if ADBD has a listening TCP default port

This time we need to set the "Service.adb.tcp.port" property to the default listening port 5555, the command to send is "adb shell setprop service.adb.tcp.port 5555". When the listening port is set up, we can see if the port is working by the two commands above:

Figure 4-4-5 ADBD Default TCP listening port is open

The next step is to get the IP address of the Android target device, which can be obtained by sending the command "adb shell netcfg":

Figure 4-4-6 Obtaining the target Android IP address

Finally, we can use the "ADB Connect" command to connect to the specified IP address of the Android device, given the connection before and after the "ADB devices-l" listed in the connection of the device information comparison, you can see more than before the connection of an IP: Port is the device ID for Android device information:

Figure 4-4-7 Comparison of device list before and after connection

ADB command-Port forwarding

The user can forward a local port to the corresponding port of the target Android device via the port forwarding option of the ADB command line client, so that the local application can communicate directly with the local port, and its effect is to communicate with the remote corresponding port directly. For example, we can set the local 4939 port to the Viewserver service on the target Android device to listen for port 4939 forwarding, so that we can directly operate the local port for Viewserver communication. Shows how to forward the port with the ADB forward command, and then the "adb forward–list" command to list all connections that have port forwarding configured.

Figure 4-4-8 Port Forwarding command example

ADB command-Install the app

The installation package for the specified path can be installed on the target Android machine via the "adb Install" command, as shown in the example:

Figure 4-4-9 Installing the App command example

ADB command-Uninstall the app

An app that specifies the package name can be uninstalled via the "adb Uninstall" command. Through the "PM" Package Management Command "PM list packages" of the Android system, you can list all installed package names and then find the app packages that need to be uninstalled.

Figure 4-4-10 List all installed app packages

After you find an app package that needs to be uninstalled, you can uninstall it by using the "adb Uninstall" command, see:

Figure 4-4-11 Uninstall an app command example

ADB command-push file to target device

The "ADB push local Remote" command allows you to push one of the local locally files to the remote address of the target Android device. The following example is the command to push the local notepade.apk command to the remote device "/data/local/tmp/" under this directory, and finally through the "LS" command to list the file:

Figure 4-4-12 Push File Example

ADB command-drop-down file to local

Sometimes we need to download some files from the target Android device to the host side, such as when the system log needs to be analyzed. At this point, we can pull a file specified by the remote remoting path down to the location specified by local locally via the "ADB Pull remote local" command. Shows an example of a command that pulls the remote "/data/local/tmp/1.png" file down to the local current directory:

Figure 4-4-13 Push File Example

ADB command-View logcat records in real time

The "adb logcat" command allows you to view real-time debugging information for Logcat printing, so that we can debug system problems during development. A portion of the "adb logcat" command is printed out:

Figure 4-4-14 adb Logcat Output Example

ADB command-send Android shell command

The last command described is the "adb shell" command, in which we have seen its power. If the "adb shell" command is not followed by any other commands, it will be entered into the Android interactive Shell, which has no difference from the Android command-line terminal directly interacting with Android. If the "adb Shell" command is followed by a shell command of an Android system, the command returns immediately after execution and does not enter the interactive shell. It is not possible to describe all the commands in the Android system, and we can only describe them in the future when we encounter them.

——— to Be Continued ———

Heaven Zhuhai Branch Rudder
Public Number: Techgogogo
Weibo: Http://weibo.com/techgogogo
Csdn:http://blog.csdn.net/zhubaitian

4th Chapter 4 "Monkeyrunner Source Analysis" ADB protocol and Services: Introduction to ADB command line client use (original)

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.