Automating Android Devices with Python and ADB
This tutorial explains how to set up ADB and the pure‑python‑adb library, enable USB debugging, and write Python scripts that control an Android phone to perform actions such as taking selfies, searching for word definitions, and capturing screenshots, providing a practical introduction to mobile automation.
Python can be used to control an Android phone through the Android Debug Bridge (ADB), allowing you to automate touches, swipes, and other actions. This guide shows how to install the required tools, enable developer options, and write Python scripts that interact with ADB.
1. Installation prerequisites
Install the ADB tool (bundled with Android Studio or available from the official documentation) and then install the Python library that provides an ADB interface:
pip install pure-python-adb # install pure-python-adb libraryOptionally, install scrcpy to mirror and control the device from your computer; add its directory to your system PATH.
2. Getting started
Connect the phone via USB, enable USB debugging in the developer options, and start the ADB server with adb start-server . If scrcpy is installed, run scrcpy to view the device screen.
3. Basic script structure
Import AdbClient from the library, create a client object, and retrieve the list of connected devices. The first device in the list is typically the one you will control.
4. Creating a selfie timer
Define a connect() function that returns the device and client objects, then use the device object to launch the camera app, wait a few seconds, and trigger a photo capture.
5. Building a definition finder
Use ADB shell commands to simulate taps at the coordinates of the browser’s search bar (e.g., (440, 200)), input a query, wait, and then capture a screenshot of the result. The script opens the browser, enters the search term, and saves the screenshot as a PNG file.
These examples demonstrate that, with Python and ADB, you can automate virtually any interaction you would normally perform manually on an Android device.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.