Skip to content

Conversation

@jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Nov 6, 2025

Context: https://github.com/dotnet/sdk/blob/2b9fc02a265c735f2132e4e3626e94962e48bdf5/documentation/specs/dotnet-run-for-maui.md

This implements the first couple of steps to support new dotnet run behavior in .NET 11.

  • $(Device) MSBuild property to specify target device (passed from dotnet run --device <id>). This will simply set $(AdbTarget) for now.

  • ComputeAvailableDevices MSBuild target to get the list of connected Android devices/emulators using adb devices command:

image

Some of the extra MSBuild item metadata was completely optional, I just left anything that adb devices -l reports to be in here.

Added unit tests for GetAvailableAndroidDevices task, that mostly test input/output parsing without actually running adb.

…rget

Context: https://github.com/dotnet/sdk/blob/2b9fc02a265c735f2132e4e3626e94962e48bdf5/documentation/specs/dotnet-run-for-maui.md

This implements the first couple of steps to support new `dotnet run`
behavior in .NET 11.

* `$(Device)` MSBuild property to specify target device (passed from
  `dotnet run --device <id>`). This will simply set `$(AdbTarget)` for
  now.

* `ComputeAvailableDevices` MSBuild target to get the list of
  connected Android devices/emulators using `adb devices` command:

    Target ComputeAvailableDevices  55 ms
      Using "GetAvailableAndroidDevices" task from assembly "D:\src\xamarin-android\bin\Debug\lib\packs\Microsoft.Android.Sdk.Windows\36.1.99\targets\..\tools\Xamarin.Android.Build.Tasks.dll".
      Task GetAvailableAndroidDevices  55 ms
        Assembly = D:\src\xamarin-android\bin\Debug\lib\packs\Microsoft.Android.Sdk.Windows\36.1.99\tools\Xamarin.Android.Build.Tasks.dll
        Parameters
          16:47:08.2643378  C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe devices -l
          16:47:08.2867570  List of devices attached
          16:47:08.2867734  0A041FDD400327  device product:redfin model:Pixel_5 device:redfin transport_id:2
          16:47:08.2867765  emulator-5554  device product:sdk_gphone64_x86_64 model:sdk_gphone64_x86_64 device:emu64xa transport_id:1
          16:47:08.2920363  Running process: C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 emu avd name
          16:47:08.3172534  pixel_7_-_api_36
          16:47:08.3172747  OK
          16:47:08.3183905  Running process: exit code == 0
          16:47:08.3185099  Found 2 Android device(s)/emulator(s)
        OutputItems
          TargetOutputs
            0A041FDD400327
              Status = Online
              Type = Device
              Device = redfin
              TransportId = 2
              Description = Pixel 5
              Product = redfin
              Model = Pixel_5
            emulator-5554
              Status = Online
              Type = Emulator
              Device = emu64xa
              TransportId = 1
              Description = pixel 7 - api 36
              Product = sdk_gphone64_x86_64
              Model = sdk_gphone64_x86_64

Some of the extra MSBuild item metadata was completely optional, I
just left anything that `adb devices -l` reports to be in here.

Added unit tests for `GetAvailableAndroidDevices` task, that mostly
test input/output parsing without actually running `adb`.
@jonathanpeppers jonathanpeppers marked this pull request as ready for review November 6, 2025 20:01
Copy link
Member

@baronfel baronfel left a comment

Choose a reason for hiding this comment

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

This looks great! I'm pretty happy with how straightforward this hook-up is.

Comment on lines +41 to +49
<Target Name="ComputeAvailableDevices"
DependsOnTargets="_ResolveMonoAndroidSdks"
Returns="@(Devices)">
<GetAvailableAndroidDevices
ToolExe="$(AdbToolExe)"
ToolPath="$(AdbToolPath)">
<Output TaskParameter="Devices" ItemName="Devices" />
</GetAvailableAndroidDevices>
</Target>
Copy link
Member

Choose a reason for hiding this comment

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

Is there any possible world where there's some way to do input/output tracking here for incrementality purposes?

Copy link
Member Author

Choose a reason for hiding this comment

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

It has to query each time because you could click the X on the emulator, unplug the device, etc.

It was taking 55ms on my machine, but we can see if there are different adb commands that are faster in the future. I was also thinking about reading files on disk like %userprofile%\.android\avd\pixel_7_-_api_36.ini that could be faster than launching a new process.

Copy link
Member

@rolfbjarne rolfbjarne left a comment

Choose a reason for hiding this comment

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

Looks great! I'll look into creating a PR for macios next week.

@jonathanpeppers jonathanpeppers merged commit 63f7cba into main Nov 7, 2025
59 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/peppers/ComputeAvailableDevices branch November 7, 2025 18:54
rolfbjarne added a commit to dotnet/macios that referenced this pull request Nov 18, 2025
…vailable to run on.

This consists of two parts:

* Add an MSBuild target that lists all the available devices (`ComputeAvailableDevices`), by returning them in a `$(Devices)` item group.
* Add support for the `$(Device)` property to specify the device or simulator to use.

Regarding the device list, we'll filter the returned list by:

* Platform (don't return an Apple TV simulator for an iOS app).
* Minimum OS version (not return an iOS 18.0 device when the app's minimum OS version is 26.0).
* Only devices that are actually available, as reported by `devicectl` or `simctl`.

References:

* dotnet/android#10576
* https://github.com/dotnet/sdk/blob/2b9fc02a265c735f2132e4e3626e94962e48bdf5/documentation/specs/dotnet-run-for-maui.md

Fixes #23995.
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.

5 participants