Skip to content

Fix AdbRunner AVD detection: use getprop first, skip offline emulators#312

Merged
jonathanpeppers merged 2 commits intomainfrom
dev/adbrunner-avd-detection
Mar 19, 2026
Merged

Fix AdbRunner AVD detection: use getprop first, skip offline emulators#312
jonathanpeppers merged 2 commits intomainfrom
dev/adbrunner-avd-detection

Conversation

@rmarinho
Copy link
Member

Summary

Two fixes for AdbRunner emulator detection reliability, discovered while investigating dotnet/android#10965.

Related PR: dotnet/android#10969

1. Swap AVD name detection order (GetEmulatorAvdNameAsync)

adb -s <serial> emu avd name returns empty output on modern Android emulators (tested with emulator 36.4.10, adb v36). This causes AVD name matching to fail even when the correct emulator is running.

Fix: Use adb shell getprop ro.boot.qemu.avd_name first (reliable on all modern emulators, always set by the emulator kernel), fall back to emu avd name for older emulator versions.

2. Skip AVD name queries for offline emulators (ListDevicesAsync)

ListDevicesAsync called GetEmulatorAvdNameAsync for ALL emulators including offline ones. Neither getprop nor emu avd name works on offline devices — they fail or timeout. During boot polling loops, this causes unnecessary delays on every iteration as the emulator transitions from offline → online.

Fix: Only query AVD names for online emulators (Status == AdbDeviceStatus.Online).

Testing

  • All 47 AdbRunner tests pass
  • Full test suite: 241 passed, 1 pre-existing env-specific failure, 10 skipped (require device)
  • Verified end-to-end on macOS with emulator 36.4.10

@rmarinho rmarinho force-pushed the dev/adbrunner-avd-detection branch 3 times, most recently from 754bc93 to 994314f Compare March 18, 2026 18:52
@rmarinho rmarinho marked this pull request as ready for review March 18, 2026 23:10
Copilot AI review requested due to automatic review settings March 18, 2026 23:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves Xamarin.Android.Tools.AndroidSdk’s AdbRunner emulator AVD detection to better handle modern emulator behavior and avoid slow/failed AVD queries while emulators are offline (e.g., during boot polling).

Changes:

  • Prefer adb shell getprop ro.boot.qemu.avd_name for emulator AVD name detection, with fallback to adb emu avd name.
  • Skip AVD-name probing for offline emulators in ListDevicesAsync (and optionally log the skip).
  • Add/extend unit tests covering the detection order and offline-emulator handling.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
tests/Xamarin.Android.Tools.AndroidSdk-Tests/AdbRunnerTests.cs Adds new tests using a fake adb implementation to validate AVD detection and offline handling.
src/Xamarin.Android.Tools.AndroidSdk/Runners/AdbRunner.cs Updates AVD name query order; skips offline emulators; adds optional logger parameter.
src/Xamarin.Android.Tools.AndroidSdk/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt Updates public API entry for the AdbRunner constructor signature change.
src/Xamarin.Android.Tools.AndroidSdk/PublicAPI/net10.0/PublicAPI.Unshipped.txt Updates public API entry for the AdbRunner constructor signature change.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +674 to +677
var psi = new System.Diagnostics.ProcessStartInfo ("chmod") {
ArgumentList = { "+x", path },
};
System.Diagnostics.Process.Start (psi)?.WaitForExit ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we had a [DllImport] for this somewhere? Can the test call a method from ProcessUtils (or whereever that was?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — replaced Process.Start("chmod") with FileUtil.Chmod(path, 0x1ED) which uses File.SetUnixFileMode on .NET 7+ and the [DllImport("libc")] chmod on older runtimes. Also replaced the null-forgiving ! on Path.GetDirectoryName() with is { Length: > 0 } property pattern per repo conventions.

@rmarinho rmarinho force-pushed the dev/adbrunner-avd-detection branch from 10ec06c to 1661ac9 Compare March 19, 2026 13:16
Two fixes for AdbRunner emulator detection reliability:

1. Swap AVD name detection order in GetEmulatorAvdNameAsync: use
   'adb shell getprop ro.boot.qemu.avd_name' first (reliable on all
   modern emulators), fall back to 'adb emu avd name' for older versions.
   The 'emu avd name' command returns empty output on emulator 36.4.10+.

2. Skip AVD name queries for offline emulators in ListDevicesAsync:
   neither getprop nor 'emu avd name' works on offline devices, causing
   unnecessary delays during boot polling loops.

Context: dotnet/android#10965

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rmarinho rmarinho force-pushed the dev/adbrunner-avd-detection branch from 1661ac9 to cc1e063 Compare March 19, 2026 14:21
…erators

- Replace Process.Start("chmod") with FileUtil.Chmod(path, 0x1ED)
  which uses managed File.SetUnixFileMode on .NET 7+ and the
  chmod DllImport on older runtimes
- Replace null-forgiving operator on Path.GetDirectoryName() with
  is { Length: > 0 } property pattern per repo conventions
- Reorder CleanupFakeAdb to delete file only when dir is non-null

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers merged commit 2bea0eb into main Mar 19, 2026
2 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/adbrunner-avd-detection branch March 19, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants