SoFunction
Updated on 2024-11-21

What does adb do in python?

ADB is a tool in the Android SDK, using ADB you can directly manipulate and manage Android emulators or real Andriod devices.

The main functions of ADB are.

1. Run Shell (command line) on your Android device

2、Manage the port mapping of the emulator or device

3. Upload/download files between computers and devices

4. Install the local APK software on your PC to the Android emulator or device

Python has two ways to invoke the adb command, one () and one ().

The difference between the two is that the former can't get the return value and the latter can.

() returns a file object, which can be read directly using the read() method.

Content Extension:

 How python calls the adb command

Executing cmd commands in Python can be done with two modules, os and subprocess. The difference is that os is blocking and subprocess is non-blocking, so it is more appropriate for us to use subprocess. Next, let's look at a command that queries for connected devices to see how it's written in python. The command used is adb devices.

import subprocess
order='adb devices' # Get connected devices
pi= (order,shell=True,stdout=)
print () # Print results

The actual print results, you can see that the current computer is connected to three devices. Here we need to explain the results of adb devices command is a one-time return, so we use the read method to read the data is not a problem, however, adb commands there are some real-time return results, such as the output of the cell phone log of the command logcat, the results will be constantly printed out the current device operation log information content, this type of command we need to get the printout of the results of the read method, if we still use the read method, waiting for the results of the return time will be very long, here we have to read a different method. python if we need to get the results of the printout, if you still use the read method, waiting for the results of the return time will be very long, here we have to change the method to read the results, write the following.

import subprocess
order='adb logcat'
pi= (order,shell=True,stdout=)
for i in iter(,'b'):
print I

This print effect, as in the cmd operation consistent, real-time print out the log information. Here we use the readline method, in fact, this writing method is similar to our read file, a single line read and all the contents of the read. Because the current pyapp framework has been basically written, so the idea of writing this article to share some python in dealing with some of the insights on the adb command, as far as python in the call adb command on the difference between the main two points, the ultimate goal is that we need to find the function of the command to obtain the results of the data, and then go to the return of these python processing data to achieve the purpose of automated testing. We have to use good adb commands, but also pay attention to the use of each command with a variety of parameters, such as the implementation of the pyapp is to support multi-device connection, so we are targeting a cell phone adb command operation, we need to bring -s plus the device number, indicating the operation of the specific device, or else the command will be reported as an error.

For example, if we target a device to perform a tap operation, the command should be written like this: adb -s 49dsd4554wdsa shell input tap 600 900, where '49dsd4554wdsa' is the device number and '600 900' the tap screen coordinates. So you can see that with the addition of -s it is easy to operate multiple devices at the same time.

to this article on python adb what function of the article is introduced to this, more related python adb function detailed content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!