01 Android Debug Bridge (adb)
The Android Debug Bridge (adb) is a versatile command line tool that allows you to communicate with your device.
The adb command can be used to perform a variety of device operations (such as installing and debugging applications) and provides access to the Unix shell (which can be used to run various commands on the device).
It is a client-server program that includes the following three components:
- client (computing): Used to send commands.
The client runs on the development computer. You can invoke the client from a command line terminal by issuing the adb command.
- Daemon (adbd): Run the command on the device.
The daemon runs as a background process on each device.
- server (computer): Manages communication between the client and the daemon.
The server runs as a background process on the development machine
How adb works
When you start one of the adb clients, the client willFirst check if there is an adb server process running. If not, it will start the server process.
The server binds to local TCP port 5037 upon startup and listens for commands from adb clients - all adb clients communicate with the adb server on port 5037.
The server then establishes connections to all running devices. It finds the emulator by scanning the odd numbered ports between 5555 and 5585 (the range for the first 16 emulators).
Once the server discovers the adb daemon (adbd), it establishes a connection to the appropriate port. Note that each emulator uses a sequential pair of ports - theEven numbered ports for console connections and odd numbered ports for adb connections.
for example:
Simulator 1, Console: 5554
Simulator 1, adb: 5555
Simulator 2, Console: 5556
Simulator 2, adb: 5557
in a similar fashion
as shown above, the emulator that connects to adb at port 5555 is the same emulator as the one whose console listens on port 5554.
Once the server has established connections to all devices, you can access them using adb commands. Because the server manages connections to devices and handles commands from multiple adb clients, you can access them from theAny client (or from a script) controls any device。
Enable adb debugging on the device
To use adb on a device connected via USB, you must enable USB debugging in the device's system settings (located under Developer Options)
On devices with Android 4.2 and higher, the Developer Options screen is hidden by default. To reveal it, go to Settings > About phone and tap the version number seven times. Return to the previous screen and find the Developer Options at the bottom.
On some devices, the Developer Options screen may be in a different location or named differently
Now you can connect the device via USB. You can connect to the device from the android_sdk/platform-tools/ Run adb devices on the directory to verify that the device is connected. If it is connected, you will see the device name listed as "devices"
Connect to the device via WLAN
In general, adb communicates with devices via USB, but you can alsoAfter completing some initial setup via USB, use adb over WLAN.The following is a summary of the information provided in the report.
- Connect the Android device and the adb host to the same WLAN network that both can access. Note that not all access points will work; you may need to use an access point whose firewall is properly configured to support adb.
- If you are connecting to a Wear OS device, turn off Bluetooth on the phone paired with the device.
- Use the USB cable to connect the device to the host computer.
- Set the target device to listen for TCP/IP connections on port 5555.
adb tcpip 5555
- Unplug the USB cable connected to the target device.
- Find the IP address of the Android device.
For example, for Nexus devices, you can find the IP address under Settings > About Tablet (or About Phone) > Status > IP Address. Or, for Wear OS devices, you can find the IP address under Settings > WLAN Settings > Advanced > IP Address.
- Connect to the device via the IP address.
adbconnect device_ip_address
- Confirm that the host computer is connected to the target device:
adb devices
Now you are ready to get started!
If the adb connection is disconnected:
- Make sure the host computer is still connected to the same WLAN network as the Android device.
- Reconnect by performing the adb connect step again.
- If the above does not solve the problem, reset the adb host:
adb kill-server
Then, start the operation from scratch.
Inquiry equipment
Before issuing adb commands, it is helpful to know which device instances are connected to the adb server. You can use the devices command to generate a list of connected devices
adb devices -l
In response, adb outputs the following status information for each device:
product key (software): A string created by adb to uniquely identify a device by its port number. Here is an example serial number: emulator-5554
state of affairs: The connection status of the device can be one of the following:
- offline:
Device not connected to adb or not responding
- device:
The device is now connected to the adb server.Please note that this state does not indicate that Android is fully booted and ready to run, as the system is still booting while the device is connected to adb. However, after booting, this is the normal operating state of the device.
- no device:
No device is connected.
clarification: If the -l option is included, the devices command tells you what the device is. This information is useful when you have more than one device connected to help you distinguish between them.
The following example shows the devices command and its output. There are three devices running. The first two lines of the list represent the emulator, and the third line represents the hardware devices connected to the computer.
Send commands to specific devices
If more than one device is running, when you issue the adb command, theThe target device must be specified. To do this, use the devices command to obtain the serial number of the target device. After obtaining the serial number, use theThe -s option and the adb commandto specify the serial number
In the following example, a list of connected devices was obtained, and then the serial number of one of the devices was used to install the
Installation of applications
You can use adb's install command to install APK on the emulator or on a connected device:
adb install path_to_apk
If the app is already installed on the device, override the installation with the -r parameter, as follows
adb install -r path_to_apk
Setting up port forwarding
You can use the forward command to set up any port forwarding, which forwards requests on a specific host port to other ports on the device. The following example sets up forwarding from host port 6100 to device port 7100:
adb forward tcp:6100 tcp:7100
The following example sets up forwarding from host port 6100 to local:logd.
adb forward tcp:6100 local:logd
Copying files to/from a device
You can use thepullcap (a poem)pushcommands copy files to and from the device. Unlike the install command (which only copies APK files to a specific location), the pull and push commands let you copy arbitrary directories and files to any location on the device
To copy a file or directory and its subdirectories from a device, use the following command:
adb pull remote local
Example:
adb pull /data/local/tmp/ C:\Users\Lxg\Desktop
To copy a file or directory and its subdirectories to the device, use the following command:
adb push local remote
Example:
adb push C:\Users\Lxg\Desktop\ /data/local/tmp
Issue shell commands
You can use shell commands to issue device commands through adb, or you can launch an interactive shell.To issue individual commands, use shell commands, as shown below:
adb [-d |-e | -s serial_number] shell shell_command
To start an interactive shell on the device, use the shell command, as shown below:
adb [-d | -e | -s serial_number] shell
To exit the interactive shell, press Ctrl + D or type exit
Android provides most of the common Unix command line tools. For a list of available tools, use the following command:
adb shell ls /system/bin
Calling the Activity Manager (am)
In the adb shell, you can use the Activity Manager (am) utility to issue commands to perform a variety of system actions, such as starting an Activity, force stopping a process, broadcasting an intent, modifying device screen properties, and more. In the shell, the syntax is as follows:
am command
You can also issue Activity Manager commands directly from adb without going to a remote shell. for example:
adb shell am start -a