Repository navigation
[Microsoft.Android.Run] Add --console mode for instrumentation output - #13651
simonrozsival wants to merge 3 commits into
Conversation
Context: dotnet/sdk#56841 Console-style apps (an ordinary `Main` targeting `net*-android`) need `dotnet run` to behave like a console app: stream the program's stdout and stderr to the terminal, forward `dotnet run -- args`, and return the program's exit code. `Microsoft.Android.Run` can already start an instrumentation, but it only reports a pass/fail summary. `--console` (used together with `--instrument`) runs `adb shell am instrument -w -r` and passes the remaining arguments as base64-encoded `argc`/`argN` extras, so spaces, quotes, and empty strings survive the device shell. It decodes `stdout-b64`/`stderr-b64` status bundles as they arrive, writes them to the host's stdout/stderr, and returns the `exitCode` from the instrumentation result. If no exit code arrives (for example, the app crashed), the raw instrumentation output is printed and the exit code is 1. The adb process is started with `ProcessStartInfo.InheritedHandles = []`, both pipes are drained asynchronously, and adb is killed and reaped in a `finally` block, so malformed output or Ctrl+C (exit code 130) never leaves it running. Nothing passes `--console` yet; a follow-up adds the SDK instrumentation host and `dotnet run` wiring. ### Validation * `./dotnet-local.sh test bin/TestDebug/net11.0/Xamarin.Android.Build.Tests.dll --filter "FullyQualifiedName~AndroidDotnetToolTests"`: 4 passed, including fake-adb tests for invalid output cleanup and handle inheritance. * Manual: >550 KB of stderr drained without deadlock while preserving both streams and exit code 42; Ctrl+C streamed output, returned 130, and reaped adb. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
--console mode for instrumentation output
There was a problem hiding this comment.
🟡 Changes recommended
Console execution ignores --no-wait and discards terminal instrumentation codes needed for fallback error reporting, causing unexpected blocking and misleading completion errors.
3 open issues
What changed in this PR
Adds opt-in console execution to Microsoft.Android.Run, preparing for Android console apps to work through dotnet run. SDK host integration and automatic wiring remain follow-up work.
Changes:
- Adds
--consolewith base64-encoded argument transport, streamed stdout/stderr, and application exit-code handling. - Adds explicit adb handle isolation and process cleanup.
- Adds fake-adb tests for malformed-output cleanup and handle inheritance.
| File | Description |
|---|---|
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Tasks/AndroidDotnetToolTests.cs |
Tests adb cleanup and isolation of inherited handles. |
src/Microsoft.Android.Run/Program.cs |
Implements console-mode options, argument transport, output decoding, and process lifecycle handling. |
Review effort: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
jonathanpeppers
left a comment
There was a problem hiding this comment.
I was looking at the design of this PR stack, and I wonder if another approach first would be nicer:
dotnet run MyActivity.csdotnet run MyInstrumentation.cs
And we'd auto-generate an AndroidManifest.xml with reasonable default values in obj.
I am concerned about introducing a static void Main() and trying to emulate a console app, where we never did this before.
I wonder what you would find missing after making the above work for Activity and Instrumentation classes?
|
I wanted to introduce the capability to just run
We can mark this feature as experimental until we are confident in it.
That already works today, doesn't it? at least I'm using it in #13657 - just with a lot of repeating boilerplate for each test case (e.g., tests/TrimmingTests/Cases/JavaInterfaceInvokers.cs). This is very low priority, so if you have concerns, I'm fine with closing this and revisiting this later. |


Context: dotnet/sdk#56841
Console-style apps (an ordinary
Maintargetingnet*-android) needdotnet runto behave like a console app: stream the program's stdout and stderr to the terminal, forwarddotnet run -- args, and return the program's exit code.Microsoft.Android.Runcan already start an instrumentation, but it only reports a pass/fail summary.--console(used together with--instrument) runsadb shell am instrument -w -rand passes the remaining arguments as base64-encodedargc/argNextras, so spaces, quotes, and empty strings survive the device shell. It decodesstdout-b64/stderr-b64status bundles as they arrive, writes them to the host's stdout/stderr, and returns theexitCodefrom the instrumentation result. If no exit code arrives (for example, the app crashed), the raw instrumentation output is printed and the exit code is 1.The adb process is started with
ProcessStartInfo.InheritedHandles = [], both pipes are drained asynchronously, and adb is killed and reaped in afinallyblock, so malformed output or Ctrl+C (exit code 130) never leaves it running.Nothing passes
--consoleyet; a follow-up adds the SDK instrumentation host anddotnet runwiring.Validation
./dotnet-local.sh test bin/TestDebug/net11.0/Xamarin.Android.Build.Tests.dll --filter "FullyQualifiedName~AndroidDotnetToolTests": 4 passed, including fake-adb tests for invalid output cleanup and handle inheritance.