adb_client

repository·main·Indexed 18 days ago

https://github.com/cocool97/adb_client

A pure Rust implementation of the Android Debug Bridge (ADB) protocol supporting connections via ADB server, USB, or TCP/IP. It includes the adb_cli binary for a lightweight CLI experience, the adb_client Rust crate for library integration, and a Python wrapper called pyadb_client.

Tokens
22.1K
Snippets
83
Records
100
Agent score
62%

What's inside adb_client

  1. Overview of adb_client

    main

    The adb_client project is a pure Rust implementation of the Android Debug Bridge (ADB) protocol. It allows developers to interact with Android devices without relying on the official adb shell commands.

    Key capabilities include:

    • Flexible Connection Modes: Use an existing ADB server as a proxy (standard behavior) or connect directly to end devices over USB or TCP/IP (bypassing the ADB server).
    • Advanced Features: Supports hidden ADB features such as framebuffer access.
    • Multi-language Support: Provides a high-level Rust abstraction and a Python wrapper (pyadb_client) for ease of use in different environments.
  2. Use PyADBServer to manage devices via ADB server

    main

    Use PyADBServer to interact with an existing ADB server (e.g., running on localhost). You can iterate through all available devices or retrieve a single connected device.

    Key methods:

    • devices(): Returns an iterator of devices.
    • get_device(): Returns a single connected device.
    • connect_device(device_id): Connects to a specific device using its ID.
    • disconnect_device(device_id): Disconnects from a specific device using its ID.
    from pyadb_client import PyADBServer
    
    # Initialize server with address
    server = PyADBServer("127.0.0.1:5037")
    
    # List all devices
    for i, device in enumerate(server.devices()):
        print(i, device.identifier, device.state)
    
    # Get only the first connected device
    device = server.get_device()
    print(device, device.identifier)
    
    # Connect to a device with device id
    device = server.connect_device("192.168.1.100:5555")
    print(f"Connected to {device.identifier}")
    
    # Disconnect from a device with device id
    server.disconnect_device("192.168.1.100:5555")
  3. Use adb_client in Rust

    main
    The adb_client crate provides a high-level abstraction over the ADB protocol, supporting both server-proxy and direct-to-device communication. For detailed API usage, refer to the package-specific documentation in the adb_client directory.
  4. Use pyadb_client in Python

    main
    The pyadb_client package is a Python wrapper around the adb_client Rust library. It exports classes that can be used directly within a Python environment to control Android devices. For detailed API usage, refer to the package-specific documentation in the pyadb_client directory.
  5. Set up pyadb_client for local development

    main

    To develop pyadb_client locally, you need to set up a virtual environment, install maturin for building the Rust-based Python extension, and use maturin to build the package.

    Steps:

    1. Create and activate a virtual environment.
    2. Install maturin.
    3. Use maturin develop to build the development package.
    4. (Optional) Generate stub files using the stub_gen binary.
    5. Build a release package using maturin build.
    # Create Python virtual environment
    python3 -m venv .venv
    source .venv/bin/activate
    
    # Install needed build dependencies
    pip install maturin
    
    # Build development package
    maturin develop
    
    # Build stub file (.pyi)
    cargo run --bin stub_gen
    
    # Build release Python package
    maturin build --release -m pyadb_client/Cargo.toml
  6. Use the adb_cli tool

    main

    The adb_cli is a command-line interface for interacting with Android devices via various transport methods: USB, TCP, MDNS discovery, or through an ADB server (Local/Host/Emulator).

    Depending on the command selected, you can perform operations such as shell access, file transfers (push/pull), package management (install/uninstall), and device diagnostics (stat, reboot, framebuffer).

  7. Configure `adb_client` crate features

    main

    The adb_client crate uses Cargo features to manage functionality. By default, the framebuffer feature is enabled. You can enable or disable specific features using the default-features option in your Cargo.toml.

    Available Features

    FeatureDescriptionDefault?
    framebufferEnables framebuffer-related methodsYes
    mdnsEnables mDNS device discovery on local networkNo
    usbEnables interactions with USB devicesNo

    To customize your build (e.g., to enable mdns and usb while disabling the default framebuffer), use the following configuration:

    [dependencies]
    adb_client = { version = "*", default-features = false, features = ["mdns", "usb"] }
  8. How transport selection works in ADBServerDevice

    main

    When calling set_serial_transport(), the ADBServerDevice determines which device to target using a specific precedence logic. This ensures that even if multiple devices have identical serial numbers, the correct one can be addressed if a transport ID is available.

    The selection order is:

    1. transport_id: If transport_id is set, it uses the unique transport ID (highest priority).
    2. identifier: If no transport ID is set, it falls back to the serial number (identifier).
    3. transport-any: If neither is configured, it defaults to targeting any available transport.
  9. Use the ADB Server for Local/Host/Emulator commands

    main

    The CLI can interact with an existing ADB server or start a local one to manage devices.

    • Local: Connects to a device via a local ADB server. If the address is a loopback or unspecified, the CLI will automatically start the ADBServer.
    • Host: Sends commands directly to the host ADB server.
    • Emu: Specifically handles emulator-related commands.