The original address
A introduction of the adb
The Android Debug Bridge (ADB) is a command line tool that communicates with the device and performs various device operations, such as installing and debugging applications. Adb provides access to the Unix shell, which you can use to run various commands on the device.
Adb is a client-server program consisting of the following three components:
- Client: Used to send commands. The client runs on the development machine. You can invoke the client from the command line terminal by issuing adb commands.
- Daemon (ADBD) : Used to run commands on the device. The daemon runs as a background process on each device.
- Server: Used to manage communication between clients and daemons. The server runs as a background process on the development machine.
Adb is included with the Android SDK platform tools package. Download the package using the SDK manager, which will install it under android_sdk/platform-tools/.
How ADB works
When an ADB client starts, the client first checks to see if any ADB server processes are running. If not, start the server process. When started, the server binds to local TCP port 5037 and listens for commands issued by adb clients. All ADB clients communicate with the ADB server through port 5037.
The server then establishes connections with all running devices. It finds emulators by scanning for odd-numbered ports between 5555 and 5585 (the range used by the first 16 emulators). Once the server discovers the ADB daemon (ADBD), it establishes a connection with the appropriate port.
Once the server is connected to all the devices, you can access them using ADB commands.
Configure the ADB environment
Adb is stored in the android_sdk/platform-tools/ directory of the android SDK by default. If you want to use Android Studio’s Terminal console directly, you need to configure the environment first.
Open the terminal and enter the following command:
cd ~
touch .bash_profile
open -e .bash_profile
Copy the code
In the configuration page that pops up, add the path of your Android platform-tools. My example is as follows:
export PATH=${PATH}:/Users/ystm1/Library/Android/sdk/tools:/Users/ystm1/Library/Android/sdk/platform-tools
Copy the code
Save exit, now remember to update the configuration to take effect:
source .bash_profile
Copy the code
Note:
If you are a ZSH user, this configuration may be a one-off for you, and you may find my ADB command error “ZSH: command not found: adb” after you restart your computer. Don’t panic because you haven’t configured config.zshrc (ignored by non-ZSH users)
1) Enter open. ZSHRC in the terminal
2) If one already exists, enter: open.zshrc-e
3) Add source ~./bash_profile to the last line of the open file
4) Enter: source ~/.zshrc to make it take effect immediately
Common ADB commands
4.1 help command
View a detailed list of all supported ADB commands:
adb --help
Copy the code
4.2 devices command
The devices command generates a list of connected devices.
adb devices -l
Copy the code
Results:
(base) ystm1 @ ystm1 ~ % adb devices - lList of devices attached192.168.1.110:5556 device product: dipper model: MI_8 Device: Dipper transport_id:290192.168.1.124:5558 Device Product :PD1616 Model :vivo_X9 Device :PD1616 transport_id:296Copy the code
The following status information:
- Serial number: A string created by ADB to uniquely identify a device by port number.
- State: The connection state of the device can be one of the following:
offline
: Device is not connected to ADB or is not responding.device
: The device is now connected to the ADB server. Note that this status does not mean that the Android system is fully up and running, as the system is still up when the device connects to ADB. However, after startup, this will be the normal operating state of the device.no device
: No device is connected.
- Note: If you include
-l
Options,devices
The command tells you what the device is. This information is useful when you connect multiple devices to help you distinguish them.
4.3 install command
The install command installs APK on the emulator or connected device
adb install path_to_apk
Copy the code
4.4 shell command
Shell commands issue device commands through ADB and can also launch interactive shells. To issue a single command, use a shell command, as shown below:
adb [-d |-e | -s serial_number] shell shell_command
Copy the code
To launch the interactive shell on the device, use the shell command as follows:
adb [-d | -e | -s serial_number] shell
Copy the code
To exit the interactive shell, press Ctrl + D or type Exit.
Android provides most of the common Unix command-line tools. To view a list of available tools, use the following command:
adb shell ls /system/bin
Copy the code
4.5 Activity Manager (AM)
adb shell am start -a android.intent.action.VIEW
Copy the code
Activity manager (AM), call package manager (PM) and call device policy manager (DPM) are not used much, you can refer to the official documentation when necessary.
5 Best Practices · Wireless debugging, free up annoying data lines
5.1 Wireless Connection Procedure
Put your phone on the same LAN as the computer you are developing. Open the command line in Android Studio on your computer. Note that you need to connect your phone to Android Studio with a USB cable first.Copy the code
Now open Android Studio Teiminal command line, connect to run ADB Devices command:Copy the code
As shown in the figure, List of Devices Attached represents the List of devices attached to Android Studio, and 6UK9X20411X01598 represents the serial number of the devices we connected to Android Studio with the data cable.
Having found the device, we need to wirelessly connect the device to Android Studio over the LAN and continue typing:
adb -s 6UK9X20411X01598 tcpip 5555
Copy the code
5555 is the port number. It is different from the common port number. This command binds devices connected to Android Studio with data cables to port 5555.
Next, we only need to bind Android Studio to the port on the LAN (note that the following LAN address 106 is the LAN address of the device to be connected, and the 5555 port number is the tcpIP port number set above, so keep the same) :
The adb connect 192.168.1.106:5555Copy the code
And that’s it! Adb Devices -l command, see the result, now you can remove the annoying data line!
Here is my screenshot
I have been using this kind of debugging mode, which can install APP debugging wirelessly. Compared with the data line, I was worried about whether the package loading speed would be slow at the beginning, but I did not feel particularly obvious speed delay when using the next installation package within 100m (I did not write HH beyond 100m), and I felt very good about using it.
5.2 Possible problems
If ADB connection is disconnected:
-
Ensure that the host is still connected to the same WLAN as the Android device.
-
Reconnect by performing the ADB Connect step again.
-
If the problem does not resolve, reset the ADB host:
adb kill-server adb start-server Copy the code
Then, start from scratch.
Note: Huawei devices may not connect successfully at first, note that in developer options, turn on “Charge only” mode to allow ADB debugging!
Wireless connectivity for Android 11
Before Android 11, it was inevitable to connect adb the normal way by using a cable to your phone first.
But sometimes there is no data line or drive and other problems, can not connect to the phone, or the data line is connected to the phone, but the contact is often disconnected, at the moment is very troublesome.
Android11’s wireless debugging solves this problem well, and it supports direct connection without cable.
Android 11 enables you to deploy and debug applications wirelessly from your workstation via the Android Debug Bridge (ADB) without actually connecting to your device via USB, avoiding common USB connection issues such as driver installation issues.
To use wireless debugging, you need to pair your device with your workstation using a pairing code. Your workstation and device must be connected to the same wireless network.
Operation steps:
1: Enable developer mode and enable wireless debugging
2: Use the pairing code to pair the device. Note down the pairing code, IP address, and port number displayed on the device
3: Open the Android Studio terminal and enter
adb pair ipaddr:port
Copy the code
Then the terminal will prompt you to enter the matching code, enter the previous mobile phone display of the matching code!
(base) ystm1@ystm1 ~ % adb pair 192.168.1.113:39753 Enter pairing code: 605434 Successfully paired to 192.168.1.113:39753 [GUID = adB-6d233f38-fn4ztm] (base) ystm1@ystm1 ~ %Copy the code