Skip to main content

AdbClient

Struct AdbClient 

Source
pub struct AdbClient { /* private fields */ }
Expand description

Client wrapping adb invocations.

Construction is cheap — internally just an adb binary path resolved via PATH lookup at command-spawn time. No persistent state.

Implementations§

Source§

impl AdbClient

Source

pub fn new() -> Self

Default constructor — uses adb from PATH.

Source

pub fn with_binary(binary: impl Into<String>) -> Self

Build with an explicit adb binary path (useful for tests or non-PATH SDK installs).

Source

pub async fn devices(&self) -> Result<Vec<AdbDevice>, AdbError>

adb devices -l — list all attached devices/emulators.

Source

pub async fn install( &self, serial: &str, apk_path: &Path, ) -> Result<(), AdbError>

adb -s <serial> install -r <apk> — install (or upgrade) an apk.

Source

pub fn spawn_shell(&self, serial: &str, cmd: &[&str]) -> Result<Child, AdbError>

Spawn adb -s <serial> shell <cmd...> and hand back the child rather than waiting for it.

For device commands that run until stopped — screenrecord is the one. The caller owns the child, and how it ends matters: screenrecord only finalizes a playable mp4 when it gets SIGINT, so killing the child outright leaves a file with no moov atom.

Source

pub async fn pull( &self, serial: &str, remote: &str, local: &Path, ) -> Result<(), AdbError>

adb -s <serial> pull <remote> <local> — copy a file off the device.

Source

pub async fn push( &self, serial: &str, local: &Path, remote: &str, ) -> Result<(), AdbError>

adb -s <serial> push <local> <remote> — copy a file onto the device.

Source

pub async fn broadcast( &self, serial: &str, action: &str, data: Option<&str>, ) -> Result<(), AdbError>

adb -s <serial> shell am broadcast -a <action> [-d <data>].

Note that result=0 is the ordinary answer — it means the broadcast was delivered and nothing set a result code, not that anything failed.

Source

pub async fn uninstall( &self, serial: &str, package: &str, ) -> Result<(), AdbError>

adb -s <serial> uninstall <pkg> — uninstall a package.

Source

pub async fn start_activity( &self, serial: &str, package: &str, activity: &str, extras: &[(&str, &str)], ) -> Result<(), AdbError>

adb -s <serial> shell am start -n <pkg>/<activity> [args] — launch app’s activity. Returns nothing on success.

Source

pub async fn force_stop( &self, serial: &str, package: &str, ) -> Result<(), AdbError>

adb -s <serial> shell am force-stop <pkg> — force-stop a package.

Source

pub async fn screenshot(&self, serial: &str) -> Result<Vec<u8>, AdbError>

adb -s <serial> shell screencap -p — capture device screen as PNG. Returns raw PNG bytes via stdout.

Source

pub async fn forward( &self, serial: &str, host_port: u16, device_port: u16, ) -> Result<(), AdbError>

adb -s <serial> forward tcp:<host> tcp:<device> — set up port forwarding from host loopback to device port.

Source

pub async fn unforward( &self, serial: &str, host_port: u16, ) -> Result<(), AdbError>

adb -s <serial> forward --remove tcp:<host> — remove a forward.

Source

pub async fn shell( &self, serial: &str, cmd: &[&str], ) -> Result<String, AdbError>

adb -s <serial> shell <cmd...> — generic shell exec, returns stdout.

Runs on the device. For the emulator console — geo, sms, rotate and friends — use Self::emu: those are adb’s own subcommands, and asking the device’s shell for them finds nothing.

Source

pub async fn emu(&self, serial: &str, cmd: &[&str]) -> Result<String, AdbError>

adb -s <serial> emu <cmd...> — the emulator console, returns stdout.

A different channel from Self::shell. adb shell emu geo fix … looks plausible but asks the device for a program named emu — it answers sh: emu: inaccessible or not found and exits 127. setLocation shipped that way, so it always failed.

The console reports differently from the shell, and that is why this method exists rather than a call site. adb shell propagates the device command’s exit status (measured: shell 'exit 42' → 42), but adb emu does not — it answers OK, or KO: <reason>, and exits 0 either way (measured: a bad argument gives KO: argument 'x' is not a number, exit 0). So the exit code carries nothing here and stdout carries everything; KO is translated into an error, because nobody downstream would think to look for it.

Emulator-only, as the name says. A physical device has no console, which is no loss: smix is simulator-only by charter.

Source

pub async fn pm_grant( &self, serial: &str, package: &str, permission: &str, ) -> Result<(), AdbError>

adb -s <serial> shell pm grant <pkg> <android.permission.X>.

Source

pub async fn pm_clear( &self, serial: &str, package: &str, ) -> Result<(), AdbError>

adb -s <serial> shell pm clear <pkg> — wipe the app’s data.

Measured: this also reverts the package’s runtime permissions to their default. There is no host-side way to wipe the data alone, so a caller who wants only the files loses the grants with them.

Source

pub async fn runtime_permissions_granted( &self, serial: &str, package: &str, ) -> Result<Vec<String>, AdbError>

The runtime permissions <pkg> currently holds.

Read from dumpsys package, which is the only host-side view of them: pm reset-permissions exists but reverts every app on the device, including whatever the runner was granted to do its job.

Source

pub async fn pm_revoke( &self, serial: &str, package: &str, permission: &str, ) -> Result<(), AdbError>

adb -s <serial> shell pm revoke <pkg> <android.permission.X>.

Trait Implementations§

Source§

impl Clone for AdbClient

Source§

fn clone(&self) -> AdbClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AdbClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AdbClient

Source§

fn default() -> AdbClient

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.