Skip to content

Commit 3b9a745

Browse files
committed
devices/adb: Find adb executable in $ANDROID_HOME/$ANDROID_SDK_ROOT
`adb` is not always available on `PATH`, sometimes it is installed only via the SDK. Make sure we find it there too - after checking `PATH` - via well-known SDK variables.
1 parent f2dc194 commit 3b9a745

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

xbuild/src/devices/adb.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::devices::{Backend, Device};
22
use crate::{Arch, Platform};
3-
use anyhow::Result;
3+
use anyhow::{Context, Result};
44
use apk::Apk;
55
use std::io::{BufRead, BufReader};
66
use std::path::{Path, PathBuf};
@@ -11,8 +11,44 @@ use std::time::Duration;
1111
pub(crate) struct Adb(PathBuf);
1212

1313
impl Adb {
14-
pub fn which() -> Result<Self> {
15-
Ok(Self(which::which(exe!("adb"))?))
14+
pub fn which() -> Result<PathBuf> {
15+
const ADB: &str = exe!("adb");
16+
17+
match which::which(ADB) {
18+
Err(which::Error::CannotFindBinaryPath) => {
19+
let sdk_path = {
20+
let sdk_path = std::env::var("ANDROID_SDK_ROOT").ok();
21+
if sdk_path.is_some() {
22+
eprintln!(
23+
"Warning: Environment variable ANDROID_SDK_ROOT is deprecated \
24+
(https://developer.android.com/studio/command-line/variables#envar). \
25+
It will be used until it is unset and replaced by ANDROID_HOME."
26+
);
27+
}
28+
29+
PathBuf::from(
30+
sdk_path
31+
.or_else(|| std::env::var("ANDROID_HOME").ok())
32+
.context(
33+
"Cannot find `adb` on in PATH nor is ANDROID_HOME/ANDROID_SDK_ROOT set",
34+
)?,
35+
)
36+
};
37+
38+
let adb_path = sdk_path.join("platform-tools").join(ADB);
39+
anyhow::ensure!(
40+
adb_path.exists(),
41+
"Expected `adb` at `{}`",
42+
adb_path.display()
43+
);
44+
Ok(adb_path)
45+
}
46+
r => r.context("Could not find `adb` in PATH"),
47+
}
48+
}
49+
50+
pub fn new() -> Result<Self> {
51+
Ok(Self(Self::which()?))
1652
}
1753

1854
fn adb(&self, device: &str) -> Command {

xbuild/src/devices/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::{Arch, BuildEnv, Platform};
55
use anyhow::Result;
66
use std::path::Path;
77

8-
mod adb;
9-
mod host;
10-
mod imd;
8+
pub(crate) mod adb;
9+
pub(crate) mod host;
10+
pub(crate) mod imd;
1111

1212
#[derive(Clone, Debug)]
1313
enum Backend {
@@ -31,7 +31,7 @@ impl std::str::FromStr for Device {
3131
}
3232
if let Some((backend, id)) = device.split_once(':') {
3333
let backend = match backend {
34-
"adb" => Backend::Adb(Adb::which()?),
34+
"adb" => Backend::Adb(Adb::new()?),
3535
"imd" => Backend::Imd(IMobileDevice::which()?),
3636
_ => anyhow::bail!("unsupported backend {}", backend),
3737
};
@@ -58,7 +58,7 @@ impl std::fmt::Display for Device {
5858
impl Device {
5959
pub fn list() -> Result<Vec<Self>> {
6060
let mut devices = vec![Self::host()];
61-
if let Ok(adb) = Adb::which() {
61+
if let Ok(adb) = Adb::new() {
6262
adb.devices(&mut devices)?;
6363
}
6464
if let Ok(imd) = IMobileDevice::which() {

0 commit comments

Comments
 (0)