ADB (Android Debug Bridge) detailed explanation
Whether you are doing App crawler automation, reverse analysis, or Android native/cross-end development, ADB (Android Debug Bridge) is a "mobile Swiss Army Knife". It allows you to control your phone like a local command line: install applications, transfer files, capture logs, screenshots, port forwarding...all done with one command.
This article starts from scratch and explains the core scenarios of device connection, file transfer, application management, and Python automation in the most straightforward language. This eliminates the need for you to look up the commands piecemeal, and you can get started after reading them.
1. Device Management: Connection is the first step
Newbies are most likely to get stuck in "unauthorized" (unauthorized), "offline" or the device list is blank. Let’s first explain USB debugging, WiFi wireless connection, and common problem troubleshooting at once.
1.1 Turn on USB debugging (key points to avoid pitfalls)
Whether using USB or WiFi, the phone must first pop up the "Allow USB debugging" authorization box and confirm it. The configuration process only needs to be done once:
- Data cable: You must use a "charging + data transmission two-in-one" data cable. A pure charging cable cannot be recognized.
- Developer Options: Go to "Settings → About Phone" and click Build Number 7 times until it prompts "Developer Options Turned On".
- Turn on debugging: Return to "Settings → System (or more settings) → Developer Options", find and enable the following two options:
- USB Debugging
- USB Debugging (Security Settings) (some systems call it "USB Installation" or "Cancel Authorization Blank", etc.)
- Pop-up box confirmation: After connecting to the computer with a data cable, the phone will pop up the "Allow USB debugging?" dialog box. Be sure to check "Always allow the use of this computer for debugging", and then click "OK".
If the pop-up box does not pop up, or it displaysunauthorized, use the following "universal restart method":
Afterwards, re-plug and unplug the data cable or reconnect to WiFi, and the pop-up box will usually appear.
1.2 Basic connection check
After connecting the device, first useadb devicesConfirm status:
1.3 WiFi wireless debugging (liberating data lines)
Wireless debugging can be enabled when the mobile phone and computer are connected to the same LAN (under the same WiFi or router), and there is no need to plug in the wires in the future.
Operation steps:
After the connection is successful, as long as the mobile phone and computer are in the same LAN, each timeadb connect <手机IP>:5555You can connect.
Tips: If the WiFi connection cannot be connected, you can first
adb kill-server && adb start-serverTry again. In addition, the 5555 port will be closed after some systems are restarted and need to be activated again with a USB cable.
2. File transfer: high-speed mutual transfer without compromising image quality
Are you still using WeChat/QQ to transfer files? Not only is the image quality compressed, but the speed is also slower. ADB files are transferred via data cable or LAN, and the speed depends on the bandwidth. It is very suitable for transferring APK, logs, and test data.
Commonly used commands:
3. Application Control: Basic Skills of App Crawler
The first step of the crawler is to control the application: installation, uninstallation, obtaining package name, startup, forced stop, all of which ADB can perfectly support.
3.1 Installation and Uninstallation
In actual use,install -randuninstall -kThe most commonly used, the former is used for version iteration, and the latter is used to reset the application environment.
3.2 Query application information (find package name, startup page)
After getting the package name and starting the Activity, you can accurately control the application startup, which is more stable than using monkey.
3.3 Starting and stopping applications
After starting the application, you can start automated work with screenshots, screen recordings, logs, etc.
4. Python automated control: batch operations free your hands
A single command is too inefficient. For example, if you want to install the same application on 10 mobile phones, or automatically take screenshots every 5 seconds, typing commands manually is too error-prone. Use Python to encapsulate ADB commands into classes, which can not only be processed in batches, but can also be flexibly integrated into the crawler process.
The following is a lightweight ADB controller based on Python’s ownsubprocessModule, no need to install any third-party libraries, just ready to use.
This simplest controller covers high-frequency operations such as connection management, package name acquisition, and screenshots. You can continue to expand according to your needs: for example, addtap、swipe、inputand other methods to realize automated clicking, sliding and text input.
5. Add a few practical tips
5.1 Port forwarding
Sometimes we need to map a local service on the mobile phone to the computer. For example, if the mobile phone runs an HTTP API on port 5000, you can use port forwarding to pass it on the computer.localhost:6000direct access.
This is very useful when debugging remote interfaces and proxy man-in-the-middle packet capture.
5.2 Clear the log buffer
When crawling or debugging, too many old logs can interfere with analysis. Clear it first and then start capturing the logs. The result is much cleaner:
Use lateradb logcat -s YOUR_TAGIt is very refreshing to capture the logs with specified tags.
ADB is the cornerstone of mobile debugging. The above commands and Python packaging are sufficient to support most app crawling and automation needs. If you encounter more complex UI interactions (such as needing to click buttons, slide lists, and enter text), you can further combineuiautomator2orAppiumThis kind of high-level framework, but the bottom layer is still inseparable from the stable connection and basic operations of ADB.
I hope this detailed explanation can help you successfully overcome the mobile automation level.

