Skip to content

Commit 510fc08

Browse files
[xabt] Fix NRT annotations in MSBuild Tasks, part 5 (#10313)
Context: #10277 This PR addresses the issue of updating MSBuild task classes in `src/Xamarin.Android.Build.Tasks/Tasks/` to follow the repository's nullable reference types guidelines. ## Changes Made ### ✅ Added `#nullable enable` directives - Added `#nullable enable` to 2 files that were missing it: - `GetNativeRuntimeComponents.cs` - `LinkNativeRuntime.cs` ### ✅ Converted to extension method patterns - Updated 17 occurrences across 15 files to use extension methods per copilot-instructions.md: - `string.IsNullOrEmpty()` → `.IsNullOrEmpty()` - `string.IsNullOrWhiteSpace()` → `.IsNullOrWhiteSpace()` **Files updated:** - `Aapt2Link.cs` (2 occurrences) - `BuildAppBundle.cs` - `BundleToolAdbTask.cs` - `CalculateLayoutCodeBehind.cs` - `CollectDalvikFilesForArchive.cs` - `CollectNativeFilesForArchive.cs` (2 occurrences) - `CollectNonEmptyDirectories.cs` (2 occurrences) - `CreateAar.cs` (2 occurrences) - `GetAotArguments.cs` - `GetAotAssemblies.cs` - `GetAssetPacks.cs` - `GetImportedLibraries.cs` - `ProcessAssemblies.cs` - `R8.cs` (5 occurrences) ## Results - **Before**: 143/147 files had `#nullable enable` (97.3% compliance) - **After**: 146/147 files have `#nullable enable` (99.3% compliance) Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
1 parent db38d15 commit 510fc08

16 files changed

+24
-22
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/Aapt2Link.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ string [] GenerateCommandLineCommands (string ManifestFile, string? currentAbi,
266266
if (ProtobufFormat)
267267
cmd.Add ("--proto-format");
268268

269-
if (!string.IsNullOrWhiteSpace (ExtraArgs)) {
269+
if (!ExtraArgs.IsNullOrWhiteSpace ()) {
270270
foreach (Match match in exraArgSplitRegEx.Matches (ExtraArgs)) {
271271
string value = match.Value.Trim (' ', '"', '\'');
272272
if (!value.IsNullOrEmpty ())
@@ -294,7 +294,7 @@ string [] GenerateCommandLineCommands (string ManifestFile, string? currentAbi,
294294
if (AdditionalAndroidAssetPaths != null) {
295295
for (int i = 0; i < AdditionalAndroidAssetPaths.Length; i++) {
296296
var assetDir = GetFullPath (AdditionalAndroidAssetPaths [i].ItemSpec.TrimEnd ('\\'));
297-
if (!string.IsNullOrWhiteSpace (assetDir)) {
297+
if (!assetDir.IsNullOrWhiteSpace ()) {
298298
if (Directory.Exists (assetDir)) {
299299
if (OS.IsWindows && !IsPathOnlyASCII (assetDir)) {
300300
hasAssetsErrors = true;

src/Xamarin.Android.Build.Tasks/Tasks/BuildAppBundle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public override bool RunTask ()
9191
}
9292

9393
JsonNode? json = JsonNode.Parse ("{}");
94-
if (!string.IsNullOrEmpty (CustomBuildConfigFile) && File.Exists (CustomBuildConfigFile)) {
94+
if (!CustomBuildConfigFile.IsNullOrEmpty () && File.Exists (CustomBuildConfigFile)) {
9595
using Stream fs = File.OpenRead (CustomBuildConfigFile);
9696
using JsonDocument doc = JsonDocument.Parse (fs, new JsonDocumentOptions { AllowTrailingCommas = true });
9797
json = doc.RootElement.ToNode ();

src/Xamarin.Android.Build.Tasks/Tasks/BundleToolAdbTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class BundleToolAdbTask : BundleTool
2323

2424
protected void AppendAdbOptions (CommandLineBuilder cmd)
2525
{
26-
var adb = string.IsNullOrEmpty (AdbToolExe) ? AdbToolName : AdbToolExe;
26+
var adb = AdbToolExe.IsNullOrEmpty () ? AdbToolName : AdbToolExe;
2727
cmd.AppendSwitchIfNotNull ("--adb ", Path.Combine (AdbToolPath, adb));
2828

2929
var adbTarget = AdbTarget;

src/Xamarin.Android.Build.Tasks/Tasks/CalculateLayoutCodeBehind.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ bool LoadLayout (string filePath, Dictionary <string, LayoutGroup> groupIndex, r
196196

197197
string? xamarinClasses = nav.GetAttribute (XamarinClassesAttribute, xamarinNS)?.Trim ();
198198

199-
if (!string.IsNullOrWhiteSpace (rootWidgetIdOverride)) {
199+
if (!rootWidgetIdOverride.IsNullOrWhiteSpace ()) {
200200
if (!ParseIdWithError (nav, filePath, rootWidgetIdOverride, true, out parsedId, out name))
201201
LogCodedError ("XA1012", Properties.Resources.XA1012, rootWidgetIdOverride);
202202
else {

src/Xamarin.Android.Build.Tasks/Tasks/CollectDalvikFilesForArchive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override bool RunTask ()
2929

3030
foreach (var dex in DalvikClasses) {
3131
var apkName = dex.GetMetadata ("ApkName");
32-
var dexPath = string.IsNullOrWhiteSpace (apkName) ? Path.GetFileName (dex.ItemSpec) : apkName;
32+
var dexPath = apkName.IsNullOrWhiteSpace () ? Path.GetFileName (dex.ItemSpec) : apkName;
3333

3434
files.AddItem (dex.ItemSpec, dalvikPath + dexPath);
3535
}

src/Xamarin.Android.Build.Tasks/Tasks/CollectNativeFilesForArchive.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void AddNativeLibraries (ArchiveFileList files, string [] supportedAbis)
187187

188188
AddNativeLibraries (files, supportedAbis, libs);
189189

190-
if (string.IsNullOrWhiteSpace (CheckedBuild))
190+
if (CheckedBuild.IsNullOrWhiteSpace ())
191191
return;
192192

193193
string mode = CheckedBuild;
@@ -245,7 +245,7 @@ void AddNativeLibraries (ArchiveFileList files, string [] supportedAbis)
245245
// If Abi is explicitly specified, simply return it.
246246
var lib_abi = AndroidRidAbiHelper.GetNativeLibraryAbi (lib);
247247

248-
if (string.IsNullOrWhiteSpace (lib_abi)) {
248+
if (lib_abi.IsNullOrWhiteSpace ()) {
249249
Log.LogCodedError ("XA4301", lib.ItemSpec, 0, Properties.Resources.XA4301_ABI, lib.ItemSpec);
250250
return null;
251251
}

src/Xamarin.Android.Build.Tasks/Tasks/CollectNonEmptyDirectories.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public override bool RunTask ()
9191
});
9292
directory.CopyMetadataTo (taskItem);
9393

94-
if (string.IsNullOrEmpty (directory.GetMetadata ("StampFile"))) {
94+
if (directory.GetMetadata ("StampFile").IsNullOrEmpty ()) {
9595
taskItem.SetMetadata ("StampFile", stampFile);
9696
} else {
9797
Log.LogDebugMessage ($"%(StampFile) already set: {stampFile}");
9898
}
99-
if (string.IsNullOrEmpty (directory.GetMetadata ("FilesCache"))) {
99+
if (directory.GetMetadata ("FilesCache").IsNullOrEmpty ()) {
100100
taskItem.SetMetadata ("FilesCache", filesCache);
101101
} else {
102102
Log.LogDebugMessage ($"%(FilesCache) already set: {filesCache}");

src/Xamarin.Android.Build.Tasks/Tasks/CreateAar.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public override bool RunTask ()
108108
if (NativeLibraries != null) {
109109
foreach (var lib in NativeLibraries) {
110110
var abi = AndroidRidAbiHelper.GetNativeLibraryAbi (lib);
111-
if (string.IsNullOrWhiteSpace (abi)) {
111+
if (abi.IsNullOrWhiteSpace ()) {
112112
Log.LogCodedError ("XA4301", lib.ItemSpec, 0, Properties.Resources.XA4301_ABI, lib.ItemSpec);
113113
continue;
114114
}
@@ -127,7 +127,7 @@ public override bool RunTask ()
127127
if (AndroidManifest != null && File.Exists (AndroidManifest.ItemSpec)) {
128128
var manifest = File.ReadAllText (AndroidManifest.ItemSpec);
129129
var doc = XDocument.Parse(manifest);
130-
if (!string.IsNullOrEmpty (doc.Element ("manifest")?.Attribute ("package")?.Value)) {
130+
if (!(doc.Element ("manifest")?.Attribute ("package")?.Value).IsNullOrEmpty ()) {
131131
aar.AddEntry ("AndroidManifest.xml", manifest, Files.UTF8withoutBOM);
132132
} else {
133133
Log.LogDebugMessage ($"Skipping {AndroidManifest.ItemSpec}. The `manifest` does not have a `package` attribute.");

src/Xamarin.Android.Build.Tasks/Tasks/GetAotArguments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public abstract class GetAotArguments : AsyncTask
8080
protected AotMode AotMode;
8181
protected SequencePointsMode SequencePointsMode;
8282
protected string SdkBinDirectory = "";
83-
protected bool UseAndroidNdk => !string.IsNullOrWhiteSpace (AndroidNdkDirectory);
83+
protected bool UseAndroidNdk => !AndroidNdkDirectory.IsNullOrWhiteSpace ();
8484

8585
public static bool GetAndroidAotMode(string androidAotMode, out AotMode aotMode)
8686
{

src/Xamarin.Android.Build.Tasks/Tasks/GetAotAssemblies.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override Task RunTaskAsync ()
3838
SdkBinDirectory = MonoAndroidHelper.GetOSBinPath ();
3939

4040
var abi = AndroidRidAbiHelper.RuntimeIdentifierToAbi (RuntimeIdentifier);
41-
if (string.IsNullOrEmpty (abi)) {
41+
if (abi.IsNullOrEmpty ()) {
4242
Log.LogCodedError ("XA0035", Properties.Resources.XA0035, RuntimeIdentifier);
4343
return Task.CompletedTask;
4444
}

0 commit comments

Comments
 (0)