Mobile Development 20 min read

Comprehensive Guide to Android ADB: Concepts, Setup, Commands, and Automation Scripts

This article provides a detailed introduction to Android Debug Bridge (ADB), covering its concepts, architecture, environment setup, common issues, command syntax, and practical automation examples using both shell scripts and Python, enabling developers to control devices, perform UI actions, and schedule tasks efficiently.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Comprehensive Guide to Android ADB: Concepts, Setup, Commands, and Automation Scripts

This tutorial introduces Android Debug Bridge (ADB), the command‑line tool that enables communication with Android devices for automation and debugging.

ADB Concept and Architecture

ADB consists of three components: the server (adb server) running on the PC, the client (adb command) that sends commands, and the daemon (adbd) on the device. The server listens on TCP port 5037, discovers devices on ports 5555‑5585, and forwards client requests to the appropriate adbd process.

Working Principle

When an adb client starts, it checks for a running server; if none exists, it launches one. The server binds to port 5037, scans for device daemons, establishes connections, and then commands can be issued to any connected device.

Environment Configuration

For Android developers using Android Studio, adb is already in the PATH. Otherwise, download the SDK Platform‑Tools, extract them, and add the folder to the system PATH. Verify installation with adb version .

Common Issues

Port 5037 occupied – kill the occupying process using netstat -ano | findstr "5037" and taskkill /pid /f , or change the server port via the ANDROID_ADB_SERVER_PORT environment variable.

Device not recognized – ensure USB debugging is enabled, authorize the PC, or delete the ~/.android/adbkey* files and restart the server.

ADB Command Details

The basic syntax is adb [-d|-e|-s ] <command> . Common options: -d (single USB device), -e (emulator), -s (specific device).

Typical Commands

View foreground activity: # adb shell # dumpsys activity activities | grep mResumedActivity

Launch an app or activity: # adb shell # monkey -p -c android.intent.category.LAUNCHER 1 # am start -n /

Force‑stop an app: # adb shell am force-stop

Simulate input (key, text, swipe, tap): # adb shell input keyevent 3 # Home # adb shell input text Hello # adb shell input swipe 300 1000 300 500 # adb shell input tap 500 500

Screenshot: # adb shell screencap -p /sdcard/sc.png # adb pull /sdcard/sc.png

Get screen resolution: # adb shell wm size

Calling ADB from an Android App

Directly invoking adb from an app is discouraged due to permission and background‑service constraints. The article provides a Kotlin example that executes shell commands via Runtime.getRuntime().exec() and a root‑privileged variant using su .

Python Wrapper for ADB Automation

A lightweight Python library is presented, using subprocess.Popen to run adb commands and offering functions such as start_app , kill_app , current_pkg_activity , key_event , input_text , swipe , click , screenshot , and screen_size . Example usage:

import adb_util
if __name__ == '__main__':
    package_name = "com.example.app"
    adb_util.start_app(package_name)

Practical Automation: Auto Check‑In Script

The guide shows how to schedule the Python script to open a work‑attendance app at a specific time. Two approaches are described:

Windows Task Scheduler – create a basic task that runs python.exe path\to\script.py at 08:30 daily.

Python schedule library – a lightweight loop that calls adb_util.start_app() at the desired time.

Both methods allow the phone to stay connected to the PC, keeping the screen on and disabling lock, so the script can trigger the app automatically.

Conclusion

The article walks readers through ADB fundamentals, resolves typical setup problems, demonstrates essential commands, and provides ready‑to‑use Python wrappers for building automation workflows such as auto check‑in, illustrating how ADB can be leveraged for mobile development tasks.

mobile developmentPythonAndroidAutomationADB
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.