Adbutils

2.12.0 · active · verified Wed Apr 15

Adbutils is a pure Python library for interacting with Android devices via the Android Debug Bridge (ADB) protocol. It provides a programmatic interface to control devices, install apps, execute shell commands, manage files, and more, without needing to directly invoke the `adb` command-line tool. The current version is 2.12.0, and it is actively maintained with frequent releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart code demonstrates how to list connected ADB devices, retrieve device properties, execute shell commands, and push a file to a device. It also includes logic to ensure the ADB server is running. Requires at least one ADB-enabled Android device connected and properly configured.

import adbutils

# Get a list of connected devices
devices = adbutils.adb.devices()
if devices:
    print(f"Connected devices: {len(devices)}")
    for i, d in enumerate(devices):
        print(f"  Device {i}: {d.serial} (status: {d.status})")
        # Example: Get a device object by serial or index
        if i == 0:
            device = d
            print(f"  First device model: {device.getprop('ro.product.model')}")
            # Example: Execute a shell command
            output = device.shell("echo Hello from device").strip()
            print(f"  Shell output: {output}")
            # Example: Push a file (requires a dummy file)
            try:
                with open('test.txt', 'w') as f:
                    f.write('This is a test file.')
                device.sync.push('test.txt', '/sdcard/test.txt')
                print('  Pushed test.txt to /sdcard/test.txt')
                device.shell('rm /sdcard/test.txt') # Clean up
                print('  Cleaned up /sdcard/test.txt')
            except Exception as e:
                print(f"  Could not push/cleanup test.txt (ignored for quickstart): {e}")
else:
    print("No ADB devices found. Make sure ADB server is running and devices are connected.")

# Ensure the ADB server is running
if not adbutils.adb.server_version():
    print("ADB server not running. Starting it...")
    adbutils.adb.start_server()
    print("ADB server started.")
else:
    print("ADB server is already running.")

view raw JSON →