Skip to content

[Microsoft.Android.Run] Add --console mode for instrumentation output - #13651

Open
simonrozsival wants to merge 3 commits into
mainfrom
dev/simonrozsival/android-run-console
Open

simonrozsival wants to merge 3 commits into
mainfrom
dev/simonrozsival/android-run-console

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

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.

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>
@simonrozsival simonrozsival changed the title dev/simonrozsival/android run console [Microsoft.Android.Run] Add --console mode for instrumentation output Nov 19, 2026
@simonrozsival
simonrozsival marked this pull request as ready for review November 19, 2026 12:16
Copilot AI balanced review requested due to automatic review settings November 19, 2026 12:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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 --console with 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

Comment thread src/Microsoft.Android.Run/Program.cs Resolved
Comment thread src/Microsoft.Android.Run/Program.cs Resolved
Comment thread src/Microsoft.Android.Run/Program.cs Resolved
simonrozsival and others added 2 commits November 19, 2026 16:27
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@jonathanpeppers jonathanpeppers left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was looking at the design of this PR stack, and I wonder if another approach first would be nicer:

  • dotnet run MyActivity.cs
  • dotnet 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?

@simonrozsival

Copy link
Copy Markdown
Member Author

I wanted to introduce the capability to just run Main in an instrumentation for 2 reasons:

We can mark this feature as experimental until we are confident in it.

I was looking at the design of this PR stack, and I wonder if another approach first would be nicer:

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.

This branch has not been deployed

No deployments
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