Repository navigation
[tests] Add TrimmingTests: on-device trimming and NativeAOT tests - #13657
simonrozsival wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Parallel tests can lose results when shared logcat is cleared, unregistered output directories bypass artifact cleanup and attachments, and the activation test can pass by reusing a cached wrapper without exercising new-peer activation.
3 open issues
What changed in this PR
Adds isolated on-device trimming tests for .NET for Android, complementing the existing shared test app. Each scenario builds its own app to verify runtime behavior and retained code under CoreCLR full/partial trimming and NativeAOT.
Changes:
- Adds a build-and-run harness with warning checks and trimming expectations read from linked assemblies or NativeAOT output.
- Adds binding and Java peer activation cases, with the known boxing-retention issue explicitly ignored.
- Integrates the suite into solutions, emulator CI jobs, and testing guidance.
| File | Description |
|---|---|
tests/TrimmingTests/TrimmingTests.csproj |
Defines the test project and copies standalone cases. |
tests/TrimmingTests/TrimmingTestBase.cs |
Builds, launches, and validates isolated apps. |
tests/TrimmingTests/TrimmingExpectations.cs |
Checks expectations against trimmed output. |
tests/TrimmingTests/Tests/JavaPeerActivationTests.cs |
Runs generic peer activation coverage. |
tests/TrimmingTests/Tests/BindingTrimmingTests.cs |
Runs binding retention and activation cases. |
tests/TrimmingTests/Cases/UsedBindings.cs |
Exercises Java callbacks into a managed listener. |
tests/TrimmingTests/Cases/UnusedBoxingBindings.cs |
Records expected boxing-binding removal for a known issue. |
tests/TrimmingTests/Cases/UnusedBindings.cs |
Checks unused bindings and app code are removed. |
tests/TrimmingTests/Cases/TrimmingTests.Expectations/TrimmingTests.Expectations.csproj |
Defines the shared expectation library. |
tests/TrimmingTests/Cases/TrimmingTests.Expectations/Expectations.cs |
Adds string-based expectation attributes. |
tests/TrimmingTests/Cases/JavaObjectActivation.cs |
Tests most-derived Java binding activation. |
tests/TrimmingTests/Cases/JavaInterfaceInvokers.cs |
Exercises interface invokers and member trimming. |
tests/TrimmingTests/Cases/JavaExceptions.cs |
Tests bound Java exception activation. |
tests/TrimmingTests/Cases/GenericPeerArrayElement.cs |
Exercises closed generic peer array activation. |
tests/TrimmingTests/Cases/Directory.Build.targets |
Isolates cases, disables NativeAOT inlining, and imports case items. |
tests/TrimmingTests/Cases/Directory.Build.props |
Shields cases from repository build properties. |
tests/TrimmingTests/AssemblyInfo.cs |
Limits concurrent tests to two. |
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs |
Omits unsupported arguments for file-based apps. |
Microsoft.Android.slnx |
Includes the new suite in the main solution. |
Microsoft.Android.Build.Tasks.slnx |
Includes the suite in the build-task solution. |
build-tools/automation/yaml-templates/stage-msbuild-emulator-tests.yaml |
Adds a reusable trimming emulator job. |
build-tools/automation/azure-pipelines-public.yaml |
Adds public-pipeline trimming coverage. |
.github/skills/tests/SKILL.md |
Adds trimming tests to test-selection guidance. |
.github/skills/tests/references/test-catalog.md |
Documents case authoring and suite execution. |
Review effort: Balanced
f593448 to
4613df5
Compare
4613df5 to
b66d195
Compare
Context: #13618 Trimming bugs show up only at runtime in a trimmed app, and the existing device tests run inside one large test app, where any test can keep a type alive for another. TrimmingTests builds a purpose-built app for each scenario, launches it on a device, and checks both its result and what trimming kept. ### Cases Each case is a single-file app (`dotnet build Case.cs`) in `tests/TrimmingTests/Cases/` with `#:property`/`#:package` directives; a case that needs Android resources, an `AndroidManifest.xml` or MSBuild items lives in `Cases/<Case>/` next to them (`Case.targets`). It logs `TRIMMING_TEST_RESULT: PASS` or `FAIL …` on startup. Cases are shielded from the repository's `Directory.Build.*` files and never root anything unless that is what they test. A test calls `BuildAndRun ("Cases/<Case>.cs", configuration)` and then `AssertTrimmingExpectations (app)`: * Every configuration builds `SignAndroidPackage` in Release: CoreCLR with `$(TrimMode)`=full, NativeAOT, and CoreCLR with `$(TrimMode)`=partial, the CoreCLR Release default. `[TrimmingConfigurations]` runs a test in all three or in the ones it lists. * The build must have no warnings except the codes the test passes as `expectedWarnings`, with `$(TrimmerSingleWarn)`=false. * The app is installed and launched with `adb`; its result is matched by process ID in a logcat stream started before the launch, so tests in a fixture run in parallel on one device without clearing logcat. * Expectation attributes modelled on dotnet/runtime's `Mono.Linker.Tests.Cases.Expectations` (`[Kept]`, `[Removed]`, `[KeptMember]`, `[Kept|Removed][Type|Member]InAssembly`, `By = Tool.…`) are checked against ILLink's `linked/` assemblies or ILC's mstat file. They take only strings, so they keep nothing themselves. NativeAOT cases build with `JitNoInline=1` because the mstat omits inlined methods, and can check generic instantiations. In partial mode, assemblies ILLink doesn't trim must instead be kept whole. `DotNetCLI` skips arguments that file-based apps reject (`/noconsolelogger`, `/flp1`, `-m`, `-nodeReuse`). The first cases cover generic peer array activation (the bug fixed two commits ago), unused bindings being removed, a C# listener called from Java, Java exception and interface invoker activation, and boxed values. `UnusedBoxingBindings` is ignored for #13635: every app keeps `JavaConvert` and all the `Java.Lang` boxing bindings. ### CI The MSBuild emulator stage gets a `Trimming+Emulator` job in the public pipeline and in `stage-msbuild-emulator-tests.yaml`, running the suite with two workers on one emulator. Co-authored-by: Copilot App <[email protected]>
`am start -W` can return before the process exists, so `pidof` found nothing and the test reported a crash. Co-authored-by: Copilot App <[email protected]>
The proxy of an open generic definition keeps its activation constructor with [DynamicDependency]. Check that this doesn't keep generic peers the app doesn't use, nor make NativeAOT compile instantiations the app doesn't use. Co-authored-by: Copilot App <[email protected]>
…vation * DeviceTest.SetupTest () clears logcat before every test, which discarded the output of apps that tests running in parallel waited for. Skip it, and check (and restart) the device once per fixture. * Register each case's directory in TestOutputDirectories, so passing tests clean it up and failing ones attach its logs and binlogs. * JavaObjectActivation disposes its Integer wrapper before ArrayList.get (), so the case activates a new peer instead of returning the cached one. Co-authored-by: Copilot App <[email protected]>
On CI the test output directory is under $(BUILD_STAGINGDIRECTORY), outside the repository, so the case apps didn't see its NuGet.config and every restore failed with NU1102 for the 12.0.0 ILLink, ILCompiler and runtime packs. Copy NuGet.config into each case directory, as DotNetCLI.New () and XamarinProject.CopyNuGetConfig () do. Co-authored-by: Copilot App <[email protected]>
Convert untyped java.util.ArrayList, HashSet, and HashMap instances to IList<string>, ICollection<string>, and IDictionary<string, string> through JNIEnv.GetArray<T> (), which reaches JavaConvert with only a runtime Type. The app never names JavaList<T>, JavaCollection<T>, or JavaDictionary<K,V> or calls their FromJniHandle (), so only JavaConvert's open-definition mapping (#13656) can keep those methods under full trimming. Co-authored-by: Copilot App <[email protected]>
JNIEnv.GetArray<T> rejects collection-interface elements before reaching JavaConvert, so the trimming case fails in every configuration. Use CopyObjectArray<T> to exercise the intended converters without rooting the wrapper types. Validation: make leeroy CONFIGURATION=Debug MSBUILD_ARGS="-m:4" passed; ./dotnet-local.sh build tests/TrimmingTests/TrimmingTests.csproj -c Debug -v:minimal --no-restore passed; ADB_TARGET="-s emulator-5556" TEST_ANDROID_JDK_PATH=/Users/simon/android-toolchain/jdk-25 JAVA_HOME=/Users/simon/android-toolchain/jdk-25 ./dotnet-local.sh test bin/TestDebug/TrimmingTests/net11.0/TrimmingTests.dll passed on API 29 arm64 (21 passed, 3 skipped). Co-authored-by: Copilot App <[email protected]>
8bec35c to
f339c39
Compare
|
/review |
|
✅ Android PR Reviewer completed successfully! Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
To allow these domains, add them to the network:
allowed:
- defaults
- "169.254.169.254"See Network Configuration for more information.
|
There was a problem hiding this comment.
⚠️ Needs Changes
0 errors · 1 warning · 0 suggestions. The expectation checker can confuse generic and non-generic method overloads, allowing incorrect trimming output to satisfy an expectation. The inline comment includes a locally reproduced example and the affected ILLink/NativeAOT matching paths.
The isolated case apps, string-only expectation attributes, and separate full/partial/NativeAOT configurations provide useful coverage without the shared test app accidentally rooting the tested types. The earlier activation, logcat-clearing, and output-cleanup feedback is addressed.
CI: GitHub reports 28 successful checks, 10 running, and 1 queued, with no failures reported; the new Trimming+Emulator job succeeded. Overall CI is not yet confirmed green. Detailed Azure build/timeline verification was blocked by CLI initialization/authentication errors.
Review validation: A standalone .NET 11 probe compiled the PR's actual metadata reader and reproduced IsKept(M<T>()) == true with only M() present. The Android device suite was not rerun locally.
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
169.254.169.254
To allow these domains, add them to the network.allowed list in your workflow frontmatter:
network:
allowed:
- defaults
- "169.254.169.254"See Network Configuration for more information.
Generated by Android PR Reviewer for #13657 · copilot · gpt61 · 53.4 AIC · ⌖ 7.8 AIC · ⊞ 20.3K
Comment /review to run again
Co-authored-by: Copilot App <[email protected]>
|
@dalexsoto review |
1 similar comment
|
@dalexsoto review |

Context: #13618
Trimming bugs show up only at runtime in a trimmed app, and the existing device tests run inside one large test app, where any test can keep a type alive for another. TrimmingTests builds a purpose-built app for each scenario, launches it on a device, and checks both its result and what trimming kept.
Cases
Each case is a single-file app (
dotnet build Case.cs) intests/TrimmingTests/Cases/with#:property/#:packagedirectives; a case that needs Android resources, anAndroidManifest.xmlor MSBuild items lives inCases/<Case>/next to them (Case.targets). It logsTRIMMING_TEST_RESULT: PASSorFAIL …on startup. Cases are shielded from the repository'sDirectory.Build.*files and never root anything unless that is what they test.A test calls
BuildAndRun ("Cases/<Case>.cs", configuration)and thenAssertTrimmingExpectations (app):SignAndroidPackagein Release: CoreCLR with$(TrimMode)=full, NativeAOT, and CoreCLR with$(TrimMode)=partial, the CoreCLR Release default.[TrimmingConfigurations]runs a test in all three or in the ones it lists.expectedWarnings, with$(TrimmerSingleWarn)=false.adb; its result is matched by process ID in a logcat stream started before the launch, so tests in a fixture run in parallel on one device without clearing logcat.Mono.Linker.Tests.Cases.Expectations([Kept],[Removed],[KeptMember],[Kept|Removed][Type|Member]InAssembly,By = Tool.…) are checked against ILLink'slinked/assemblies or ILC's mstat file. They take only strings, so they keep nothing themselves. NativeAOT cases build withJitNoInline=1because the mstat omits inlined methods, and can check generic instantiations. In partial mode, assemblies ILLink doesn't trim must instead be kept whole.DotNetCLIskips arguments that file-based apps reject (/noconsolelogger,/flp1,-m,-nodeReuse).The first cases cover generic peer array activation (the bug fixed earlier in this stack, including a check that the generated
[DynamicDependency]keeps neither unused generic peers nor, on NativeAOT, instantiations the app doesn't use), unused bindings being removed, a C# listener called from Java, Java exception and interface invoker activation, and boxed values.UnusedBoxingBindingsis ignored for #13635: every app keepsJavaConvertand all theJava.Langboxing bindings.CI
The MSBuild emulator stage gets a
Trimming+Emulatorjob in the public pipeline and instage-msbuild-emulator-tests.yaml, running the suite with two workers on one emulator.Validation
On an API 35 arm64 emulator with a locally built SDK (
make leeroy), the top of this stack:99 tests: 88 passed and 11 skipped (ignored with links to open issues). Making the generic peer case construct
GenericArrayElement<long>and an unused generic peer makes its over-preservation checks fail on CoreCLR full trimming and NativeAOT. Failure checks: turning off a fix (_DefaultValueAttributeSupport=falsefor #9525,UseTypeMapAttributesForJavaDictionaryValueTypeLookups=falseand an extra[Removed]instantiation for #12793, treating Mono.Android as untrimmed in partial mode) makes the corresponding test fail; the ignored cases fail today for the reason in their issue.