Skip to content

[Mono.Android] Fix build warnings in Mono.Android and build tooling#11958

Closed
simonrozsival wants to merge 4 commits into
dotnet:mainfrom
simonrozsival:dev/simonrozsival/mono-android-warning-fixes
Closed

[Mono.Android] Fix build warnings in Mono.Android and build tooling#11958
simonrozsival wants to merge 4 commits into
dotnet:mainfrom
simonrozsival:dev/simonrozsival/mono-android-warning-fixes

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Fixes several compiler/analyzer warnings, primarily surfaced when building Mono.Android.csproj.

Changes

  • Nullability (CS86xx): Add nullable annotations and explicit null checks in JNIEnv, TypeManager, and the generated AnimatorSet/ValueAnimator bindings.
  • BluetoothDevice: Annotate the obsoleted ConnectGatt overloads with [ObsoletedOSPlatform("android37.0")].
  • CS0649: Suppress "field is never assigned" on internal structs that are populated from native code (JavaMarshalRegisteredPeers, JniRemappingLookup, and jnienv-gen's Generator).
  • CA1416: Suppress the platform-compatibility warning for the Windows-only registry access in the test helper AndroidSdkResolver.
  • CS0436: Add to NoWarn in Mono.Android.csproj.
  • PublicAPI: Remove duplicate PublicAPI.Shipped.txt entries (API-36.1, API-37).
  • Cleanup: Remove a duplicate using System.Globalization; in the InlineData test helper and widen AndroidMessageHandler.CreateHttpRequestException's message parameter to nullable.

No functional/behavioral changes — warning cleanup only.

Copilot AI review requested due to automatic review settings July 2, 2026 14:46

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.

Pull request overview

This PR focuses on reducing compiler/analyzer noise in the .NET for Android codebase by tightening nullability annotations, adding targeted suppressions/attributes, and cleaning up PublicAPI lists, primarily in src/Mono.Android and related build/test tooling.

Changes:

  • Adjust nullability and add explicit null-handling in core interop/runtime code (JNIEnv, TypeManager) and in generated bindings (AnimatorSet, ValueAnimator).
  • Add/suppress warnings where appropriate (CA1416 in test helper, CS0649 for native-populated structs, CS0436 in Mono.Android.csproj).
  • Clean up API surface bookkeeping (remove duplicate PublicAPI.Shipped.txt entries) and minor test/tooling cleanup.
Show a summary per file
File Description
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/AndroidSdkResolver.cs Suppresses CA1416 around Windows-only registry access in test tooling
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/InlineData.cs Removes a duplicate using
src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs Updates helper signature to accept nullable message
src/Mono.Android/PublicAPI/API-37/PublicAPI.Shipped.txt Removes a duplicate shipped API entry
src/Mono.Android/PublicAPI/API-36.1/PublicAPI.Shipped.txt Removes a duplicate shipped API entry
src/Mono.Android/Mono.Android.csproj Adds CS0436 to NoWarn
src/Mono.Android/Microsoft.Android.Runtime/JniRemappingLookup.cs Suppresses CS0649 for a native-populated struct
src/Mono.Android/Microsoft.Android.Runtime/JavaMarshalRegisteredPeers.cs Suppresses CS0649 for an internal native-mirrored struct
src/Mono.Android/Java.Interop/TypeManager.cs Adds null checks for strings coming from native pointers / mapping lookups
src/Mono.Android/Android.Runtime/JNIEnv.cs Enables nullable context and tightens null-handling for typemap/array helpers
src/Mono.Android/Android.Bluetooth/BluetoothDevice.cs Adds ObsoletedOSPlatform("android37.0") to obsolete overloads
src/Mono.Android/Android.Animation/ValueAnimator.cs Addresses nullable warnings in generated binding code
src/Mono.Android/Android.Animation/AnimatorSet.cs Addresses nullable warnings in generated binding code
build-tools/jnienv-gen/Generator.cs Suppresses CS0649 for generator metadata populated indirectly

Copilot's findings

  • Files reviewed: 14/14 changed files
  • Comments generated: 4

Comment thread src/Mono.Android/Android.Animation/ValueAnimator.cs Outdated
Comment thread src/Mono.Android/Android.Animation/AnimatorSet.cs Outdated
Comment thread src/Mono.Android/Android.Runtime/JNIEnv.cs Outdated
@jonathanpeppers

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Android PR Reviewer completed successfully!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 Code review

Focused, well-scoped warning cleanup. Nice touches: the pragma suppressions carry explanatory comments, the CS0649 suppressions on native-mirror structs are appropriate, the new argument checks use nameof, and the duplicate using System.Globalization; removal in InlineData.cs is genuine dead-code removal. I read the full source around each change; everything below is non-blocking and surfaced inline.

⚠️ Worth fixing (1)

  • Incomplete PublicAPI dedup — the cleanup removed the VP9Profile2HDR duplicate but an exact-duplicate VP9Profile3HDR line remains right below it in both API-36.1 and API-37/PublicAPI.Shipped.txt (lines 66145/66146), leaving the same RS0025 duplicate-entry warning this PR is clearing. Details inline.

💡 Suggestions (3)

  • #nullable enable in JNIEnv.cs is redundant — nullable is already enabled project-wide (Mono.Android.csproj:24).
  • string? type = GetClassNameFromInstance (array) in JNIEnv.cs, plus its type == null check, are now dead since the same PR made that method return non-null string.
  • AnimatorSet.cs/ValueAnimator.cs use ?? throw for the object return but keep ! on __this; the generated-mirror pattern in sibling files uses ! for that return.

Accuracy note: a few changes do subtly alter behavior on previously null/NRE paths — GetClassNameFromInstance, AnimatorSet/ValueAnimator.SetDuration, and the managed→java typemap now throw a clear exception where they would previously have returned null. That is an improvement, just not strictly “no behavioral change.”

CI: no checks had reported at review time (status still pending), so a green build is unconfirmed; mergeable_state was blocked.

Verdict: ⚠️ one small follow-up (the leftover duplicate) plus optional tidy-ups — otherwise a clean change. Submitting as a comment only.

Generated by Android PR Reviewer for #11958 · 417.8 AIC · ⌖ 23.6 AIC · ⊞ 6.8K
Comment /review to run again

Comment thread src/Mono.Android/PublicAPI/API-36.1/PublicAPI.Shipped.txt Outdated
Comment thread src/Mono.Android/Android.Runtime/JNIEnv.cs Outdated
Comment thread src/Mono.Android/Android.Runtime/JNIEnv.cs Outdated
Comment thread src/Mono.Android/Android.Animation/AnimatorSet.cs
simonrozsival and others added 4 commits July 3, 2026 12:36
Addresses several compiler/analyzer warnings, primarily in Mono.Android.csproj:

- Add nullable annotations and null checks in JNIEnv, TypeManager, and
  generated bindings (AnimatorSet, ValueAnimator) to resolve CS86xx nullability warnings.
- Annotate obsoleted BluetoothDevice.ConnectGatt overloads with ObsoletedOSPlatform.
- Suppress CS0649 on internal control-block/replacement-method structs that are
  populated from native code (JavaMarshalRegisteredPeers, JniRemappingLookup, jnienv-gen).
- Suppress CA1416 for the Windows-only registry access in AndroidSdkResolver.
- Allow CS0436 in Mono.Android.csproj NoWarn.
- Remove duplicate PublicAPI.Shipped.txt entries (API-36.1, API-37).
- Remove duplicate using in InlineData test helper.
- Widen AndroidMessageHandler.CreateHttpRequestException message parameter to nullable.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ve and pragma

- Remove leftover byte-identical VP9Profile3HDR duplicate in API-36.1 and
  API-37 PublicAPI.Shipped.txt (clears the RS0025 the PR targets).
- Drop redundant per-file '#nullable enable' in JNIEnv.cs (nullable is enabled
  project-wide) and remove the now-dead 'string?'/'type == null' check since
  GetClassNameFromInstance returns non-null.
- Replace misleading CA1416 pragma with an analyzer-recognized
  OperatingSystem.IsWindows() guard in AndroidSdkResolver.cs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…line

The API-37 (CinnamonBun preview) PublicAPI baseline (PublicAPI.Shipped.txt is a
copy of API-36.1) is out of date with the generated android-37 bindings, so the
PublicApiAnalyzer reports thousands of RS0016 (undeclared public API) and RS0017
(declared-but-missing) errors that are unrelated to this warning-cleanup PR.

Suppress RS0016/RS0017 via NoWarn (which overrides the WarningsAsErrors escalation)
to unblock the build. A follow-up PR will regenerate the API-37
PublicAPI.Shipped.txt / PublicAPI.Unshipped.txt files to declare the new API surface.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/mono-android-warning-fixes branch from 3aaec9c to d8745bb Compare July 3, 2026 11:00
@simonrozsival simonrozsival changed the title Fix build warnings in Mono.Android and build tooling [Mono.Android] Fix build warnings in Mono.Android and build tooling Jul 3, 2026
@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Jul 3, 2026
Comment on lines +523 to +526
#pragma warning disable CS0649 // Field 'JniFunction.Prebind' is never assigned to, and will always have its default value false
// If true, then we initialize the binding on the static ctor, we dont lazy-define it
public bool Prebind;
#pragma warning restore CS0649

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.

This was removed in main, so I think we can rebase.

@simonrozsival

Copy link
Copy Markdown
Member Author

Superseded by #11999

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants