[dotnet test] Add androidtest project template and dotnet run instrumentation support#10862
Draft
jonathanpeppers wants to merge 15 commits intomainfrom
Draft
[dotnet test] Add androidtest project template and dotnet run instrumentation support#10862jonathanpeppers wants to merge 15 commits intomainfrom
androidtest project template and dotnet run instrumentation support#10862jonathanpeppers wants to merge 15 commits intomainfrom
Conversation
rmarinho
reviewed
Mar 3, 2026
|
|
||
| static class AdbHelper | ||
| { | ||
| public static ProcessStartInfo CreateStartInfo (string adbPath, string? adbTarget, string arguments) |
Member
There was a problem hiding this comment.
Does this make sense going to android-tools? and use that nupkg?
Member
Author
There was a problem hiding this comment.
Yes, we should open a PR here to test out any new Adb APIs from dotnet/android-tools. None are merged yet, right?
It would be proof the new APIs work, though.
…trumentation support Context: #10683 Add a new `androidtest` .NET project template that creates an Android test project using MSTest with Microsoft.Testing.Platform. The template includes a TestInstrumentation class that runs tests via `am instrument` and reports results (passed/failed/skipped) back through Android's instrumentation protocol. Add `--instrument` option to Microsoft.Android.Run so `dotnet run` can launch test projects via `am instrument -w` instead of `am start`. Fix an issue where NuGet packages like MSTest add assemblies as both properly-resolved publish assets (with DestinationSubPath metadata) and as None items (without DestinationSubPath) that flow into `_SourceItemsToCopyToPublishDirectory`. When NuGet conflict resolution arbitrarily picks the None-based copy, the assembly loses its DestinationSubPath and TargetPath metadata, causing it to either be missing from the APK or deployed to the wrong directory on device. The fix conditionally removes only items without DestinationSubPath, then re-adds any assemblies that were completely stripped with a cleared TargetPath so that downstream targets (ProcessAssemblies, FastDeploy) can set the correct per-architecture paths. Changes: - New `androidtest` template under src/Microsoft.Android.Templates/ - New GetAndroidInstrumentationName MSBuild task to resolve the instrumentation runner class from AndroidManifest.xml - Updated Microsoft.Android.Sdk.Application.targets to pass `--instrument` when EnableMSTestRunner is true - Added AdbHelper class to Microsoft.Android.Run for shared adb process management - Added `--instrument` CLI option to Microsoft.Android.Run/Program.cs - Fixed _ComputeFilesToPublishForRuntimeIdentifiers to preserve properly-resolved publish assets and recover missing assemblies - Added DotNetNewAndroidTest device integration test TODO: - In a future PR, add support for `dotnet test` end-to-end.
16a61dc to
f121c7c
Compare
Check pe.HasMetadata before calling GetMetadataReader() to avoid InvalidOperationException when processing native DLLs (e.g. from BenchmarkDotNet/TraceEvent NuGet packages) that don't have .NET metadata. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jonathanpeppers
added a commit
to jonathanpeppers/iOSDotNetTest
that referenced
this pull request
Mar 10, 2026
MSTest test platform assemblies fail AOT compilation because the IL linker trims types (DoesNotReturnAttribute, MemberNotNullWhenAttribute) from Microsoft.TestPlatform.CoreUtilities that are still referenced via custom attributes in other test platform DLLs. Use MtouchInterpreter=all to bypass AOT and run all assemblies through the interpreter instead. See: dotnet/android#10862 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
18 tasks
…tifiers Compare by filename instead of full ItemSpec path when checking if assemblies from _SourceItemsToCopyToPublishDirectory already exist in ResolvedFileToPublish. Native DLLs like KernelTraceControl.dll (from Microsoft.Diagnostics.Tracing.TraceEvent NuGet) can exist under different paths, causing the old Remove-by-path to miss them and re-add duplicates that crash GetPerArchAssemblies with XALNS7004. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…meIdentifiers When _SourceItemsToCopyToPublishDirectory contains multiple items with the same filename from different source paths (e.g., KernelTraceControl.dll), _MissingAssemblyNames ends up with duplicate entries. These duplicates get re-added to ResolvedFileToPublish, causing a Dictionary duplicate key error in GetPerArchAssemblies (XALNS7004). Use RemoveDuplicates task to deduplicate by filename before re-adding, following the same pattern already used for NativeAOT deduplication. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
NuGet packages like microsoft.diagnostics.tracing.traceevent (a
dependency of BenchmarkDotNet) include native Windows DLLs under
lib/native/{arch}/ paths. These were flowing through
_SourceItemsToCopyToPublishDirectory into _MissingAssemblyNames and
being re-added to ResolvedFileToPublish. When the linker tried to
load them with Mono.Cecil, it threw BadImageFormatException
(XALNS7000/XA0009).
Filter out items whose OriginalItemSpec contains a /native/ path
segment, which covers both lib/native/ and runtimes/*/native/ NuGet
layout conventions.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
NuGet packages like Microsoft.Testing.Extensions.CodeCoverage ship native .so files under runtimes/linux-x64/native/. The .NET SDK stamps RuntimeIdentifier=android-arm64 metadata on all ResolvedFileToPublish items during inner builds, causing ProcessNativeLibraries to treat these non-Android libraries as valid Android native libs. This leads to spurious XA0141 warnings about 16 KB page alignment. Use %(PathInPackage) metadata to detect the actual source RID and exclude .so files from non-Android runtimes (linux-x64, linux-musl-x64, osx-x64, win-x64, etc.) while preserving android-* and linux-bionic-* RIDs which are valid Android targets. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jonathanpeppers
added a commit
that referenced
this pull request
Mar 16, 2026
…native Windows DLLs (#10938) Context: #10862 ## Summary Extracted from PR #10862: Add `PEReader.HasMetadata` checks before calling `GetMetadataReader()` across all code paths that use `PEReader` to prevent exceptions when processing native (non-.NET) PE DLLs. ## Problem When NuGet packages (like MSTest) include native Windows `.dll` files that flow into `ResolvedFileToPublish`, calling `PEReader.GetMetadataReader()` on these files throws an exception because they are not .NET assemblies and have no CLI metadata. ## Fix Added `PEReader.HasMetadata` guards before `GetMetadataReader()` calls across all relevant code paths. In MSBuild tasks with log access, a `LogDebugMessage()` call is included when skipping non-.NET assemblies. Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
…esToPublishForRuntimeIdentifiers Instead of removing all _SourceItemsToCopyToPublishDirectory items and then recovering missing assemblies through temp item groups and dedup, just set DestinationSubPath on .dll/.pdb items that lack it before the Remove. Items with DestinationSubPath survive; everything else is stripped as before. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Some NuGet packages (e.g. MSTest.TestFramework) add assemblies as @(Reference) items with CopyToOutputDirectory metadata via their .targets files. This causes the assemblies to flow into @(_SourceItemsToCopyToPublishDirectory) in addition to the normal reference resolution path, creating duplicate entries in @(ResolvedFileToPublish) that crash downstream tasks like GetPerArchAssemblies with 'duplicate key' errors. Add _PatchNuGetReferenceMetadata target that strips CopyToOutputDirectory from all @(Reference) items during the inner per-RID publish build, so they only flow through normal reference resolution. This allows reverting _ComputeFilesToPublishForRuntimeIdentifiers back to the simple Remove of @(_SourceItemsToCopyToPublishDirectory). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context: #10683
Add a new
androidtest.NET project template that creates an Android test project using MSTest with Microsoft.Testing.Platform. The template includes a TestInstrumentation class that runs tests viaam instrumentand reports results (passed/failed/skipped) back through Android's instrumentation protocol.Add
--instrumentoption to Microsoft.Android.Run sodotnet runcan launch test projects viaam instrument -winstead ofam start.Fix an issue where NuGet packages like MSTest add assemblies as both properly-resolved publish assets (with DestinationSubPath metadata) and as None items (without DestinationSubPath) that flow into
_SourceItemsToCopyToPublishDirectory. When NuGet conflict resolution arbitrarily picks the None-based copy, the assembly loses its DestinationSubPath and TargetPath metadata, causing it to either be missing from the APK or deployed to the wrong directory on device. The fix conditionally removes only items without DestinationSubPath, then re-adds any assemblies that were completely stripped with a cleared TargetPath so that downstream targets (ProcessAssemblies, FastDeploy) can set the correct per-architecture paths.Changes:
androidtesttemplate under src/Microsoft.Android.Templates/--instrumentwhen EnableMSTestRunner is true--instrumentCLI option to Microsoft.Android.Run/Program.csTODO:
dotnet testend-to-end.