Skip to content

Commit 2e4edb0

Browse files
[xabt] fix NRT annotations in MSBuild Tasks (#10283)
Context: #10277 This PR systematically modernizes MSBuild task classes in `src/Xamarin.Android.Build.Tasks/Tasks/` to follow the repository's nullable reference types guidelines as outlined in `copilot-instructions.md`. ## Changes Made ### Phase 1: Files with `#nullable disable` (10/22 completed - 45%) Converted files from `#nullable disable` to `#nullable enable` and modernized properties: **Completed files:** - AndroidApkSigner.cs, CalculateProjectDependencies.cs, JavaToolTask.cs (base class) - ConvertCustomView.cs, MergeRemapXml.cs, ImportJavaDoc.cs - JavaDoc.cs, JavaSourceUtils.cs, StripNativeLibraries.cs, LinkApplicationSharedLibraries.cs ### Phase 2: Files without nullable directives (27/117 completed - 23%) Added `#nullable enable` directive and converted patterns: **Completed files:** - Aapt2Compile.cs, AndroidMessage.cs, AndroidAdb.cs, AndroidSignPackage.cs - Aapt2LinkAssetPack.cs, AdjustJavacVersionArguments.cs, AndroidCreateDebugKey.cs - AndroidError.cs, AndroidWarning.cs, AndroidComputeResPaths.cs, AndroidDotnetToolTask.cs - AndroidZipAlign.cs, AppendCustomMetadataToItemGroup.cs, AssemblyModifierPipeline.cs - CheckClientHandlerType.cs, CheckDuplicateJavaLibraries.cs, CheckForInvalidDesignerConfig.cs - CheckProjectItems.cs, ClassParse.cs, CollectAssemblyFilesForArchive.cs, ComputeHash.cs - CopyIfChanged.cs, CopyResource.cs, CreateAssemblyStore.cs, CollectDalvikFilesForArchive.cs - D8.cs, CreateTemporaryDirectory.cs ## Progress Summary - **Total files modernized:** 37 out of 139 target files (27% complete) - **Phase 1 progress:** 10/22 files with `#nullable disable` (45% complete) - **Phase 2 progress:** 27/117 files without nullable directives (23% complete) - **Patterns established:** Clear, systematic approach for remaining files ## Impact - Improves type safety across MSBuild task properties - Follows established repository conventions for nullable reference types - Maintains backward compatibility while modernizing the codebase - Establishes foundation for completing remaining files #10277 is not complete, but we will try another pass in a future PR. Co-authored-by: Jonathan Peppers <[email protected]>
1 parent 0ed10b4 commit 2e4edb0

37 files changed

+224
-154
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Copyright (C) 2011 Xamarin, Inc. All rights reserved.
2+
#nullable enable
3+
24
using System;
35
using System.Diagnostics;
46
using System.IO;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Diagnostics;
35
using System.IO;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using Microsoft.Build.Utilities;
35
using Microsoft.Build.Framework;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using Microsoft.Android.Build.Tasks;
24
using Microsoft.Build.Framework;
35
using Microsoft.Build.Utilities;
@@ -34,7 +36,7 @@ protected override string GenerateFullPathToTool ()
3436
protected override string GenerateCommandLineCommands ()
3537
{
3638
var sb = new StringBuilder ();
37-
if (!string.IsNullOrEmpty (AdbTarget))
39+
if (!AdbTarget.IsNullOrEmpty ())
3840
sb.Append ($" {AdbTarget} ");
3941
sb.AppendFormat ("{0} {1}", Command, Arguments);
4042
return sb.ToString ();

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#nullable disable
1+
#nullable enable
22

33
using System;
44
using System.IO;
@@ -14,21 +14,21 @@ public class AndroidApkSigner : JavaToolTask
1414
public override string TaskPrefix => "AAS";
1515

1616
[Required]
17-
public string ApkSignerJar { get; set; }
17+
public string ApkSignerJar { get; set; } = "";
1818

1919
[Required]
20-
public string ApkToSign { get; set; }
20+
public string ApkToSign { get; set; } = "";
2121

2222
[Required]
23-
public ITaskItem ManifestFile { get; set; }
23+
public ITaskItem ManifestFile { get; set; } = null!;
2424

25-
public string KeyStore { get; set; }
25+
public string? KeyStore { get; set; }
2626

27-
public string KeyAlias { get; set; }
27+
public string? KeyAlias { get; set; }
2828

29-
public string PlatformKey { get; set; }
29+
public string? PlatformKey { get; set; }
3030

31-
public string PlatformCert { get; set; }
31+
public string? PlatformCert { get; set; }
3232

3333
/// <summary>
3434
/// The Password for the Key.
@@ -39,7 +39,7 @@ public class AndroidApkSigner : JavaToolTask
3939
/// env:<PasswordEnvironentVariable>
4040
/// file:<PasswordFile>
4141
/// </summary>
42-
public string KeyPass { get; set; } = string.Empty;
42+
public string KeyPass { get; set; } = "";
4343

4444
/// <summary>
4545
/// The Password for the Keystore.
@@ -50,15 +50,15 @@ public class AndroidApkSigner : JavaToolTask
5050
/// env:<PasswordEnvironentVariable>
5151
/// file:<PasswordFile>
5252
/// </summary>
53-
public string StorePass { get; set; } = string.Empty;
53+
public string StorePass { get; set; } = "";
5454

55-
public string AdditionalArguments { get; set; }
55+
public string? AdditionalArguments { get; set; }
5656

5757
void AddStorePass (CommandLineBuilder cmd, string cmdLineSwitch, string value)
5858
{
59-
string pass = value.Replace ("env:", string.Empty)
60-
.Replace ("file:", string.Empty)
61-
.Replace ("pass:", string.Empty);
59+
string pass = value.Replace ("env:", "")
60+
.Replace ("file:", "")
61+
.Replace ("pass:", "");
6262
if (value.StartsWith ("env:", StringComparison.Ordinal)) {
6363
cmd.AppendSwitchIfNotNull ($"{cmdLineSwitch} env:", pass);
6464
}
@@ -74,20 +74,21 @@ protected override string GenerateCommandLineCommands ()
7474
var cmd = new CommandLineBuilder ();
7575

7676
var manifest = AndroidAppManifest.Load (ManifestFile.ItemSpec, MonoAndroidHelper.SupportedVersions);
77-
int minSdk = MonoAndroidHelper.SupportedVersions.MinStableVersion.ApiLevel;
78-
int maxSdk = MonoAndroidHelper.SupportedVersions.MaxStableVersion.ApiLevel;
77+
int? minSdk = MonoAndroidHelper.SupportedVersions.MinStableVersion?.ApiLevel;
78+
int? maxSdk = MonoAndroidHelper.SupportedVersions.MaxStableVersion?.ApiLevel;
7979
if (manifest.MinSdkVersion.HasValue)
8080
minSdk = manifest.MinSdkVersion.Value;
8181

8282
if (manifest.TargetSdkVersion.HasValue)
8383
maxSdk = manifest.TargetSdkVersion.Value;
8484

85-
minSdk = Math.Min (minSdk, maxSdk);
85+
if (minSdk.HasValue && maxSdk.HasValue)
86+
minSdk = Math.Min (minSdk.Value, maxSdk.Value);
8687

8788
cmd.AppendSwitchIfNotNull ("-jar ", ApkSignerJar);
8889
cmd.AppendSwitch ("sign");
8990

90-
if (!string.IsNullOrEmpty (PlatformKey) && !string.IsNullOrEmpty (PlatformCert)) {
91+
if (!PlatformKey.IsNullOrEmpty () && !PlatformCert.IsNullOrEmpty ()) {
9192
cmd.AppendSwitchIfNotNull ("--key ", PlatformKey);
9293
cmd.AppendSwitchIfNotNull ("--cert ", PlatformCert);
9394
} else {
@@ -97,11 +98,15 @@ protected override string GenerateCommandLineCommands ()
9798
AddStorePass (cmd, "--key-pass", KeyPass);
9899
}
99100

100-
cmd.AppendSwitchIfNotNull ("--min-sdk-version ", minSdk.ToString ());
101-
cmd.AppendSwitchIfNotNull ("--max-sdk-version ", maxSdk.ToString ());
101+
if (minSdk is not null) {
102+
cmd.AppendSwitchIfNotNull ("--min-sdk-version ", minSdk.ToString ());
103+
}
104+
if (maxSdk is not null) {
105+
cmd.AppendSwitchIfNotNull ("--max-sdk-version ", maxSdk.ToString ());
106+
}
102107

103108

104-
if (!string.IsNullOrEmpty (AdditionalArguments))
109+
if (!AdditionalArguments.IsNullOrEmpty ())
105110
cmd.AppendSwitch (AdditionalArguments);
106111

107112
cmd.AppendSwitchIfNotNull (" ", ApkToSign);
@@ -129,7 +134,7 @@ protected override string ToolName {
129134

130135
protected override bool ValidateParameters ()
131136
{
132-
if (!string.IsNullOrEmpty (PlatformKey) && !string.IsNullOrEmpty (PlatformCert)) {
137+
if (!PlatformKey.IsNullOrEmpty () && !PlatformCert.IsNullOrEmpty ()) {
133138
if (!File.Exists (PlatformKey)) {
134139
Log.LogCodedError ("XA4310", Properties.Resources.XA4310, "$(AndroidSigningPlatformKey)", PlatformKey);
135140
return false;
@@ -139,15 +144,15 @@ protected override bool ValidateParameters ()
139144
return false;
140145
}
141146
} else {
142-
if (!string.IsNullOrEmpty (KeyStore) && !File.Exists (KeyStore)) {
147+
if (!KeyStore.IsNullOrEmpty () && !File.Exists (KeyStore)) {
143148
Log.LogCodedError ("XA4310", Properties.Resources.XA4310, "$(AndroidSigningKeyStore)", KeyStore);
144149
return false;
145150
}
146-
if (string.IsNullOrEmpty (KeyPass)) {
151+
if (KeyPass.IsNullOrEmpty ()) {
147152
Log.LogCodedError ("XA4314", Properties.Resources.XA4314, "$(AndroidSigningKeyPass)");
148153
return false;
149154
}
150-
if (string.IsNullOrEmpty (StorePass)) {
155+
if (StorePass.IsNullOrEmpty ()) {
151156
Log.LogCodedError ("XA4314", Properties.Resources.XA4314, "$(AndroidSigningStorePass)");
152157
return false;
153158
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
2626

27+
#nullable enable
28+
2729
using System;
2830
using System.Text;
2931
using Microsoft.Build.Utilities;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
#nullable enable
2+
3+
using System;
24
using System.IO;
35
using System.Resources;
46
using Microsoft.Build.Framework;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.IO;
35
using System.Runtime.InteropServices;
@@ -106,13 +108,13 @@ string FindDotnet ()
106108
string FindMono ()
107109
{
108110
string mono = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<string> (MonoKey, Lifetime);
109-
if (!string.IsNullOrEmpty (mono)) {
111+
if (!mono.IsNullOrEmpty ()) {
110112
Log.LogDebugMessage ($"Found cached mono via {nameof (BuildEngine4.RegisterTaskObject)}");
111113
return mono;
112114
}
113115

114116
var env = Environment.GetEnvironmentVariable ("PATH");
115-
if (string.IsNullOrEmpty (env)) {
117+
if (!env.IsNullOrEmpty ()) {
116118
foreach (var path in env.Split (Path.PathSeparator)) {
117119
if (File.Exists (mono = Path.Combine (path, "mono"))) {
118120
Log.LogDebugMessage ("Found mono in $PATH");
@@ -138,7 +140,7 @@ string FindMono ()
138140
protected virtual CommandLineBuilder GetCommandLineBuilder ()
139141
{
140142
var cmd = new CommandLineBuilder ();
141-
if (!string.IsNullOrEmpty (AssemblyPath)) {
143+
if (!AssemblyPath.IsNullOrEmpty ()) {
142144
cmd.AppendFileNameIfNotNull (AssemblyPath);
143145
}
144146
return cmd;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using Microsoft.Build.Framework;
24
using Microsoft.Build.Utilities;
35
using Microsoft.Android.Build.Tasks;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using Microsoft.Build.Framework;
24
using Microsoft.Build.Utilities;
35
using Microsoft.Android.Build.Tasks;

0 commit comments

Comments
 (0)