Skip to content

[tests] Enable compiler warnings-as-errors for Mono.Android tests - #12761

Open
simonrozsival wants to merge 2 commits into
mainfrom
simonrozsival-ci-warning-cleanup
Open

simonrozsival wants to merge 2 commits into
mainfrom
simonrozsival-ci-warning-cleanup

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Sep 11, 2026

Copy link
Copy Markdown
Member

Summary

Recurring warnings in the Mono.Android device tests obscure new diagnostics and keep the project on the repository's compiler-warning exemption list. Clean up those warnings and remove the exemption so CI, and local builds using $(_AndroidTreatWarningsAsErrors), enforce compiler diagnostics.

  • Enable nullable checking in affected test files and add explicit checks without null-forgiving operators.
  • Fix unused declarations, member hiding, equality hashing, and obsolete certificate loading. Add an equality/hash-code regression test.
  • Preserve intentional obsolete-API coverage with narrowly scoped suppressions identifying the obsolete API and Android version.
  • Restrict CS0108/CS0436 suppression to generated test bindings: friend access exposes otherwise-internal Mono.Android members and delegates.
  • Compile test JARs with javac --release, including Java 8 version notation, and include build configuration files in incremental inputs.
  • Replace avoidable reflection with existing friend-access APIs, propagate constructor requirements through test helpers, and replace the obsolete preservation attribute with a rooted dynamic dependency. Keep the shared NativeAOT collection probe compatible with non-friend assemblies.
  • Declare the XML serialization regression's dynamic-code requirement as well as its trimming requirement, fixing IL3050 in the NativeAOT lane without excluding the test.

This enables C# compiler warnings-as-errors, not blanket enforcement for all build tools. Runtime regression tests deliberately exercise trim-unsafe and AOT-unsafe APIs and dependencies. Their linker and NativeAOT analysis warnings remain visible and non-fatal through $(ILLinkTreatWarningsAsErrors)=false and $(IlcTreatWarningsAsErrors)=false; IL2037 remains an error in both pipelines. NativeAOT needs its own setting because it otherwise inherits $(TreatWarningsAsErrors) independently of ILLink.

Java compilation rationale and compatibility

The change in build-tools/scripts/Jar.targets addresses the JDK 25 warning [options] location of system modules is not set in conjunction with -source 17. Both $(JavacSourceVersion) and $(JavacTargetVersion) resolve to 17 in the build verified here.

-source 17 -target 17 selects Java 17 syntax and class-file format, but still compiles against the host JDK's platform APIs. --release 17 selects those same language and bytecode versions and also uses Java 17 platform API signatures. This removes the warning without disabling it and prevents accidental references to newer host-JDK APIs. The 1.8 special case translates the older version notation to --release 8.

This is not an equivalent replacement for every configuration. It narrows the available Java platform APIs and uses $(JavacTargetVersion) for the language level too, so the unconditional replacement no longer honors an independently configured $(JavacSourceVersion). Java SE release compatibility is also not an Android API-level check. The verified 17/17 configuration works, but preserving independent source/target settings in this shared helper would require retaining the original flags for that case rather than silently ignoring the source version; that guard is not part of this change.

Validation

  • Clean Debug/CoreCLR build with trimmable typemaps, $(_AndroidTreatWarningsAsErrors)=true, and MSBuild -warnaserror: 0 warnings, 0 errors, including after the NativeAOT follow-up fix.
  • Release/CoreCLR build and installation succeeded with compiler enforcement. A clean Release build still reports 13 linker/packaging warnings: startup-hook/TypeConverter coverage, dependency trim diagnostics, and XA4304. These are not suppressed by this change.
  • Reproduced the NativeAOT IL3050 failure locally with CI's $(TestsFlavor)=NativeAOT and $(PublishAot)=true settings. After the fix, the NativeAOT package builds and installs with C# warnings-as-errors enabled; expected NativeAOT analysis warnings remain visible.
  • Full NativeAOT execution on the arm64 emulator: 701 passed, 11 skipped, 0 failed. Confirmed TrimmingDefaultValueAttribute passed rather than being excluded.
  • The new InterfaceInfo_EqualValuesHaveEqualHashCodes regression passed on an API 35 emulator in Release.
  • Inspected the linked Release assembly to confirm the expression regression fixture and its delegate survive trimming.

Validation used the local .NET 11 RC2 SDK and covers CoreCLR/trimmable and NativeAOT execution. Mono was not executed locally; the SDK's default check rejects $(UseMonoRuntime)=true with NETSDK1242.


  • Useful description of why the change is necessary.
  • Links to issues fixed: N/A; cleanup identified from CI warnings.
  • Unit tests: equality/hash-code regression added and run; NativeAOT lane and XML regression verified.

Recurring warnings obscure new diagnostics in the Mono.Android device
tests.  Fix nullable contexts, unused declarations, member hiding,
equality hashing, obsolete certificate loading, and Java compilation
settings so a clean Debug/CoreCLR build is warning-free.

Preserve intentional obsolete-API coverage with narrowly scoped,
documented suppressions.  Limit generated binding suppressions to the
test project, where friend access exposes internal Mono.Android members.
Replace avoidable reflection and describe constructor requirements to
the trimmer without breaking the shared NativeAOT probe.

Remove the compiler-warning exemption from the repository CI policy.
Keep existing linker warnings visible and non-fatal for intentional
trim-unsafe regression coverage and dependencies; IL2037 remains an error.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 11, 2026 08:27

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.

Copilot review overview

🟡 Changes recommended

One or more issues must be addressed before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 High severity

New issues introduced by this change (1)
Severity Finding
High severity tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​ConstructorActivationTests.cs — Do not cast ConcurrentDictionary to non-generic IDictionary
What changed in this PR

Enables compiler warnings-as-errors for Mono.Android tests while preserving intentional linker and obsolete-API coverage.

Changes:

  • Adds nullable checks, warning suppressions, and an equality/hash-code regression test.
  • Replaces avoidable reflection and improves trimming annotations.
  • Updates test JAR compilation to use javac --release and track project inputs.

Review finding: reflection activation tests will fail because ConcurrentDictionary is cast to non-generic IDictionary.

File Description
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Xamarin.Android.RuntimeTests/​TestInstrumentation.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Xamarin.Android.RuntimeTests/​NonJavaObject.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Xamarin.Android.RuntimeTests/​MyFragment.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Xamarin.Android.Net/​TrustManagerMarshallingTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Xamarin.Android.Net/​AndroidMessageHandlerTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System/​TimeZoneTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System/​ExceptionTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System/​AppDomainTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System.Xml/​XmlSerializer.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System.Net/​SslTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System.Net/​ProxyTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System.Net/​NetworkInterfaces.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​System.Linq/​LinqExpressionTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Mono.Android.NET-Tests.csproj Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Lang/​ObjectTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​TrimmableTypeMapTypeManagerTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​TrimmableTypeMapRuntimeCoverageTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​RawInterfaceCollectionHolder.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JnienvTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JavaObjectExtensionsTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​JavaConvertTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​ExportTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Java.Interop/​ConstructorActivationTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Android.Runtime/​JnienvArrayMarshaling.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Android.OS/​BundleTest.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​Android.Graphics/​NinePatchTests.cs Updated as part of this pull request.
tests/​Mono.Android-Tests/​Mono.Android-Tests/​.editorconfig Updated as part of this pull request.
Directory.Build.props Updated as part of this pull request.
build-tools/​scripts/​Jar.targets Updated as part of this pull request.

Comment on lines +677 to +678
return cacheField.GetValue (null) as IDictionary ??
throw new InvalidOperationException ("The reflection activation constructor cache is not a dictionary.");
The NativeAOT lane reports IL3050 for the XML serialization regression.
RequiresUnreferencedCode only describes trimming requirements; also
declare RequiresDynamicCode without excluding the test.

NativeAOT analysis inherits TreatWarningsAsErrors independently of
ILLink.  Keep its expected regression-suite and dependency diagnostics
visible and non-fatal, matching the existing linker policy, while C#
compiler diagnostics and IL2037 remain errors.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

2 participants