[Mono.Android] Fix build warnings in hand-written code#11999
[Mono.Android] Fix build warnings in hand-written code#11999simonrozsival wants to merge 15 commits into
Conversation
Match the nullability of overridden base-class parameters (TypeConverter, SynchronizationContext, Java stream adapters, Color/AndroidBitmapInfo equality, AdapterView.RawAdapter) so overrides no longer widen non-nullable base parameters, guarding newly-nullable arguments where the body dereferences them.
JavaObject.ToString() overrides Java.Lang.Object.ToString() (non-null), so return a non-null string instead of string?.
Match the parameter nullability of the implemented interface members (IComparer, ISpliterator, ISetCookie, IX509TrustManager/ISSLSession, IXmlPullParser). Guard TrustManager callbacks with ArgumentNullException.ThrowIfNull and return null early from GetNamespace for a null prefix.
Add the 'new' keyword to members that intentionally hide an inherited virtual member (SparseArray<E>.Get/ValueAt, ArrayAdapter<T>.GetItem/Sort, JavaList.Equals(Java.Lang.Object)).
Add the 'new' keyword to members that intentionally hide an inherited member: generic collection specializations (JavaCollection/JavaList/JavaSet/JavaDictionary/ArrayAdapter/BaseAdapter), static helpers (FromJniHandle/ToLocalJniHandle/CreateFromResource), IXmlResourceParser members disambiguating IAttributeSet/IXmlPullParser, StringBuffer/StringBuilder.Append, AsyncTask.class_ref, and JavaProxyThrowable.InnerException.
The API-37 public API baseline was missing ~4570 members (generated bindings plus a few hand-written types), and the nullability fixes in earlier commits changed the annotations of several public members. Populate PublicAPI.Unshipped.txt via the RS0016 code fix and add *REMOVED* markers for the stale signatures (RS0017) so the PublicApiAnalyzer passes with RS0016/RS0017 enforced.
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce build warnings in src/Mono.Android by aligning nullability annotations with overridden/interface members, adding new where members intentionally hide inherited members, and updating PublicAPI baselines so the API analyzers (RS0016/RS0017) pass.
Changes:
- Adjusted nullability and argument validation across various hand-written runtime, networking, and System.Drawing converter code paths to satisfy CS8764/CS8765/CS8767.
- Added
newon members that intentionally hide inherited members to address CS0108/CS0114. - Updated API shipped baselines and warning suppression configuration (
NoWarn) to keep analyzers enforced while reducing warning noise.
Show a summary per file
| File | Description |
|---|---|
| src/Mono.Android/Xamarin.Android.Net/ServerCertificateCustomValidator.cs | Nullability fixes for trust manager / SSL session APIs. |
| src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs | Nullability alignment for exception factory helper. |
| src/Mono.Android/System.Drawing/SizeFConverter.cs | TypeConverter override nullability + culture handling. |
| src/Mono.Android/System.Drawing/SizeConverter.cs | TypeConverter override nullability alignment. |
| src/Mono.Android/System.Drawing/RectangleConverter.cs | TypeConverter override nullability + culture handling. |
| src/Mono.Android/System.Drawing/PointConverter.cs | TypeConverter override nullability alignment. |
| src/Mono.Android/PublicAPI/API-37/PublicAPI.Shipped.txt | Shipped API baseline cleanup (duplicate entries removed). |
| src/Mono.Android/PublicAPI/API-36.1/PublicAPI.Shipped.txt | Shipped API baseline cleanup (duplicate entries removed). |
| src/Mono.Android/Org.Apache.Http.Impl.Cookie/BasicClientCookie.cs | Cookie setter nullability aligned to interface expectations. |
| src/Mono.Android/Mono.Android.csproj | Updates warning suppression list (notably removing IL3050 from NoWarn). |
| src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMapTypeManager.cs | Refactors runtime branching based on dynamic-code support; adds RequiresDynamicCode annotation. |
| src/Mono.Android/Microsoft.Android.Runtime/ManagedTypeManager.cs | Adds IL3050 suppression for reflection-based type manager. |
| src/Mono.Android/Microsoft.Android.Runtime/JniRemappingLookup.cs | Suppresses CS0649 for struct fields used for native data. |
| src/Mono.Android/Microsoft.Android.Runtime/JavaMarshalRegisteredPeers.cs | Suppresses CS0649 for mirrored native control-block struct. |
| src/Mono.Android/Java.Util/Spliterators.cs | Nullability alignment for TryAdvance consumer parameter. |
| src/Mono.Android/Java.Lang/StringBuilder.cs | Adds new on Append overload to acknowledge member hiding. |
| src/Mono.Android/Java.Lang/StringBuffer.cs | Adds new on Append overload to acknowledge member hiding. |
| src/Mono.Android/Java.Interop/TypeManager.cs | Nullability alignment for comparer + additional null checks in typemap lookup. |
| src/Mono.Android/Java.Interop/JavaPeerProxy.cs | Changes proxy-attribute ctor accessibility to protected. |
| src/Mono.Android/Java.Interop/JavaConvert.cs | Adjusts dynamic/AOT generic collection factory logic; adds RequiresDynamicCode and suppression path. |
| src/Mono.Android/Android.Widget/BaseAdapter.cs | Adds new to acknowledge static member hiding (class_ref). |
| src/Mono.Android/Android.Widget/ArrayAdapter.cs | Adds new to acknowledge member hiding (CreateFromResource, GetItem, Sort). |
| src/Mono.Android/Android.Widget/AdapterView.cs | Nullability alignment for RawAdapter override. |
| src/Mono.Android/Android.Util/SparseArray.cs | Adds new to acknowledge member hiding (Get, ValueAt). |
| src/Mono.Android/Android.Runtime/XmlReaderPullParser.cs | Adds new and fixes parameter nullability for XML parser adapter members. |
| src/Mono.Android/Android.Runtime/XmlPullParserReader.cs | Adds new to acknowledge member hiding (FromJniHandle). |
| src/Mono.Android/Android.Runtime/RuntimeNativeMethods.cs | Adjusts P/Invoke signature nullability (clr_typemap_managed_to_java). |
| src/Mono.Android/Android.Runtime/OutputStreamAdapter.cs | Stream override nullability alignment + explicit argument validation. |
| src/Mono.Android/Android.Runtime/JNIEnv.cs | Refines array creation logic for dynamic/AOT paths; propagates updated nullability for helpers. |
| src/Mono.Android/Android.Runtime/JavaSet.cs | Adds new for enumerator / FromJniHandle member hiding. |
| src/Mono.Android/Android.Runtime/JavaProxyThrowable.cs | Adds new keyword for intentionally hidden InnerException. |
| src/Mono.Android/Android.Runtime/JavaObject.cs | Ensures ToString() is non-null to match base contract. |
| src/Mono.Android/Android.Runtime/JavaList.cs | Adds new for hidden members (Equals/indexer/enumerator/FromJniHandle). |
| src/Mono.Android/Android.Runtime/JavaDictionary.cs | Adds new for hidden members (Keys/Values/enumerator/FromJniHandle). |
| src/Mono.Android/Android.Runtime/JavaCollection.cs | Adds new for hidden members (enumerator/FromJniHandle). |
| src/Mono.Android/Android.Runtime/InputStreamAdapter.cs | Stream override nullability alignment + explicit argument validation. |
| src/Mono.Android/Android.OS/AsyncTask.cs | Adds new to acknowledge static member hiding (class_ref). |
| src/Mono.Android/Android.Graphics/Color.cs | Nullability alignment for overrides + marshaler signature updates. |
| src/Mono.Android/Android.Graphics/AndroidBitmapInfo.cs | Nullability alignment for Equals override. |
| src/Mono.Android/Android.Content.Res/IXmlResourceParser.cs | Adds new to disambiguate interface members and avoid hiding warnings. |
| src/Mono.Android/Android.Bluetooth/BluetoothDevice.cs | Adds ObsoletedOSPlatform("android37.0") to obsolete overloads. |
| src/Mono.Android/Android.App/SyncContext.cs | Nullability alignment for SynchronizationContext overrides. |
| src/Mono.Android/Android.App/AlertDialog.cs | Switches to EventHelper for event add/remove helper calls. |
| src/Mono.Android/Android.Animation/ValueAnimator.cs | Nullability tweaks in binding glue (Delegate?, GetObject(...)). |
| src/Mono.Android/Android.Animation/AnimatorSet.cs | Nullability tweaks in binding glue (Delegate?, GetObject(...)). |
Copilot's findings
- Files reviewed: 45/46 changed files
- Comments generated: 7
| protected override Java.Lang.Object? RawAdapter { | ||
| get { return JavaObjectExtensions.JavaCast<Java.Lang.Object>(JavaConvert.ToJavaObject (Adapter))!; } | ||
| set { Adapter = JavaConvert.FromJavaObject<T>(value)!; } | ||
| } |
There was a problem hiding this comment.
The getter no longer uses the null-forgiving operator now that RawAdapter is nullable.
The setter keeps it intentionally: Adapter is a non-nullable generic T, but null is a valid input here (Android's setAdapter(null) clears the adapter). Removing ! would require either throwing on a legitimate null or changing the public Adapter signature to T?, so the operator is retained to preserve the clear-adapter behavior. (3886367)
Guard CheckClientTrusted params (consistency with CheckServerTrusted); keep the clr_typemap_managed_to_java P/Invoke param non-nullable and guard the call site when Type.FullName is null; drop the redundant null-forgiving operator in AdapterView.RawAdapter getter; replace null-forgiving operators in ValueAnimator/AnimatorSet marshaling with explicit null checks that throw a clear exception.
|
Thanks for the review — addressed in
One intentional exception: the |
|
Superseded by #12003, which is rebased on the latest main and pushed to an origin branch (so it runs the full CI). Closing in favor of that PR. |
Summary
This PR reduces the C# build warnings in
src/Mono.Androidby fixing the warnings that originate in hand-written source and by completing the API-37 public API baseline so the public API analyzer passes.It intentionally does not remove every
NoWarnsuppression, nor theMono.Android.csprojentry in_AllowProjectWarnings. The warnings that remain all originate in generated code, and fixing them requires changes to the code generators. Those are called out as follow-ups below.What is fixed (hand-written code)
Each warning code was addressed in its own commit:
TypeConvertersubclasses inSystem.Drawing,SyncContext, the Java stream adapters,Color/AndroidBitmapInfoequality,AdapterView.RawAdapter).JavaObject.ToString()returns a non-nullstringto matchJava.Lang.Object.ToString().IComparer,ISpliterator,ISetCookie,IX509TrustManager/ISSLSession,IXmlPullParser).newkeyword to members that intentionally hide an inherited member (generic collection specializations, staticFromJniHandle/ToLocalJniHandle/CreateFromResourcehelpers,IXmlResourceParserinterface disambiguation,StringBuffer/StringBuilder.Append,AsyncTask.class_ref,JavaProxyThrowable.InnerException,SparseArray<E>,ArrayAdapter<T>,JavaList.Equals).Public API baseline (RS0016 / RS0017)
Some of the nullability fixes above change the annotations of public members, and the API-37 baseline was also missing a large number of generated binding members.
PublicAPI/API-37/PublicAPI.Unshipped.txthas been regenerated (via theRS0016code fix, plus*REMOVED*markers for stale signatures) so thatRS0016/RS0017are satisfied and remain enforced (they are not inNoWarn).Why
NoWarnand_AllowProjectWarningsare keptThe remaining warnings are all in generated code and cannot be fixed here without changing the generators:
mcw/*.cs(binding generator output):CS0108,CS0114,CS0618,CS0809,CS8613,CS8764,CS8765,CS8766,CS8767.JNIEnv.g.cs(jnienv-gentool,// Generated file; DO NOT EDIT!):RS0041(public members using oblivious reference types).Because these files are regenerated on every build, hand-editing them does not stick — the fix has to happen in the generator. Until those follow-ups land we must:
<NoWarn>inMono.Android.csproj, and<_AllowProjectWarnings Condition=" '$(MSBuildProjectFile)' == 'Mono.Android.csproj' ">true</_AllowProjectWarnings>inDirectory.Build.props.Removing either now would turn hundreds of generated-code warnings into build errors under
TreatWarningsAsErrors.Follow-ups
mcw/*.cs, then dropCS0108;CS0114;CS0618;CS0809;CS8613;CS8764;CS8765;CS8766;CS8767fromNoWarn.jnienv-gen– annotateJNIEnv.g.csoutput for nullability, then dropRS0041fromNoWarn.Once those land, the remaining
NoWarncodes and the_AllowProjectWarningsopt-in forMono.Android.csprojcan be removed.