introduce

The Android Debug Bridge (ADB) is a versatile command-line tool for communicating with devices.

Adb commands can be used to perform a variety of device operations, such as installing and debugging applications, and provide access to the Unix shell, which can be used to run various commands on the device. It is a C/S 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.

Once you have installed the SDK in Android Studio, you can find adb tools in the directory corresponding to platform-Tools.

The position of the tool for the android_sdk/platform – the tools/adb, on my MAC default location for local/Users/lumin/Library/Android/SDK/platform – the tools.

Of course, you can manually configure the system environment variables and then execute adb commands.

List connected devices

➜ platform-tools./adb Devices-l List of Devices Attached 8FD0218A25001582 Device USB :338821120X Product: CLT-AL00L Model :CLT_AL00l device:HWCLT transport_id:6 192.168.43.57:5555 Device Product :GLL-AL00CN model:GLL_AL00 Device :HWGLL transport_id:5Copy the code

It is connected to two devices. The first device is directly connected by USB data cable, and the second device is connected by IP address in the same LAN.

The first item is the identifier (serial number) of the device by which subsequent commands specify that the device will operate.

Usually you need to specify the serial number for multiple devices:

adb -s 8FD0218A25001582 command
Copy the code

Adb prints the following status information for each device:

  • 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: The 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 the command contains the -l option, the devices command tells you what the device is. This information is useful when you connect multiple devices to help you distinguish them.

Connecting remote Devices

➜ platform-tools./adb connect 192.168.43.57:5555 already connected to 192.168.43.57:5555Copy the code

File each other

You can use the pull and push commands to copy files to and from the device, and use the pull and push commands to copy any directory and files to any location on the device.

Copy a file or directory (and its subdirectories) from a device:

adb pull remote local
Copy the code

Copy a file or directory (and its subdirectories) to a device:

adb push local remote
Copy the code

Copy a file to the /sdcard/ HAPS directory

➜ platform - tools. / adb - s 8 fd0218a25001582 push/Users/lumin/build/outputs/hap/release/entry - release - rich - signed. Hap /sdcard/haps /Users/lumin/build/output... 2. To bounce lightly. 2. Ap: 1 file pushed, 0 skippedin0.002 s)Copy the code

Stopping the ADB server

In some cases, you may need to terminate the ADB server process and then restart it to resolve the problem (for example, if adb does not respond to commands).

adb kill-server
Copy the code

Sends shell commands to the device

You need to specify the device before sending the command. If only one device is connected, the system sends commands to this device by default.

adb [-d | -e | -s serial_number] command
Copy the code

Single command operation

List the device root directory

➜ platform-tools./adb -s 8FD0218A25001582 shell ls -l/total 28 drwxr-xr-x 2 root root 0 2018-08-08 00:01 3RDmodem drwxr-xr-x 2 root root 0 2018-08-08 00:01 3rdmodemnvm drwxr-xr-x 2 root root 0 2018-08-08 00:01 3rdmodemnvmbkp dr-xr-xr-x 110 root root 0 2021-05-17 10:19 acct lrw-r--r-- 1 root root 11 2018-08-08 00:01 bin -> /system/bin lrw-r--r-- 1 root root 50 2018-08-08 00:01 bugreports -> /data/user_de/0/com.android.shell/files/bugreports drwxrwx--- 7  system cache 4096 2019-08-29 02:56 cache lrw-r--r-- 1 root root 13 2018-08-08 00:01 charger -> /sbin/charger drwxr-xr-x  4 root root 0 1970-01-01 08:00 config drwxr-xr-x 7 root root 79 2018-08-08 00:01 cust lrw-r--r-- 1 root root 17 2018-08-08 00:01 d -> /sys/kernel/debugCopy the code

Interactive shell commands

adb [-d | -e | -s serial_number] shell
Copy the code

Exit the interactive command:

exit
Copy the code

View the tools supported by the device

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

Install the application

adb install path_to_apk
Copy the code

Read more:

> developer. The android. Google. Cn/studio/comm…