Skip to content

Commit d4470fe

Browse files
authored
Update Android SDK tools path lookup to be more strongly anchored to the provided root. (#21046)
### Description <!-- Describe your changes. --> The tools should really all come from the same Android NDK, so using `shutil.which` adds potential confusion when we do a lookup for the target program by name first due to adding `dirnames.insert(0, "")` as the first directory entry to lookup as it will match the filename anywhere in the current path. That's problematic as the emulator should come from <sdk_tools>/emulator/emulator (see [here](https://www.stkent.com/2017/08/10/update-your-path-for-the-new-android-emulator-location.html)), but the paths on the CI machines result in the old location of <sdk_tools>/tools/emulator being selected. This leads to the emulator failing to run on arm64 macOS CIs as the old emulator does not look for the arm64 binary. At the most you may have multiple cmdline-tools versions installed, but if we need to support explicitly specifying a version for that path that can be added. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> Make emulator run on arm64 macOS machines.
1 parent f25cf19 commit d4470fe

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

tools/python/util/android/android.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import collections
55
import contextlib
66
import datetime
7-
import os
8-
import shutil
97
import signal
108
import subprocess
119
import time
1210
import typing
11+
from pathlib import Path
1312

1413
from ..logger import get_logger
1514
from ..platform_helpers import is_linux, is_windows
@@ -28,26 +27,17 @@ def filename(name, windows_extension):
2827
else:
2928
return name
3029

31-
def resolve_path(dirnames, basename):
32-
dirnames.insert(0, "")
33-
for dirname in dirnames:
34-
path = shutil.which(os.path.join(os.path.expanduser(dirname), basename))
35-
if path is not None:
36-
path = os.path.realpath(path)
37-
_log.debug(f"Found {basename} at {path}")
38-
return path
39-
raise FileNotFoundError(f"Failed to resolve path for {basename}")
30+
sdk_root = Path(sdk_root).resolve(strict=True)
4031

4132
return SdkToolPaths(
42-
emulator=resolve_path([os.path.join(sdk_root, "emulator")], filename("emulator", "exe")),
43-
adb=resolve_path([os.path.join(sdk_root, "platform-tools")], filename("adb", "exe")),
44-
sdkmanager=resolve_path(
45-
[os.path.join(sdk_root, "cmdline-tools", "latest", "bin")],
46-
filename("sdkmanager", "bat"),
33+
# do not use sdk_root/tools/emulator as that is superceeded by sdk_root/emulator/emulator
34+
emulator=str((sdk_root / "emulator" / filename("emulator", "exe")).resolve(strict=True)),
35+
adb=str((sdk_root / "platform-tools" / filename("adb", "exe")).resolve(strict=True)),
36+
sdkmanager=str(
37+
(sdk_root / "cmdline-tools" / "latest" / "bin" / filename("sdkmanager", "bat")).resolve(strict=True)
4738
),
48-
avdmanager=resolve_path(
49-
[os.path.join(sdk_root, "cmdline-tools", "latest", "bin")],
50-
filename("avdmanager", "bat"),
39+
avdmanager=str(
40+
(sdk_root / "cmdline-tools" / "latest" / "bin" / filename("avdmanager", "bat")).resolve(strict=True)
5141
),
5242
)
5343

0 commit comments

Comments
 (0)