Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] fix NRT warnings & other warnings (#9483)
Browse files Browse the repository at this point in the history
Introduces the setting to `Xamarin.Android.Build.Tasks.csproj`:

    <WarningsAsErrors>Nullable</WarningsAsErrors>

Then I fixed the existing warnings. Down the road this will make it
easier to adopt `#enable nullable`, and eventually `Nullable=enable`
the entire project. Trying to do that now results in 1,000+ warnings...

Most of these are types that could easily be null, and they weren't
marked as nullable with `?`. Luckily, the code appears to be checking
for null in these places already.

For `*Attribute` types that need `IJniNameProviderAttribute.Name`,
I explicitly implemented a non-nullable version of this property in
`*Attribute.Partial.cs` files.
  • Loading branch information
jonathanpeppers authored Nov 19, 2024
1 parent 7cb783c commit a1fab8b
Show file tree
Hide file tree
Showing 29 changed files with 73 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ namespace Android.App {

partial class ActivityAttribute {

string _ParentActivity;
TypeDefinition type;
ICollection<string> specified;
string? _ParentActivity;
TypeDefinition? type;
ICollection<string>? specified;

static partial void AddManualMapping ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace Android.App {

partial class ApplicationAttribute {

string _BackupAgent;
ICustomAttributeProvider provider;
string? _BackupAgent;
ICustomAttributeProvider? provider;

ICollection<string> specified;
ICollection<string>? specified;

static partial void AddManualMapping ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.Content {

partial class BroadcastReceiverAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static BroadcastReceiverAttribute FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static string[] ToStringArray (object value)
return values.Select (v => (string) v.Value).ToArray ();
}

ICollection<string> specified;
ICollection<string>? specified;

public static ContentProviderAttribute FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.Content {

partial class GrantUriPermissionAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<GrantUriPermissionAttribute> FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.App {

partial class InstrumentationAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<InstrumentationAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace Android.App

partial class LayoutAttribute
{
TypeDefinition type;
ICollection<string> specified;
TypeDefinition? type;
ICollection<string>? specified;

public static LayoutAttribute FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.App {

partial class MetaDataAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<MetaDataAttribute> FromCustomAttributeProvider (ICustomAttributeProvider type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Android.App {

partial class PermissionAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<PermissionAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Android.App {

partial class PermissionGroupAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<PermissionGroupAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Android.App {

partial class PermissionTreeAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<PermissionTreeAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.App {

partial class PropertyAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<PropertyAttribute> FromCustomAttributeProvider (ICustomAttributeProvider type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Android.App {

partial class ServiceAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static ServiceAttribute FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)
return mapping.ToElement (this, specified, packageName, cache);
}

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<SupportsGLTextureAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)
return mapping.ToElement (this, specified, packageName, cache);
}

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<UsesConfigurationAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal XElement ToElement (string packageName, TypeDefinitionCache cache)
return mapping.ToElement (this, specified, packageName, cache);
}

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<UsesFeatureAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.App {

partial class UsesLibraryAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<UsesLibraryAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Android.App {

partial class UsesPermissionAttribute {

ICollection<string> specified;
ICollection<string>? specified;

public static IEnumerable<UsesPermissionAttribute> FromCustomAttributeProvider (ICustomAttributeProvider provider, TypeDefinitionCache cache)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/GetAotArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int GetNdkApiLevel (NdkTools ndk, AndroidTargetArch arch)
// level already set
} else {
// Probably not ideal!
level = MonoAndroidHelper.SupportedVersions.MaxStableVersion.ApiLevel;
level = MonoAndroidHelper.SupportedVersions.MaxStableVersion?.ApiLevel ?? 21;
}

// Some Android API levels do not exist on the NDK level. Workaround this my mapping them to the
Expand Down Expand Up @@ -353,7 +353,7 @@ string GetLdFlags (NdkTools ndk, AndroidTargetArch arch, int level, string toolP
return ldFlags.ToString ();
}

static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string archDir = null)
static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string? archDir = null)
{
var baseDir = Path.GetFullPath (Path.Combine (binDir, ".."));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ public ManifestDocumentElement (string element)

class MappingInfo {
public string AttributeName;
public Func<T, object> Getter;
public Action<T, object> Setter;
public Func<T, object?> Getter;
public Action<T, object>? Setter;
public Type MemberType;
public Func<T, string> AttributeValue;
public Func<T, ICustomAttributeProvider, IAssemblyResolver, TypeDefinitionCache, string> AttributeValue2;
}

readonly IDictionary<string, MappingInfo> Mappings = new Dictionary<string, MappingInfo> ();

public void Add (string member, string attributeName, Func<T, object> getter, Action<T, object> setter, Type memberType = null)
public void Add (string member, string attributeName, Func<T, object?> getter, Action<T, object>? setter, Type memberType = null)
{
Mappings.Add (member, new MappingInfo {
AttributeName = attributeName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<Nullable>annotations</Nullable>
<!-- Causes issues with linker files imported from Mono -->
<NoWarn>$(NoWarn);CA1310;CA1305</NoWarn>
<WarningsAsErrors>Nullable</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using Android.Views;
using Java.Interop;

namespace Android.App;

public sealed partial class ActivityAttribute
{
string IJniNameProviderAttribute.Name => Name ?? "";

[Obsolete ("There is no //activity/@android:layoutDirection attribute. This was a mistake. " +
"Perhaps you wanted ConfigurationChanges=ConfigChanges.LayoutDirection?")]
public LayoutDirection LayoutDirection { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ static ActivityAttribute ()
mapping.Add (
member: "ShowOnLockScreen",
attributeName: "showOnLockScreen",
#pragma warning disable CS0618 // Type or member is obsolete
getter: self => self.ShowOnLockScreen,
setter: (self, value) => self.ShowOnLockScreen = (bool) value
#pragma warning restore CS0618 // Type or member is obsolete
);
mapping.Add (
member: "ShowWhenLocked",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Java.Interop;

namespace Android.App;

public sealed partial class ApplicationAttribute
{
string IJniNameProviderAttribute.Name => Name ?? "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Java.Interop;

namespace Android.App;

public sealed partial class InstrumentationAttribute
{
string IJniNameProviderAttribute.Name => Name ?? "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Java.Interop;

namespace Android.App;

public sealed partial class ServiceAttribute
{
string IJniNameProviderAttribute.Name => Name ?? "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Java.Interop;

namespace Android.Content;

public partial class BroadcastReceiverAttribute
{
string IJniNameProviderAttribute.Name => Name ?? "";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Java.Interop;

namespace Android.Content;

Expand All @@ -12,4 +13,6 @@ public ContentProviderAttribute (string [] authorities)
throw new ArgumentException ("At least one authority must be specified.", "authorities");
Authorities = authorities;
}

string IJniNameProviderAttribute.Name => Name ?? "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\BroadcastReceiverAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\BroadcastReceiverAttribute.Partial.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\ContentProviderAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Content\ContentProviderAttribute.Partial.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\RegisterAttribute.cs" />
Expand All @@ -20,8 +21,11 @@
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ActivityAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ActivityAttribute.Partial.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ApplicationAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ApplicationAttribute.Partial.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ServiceAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\ServiceAttribute.Partial.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\InstrumentationAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\InstrumentationAttribute.Partial.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.App\LayoutAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\IJniNameProviderAttribute.cs" />
</ItemGroup>
Expand Down

0 comments on commit a1fab8b

Please sign in to comment.