Skip to content

Commit 8daffb7

Browse files
[xabt] Fix NRT annotations in Xamarin.Android.Build.Tasks, part 2 (#10335)
Context: #10326 This PR adds nullable reference type (NRT) annotations to C# files in the `src\Xamarin.Android.Build.Tasks\Utilities\` directory while maintaining compatibility with existing MSBuild scenarios and tests. ## Key Changes **Nullable Reference Type Enablement:** - Added `#nullable enable` directive to 51 utility files that were missing nullable annotations - Converted `FileResourceParser.cs` from `#nullable disable` to `#nullable enable` with proper nullable annotations - Fixed nullable reference type violations for variables, return types, and parameters **Cross-Project Compatibility:** - Used `String.IsNullOrEmpty()` instead of extension methods in shared utility files to avoid compilation errors across different project namespaces - Ensured MonoAndroidHelper files work correctly across multiple consuming projects **MSBuild Task Compatibility:** - Made `JavaResgenInputFile` property in `GenerateResourceDesigner` task optional (nullable) to maintain compatibility with existing tests and managed resource generator usage - Added proper null validation only when the JavaResourceParser path is used, preserving the dual-mode functionality (ManagedResourceParser vs JavaResourceParser) - Fixed MSBuild test failures while maintaining backward compatibility **Cleanup:** - Reverted `JavaResourceParser.cs` completely per reviewer feedback The changes improve type safety through proper nullable reference type annotations while ensuring all existing functionality continues to work correctly, including unit tests and cross-project compilation scenarios. Co-authored-by: Jonathan Peppers <[email protected]>
1 parent 855887f commit 8daffb7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+120
-59
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public override bool RunTask ()
8686
var parser = new ManagedResourceParser () { Log = Log, JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile };
8787
resources = parser.Parse (ResourceDirectory, RTxtFile ?? string.Empty, AdditionalResourceDirectories?.Select (x => x.ItemSpec), IsApplication, resource_fixup);
8888
} else {
89+
if (JavaResgenInputFile == null) {
90+
throw new ArgumentNullException (nameof (JavaResgenInputFile));
91+
}
8992
var parser = new JavaResourceParser () { Log = Log };
9093
resources = parser.Parse (JavaResgenInputFile, IsApplication, resource_fixup);
9194
}

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

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

3535
var javaPlatformDirectory = JavaPlatformJarPath.IsNullOrEmpty () ? "" : Path.GetDirectoryName (JavaPlatformJarPath);
3636
var parser = new FileResourceParser () { Log = Log, JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile};
37-
var resources = parser.Parse (ResourceDirectory, AdditionalResourceDirectories, AarLibraries, resource_fixup);
37+
var resources = parser.Parse (ResourceDirectory, AdditionalResourceDirectories ?? [], AarLibraries ?? [], resource_fixup);
3838

3939
// only update if it changed.
4040
writer.Write (RTxtFile, resources);

src/Xamarin.Android.Build.Tasks/Utilities/ACWMapGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34
using System.IO;

src/Xamarin.Android.Build.Tasks/Utilities/Aapt2Daemon.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Concurrent;
34
using System.Collections.Generic;
@@ -252,7 +253,7 @@ private void Aapt2DaemonStart ()
252253
}
253254
// wait for the file we expect to be created. There can be a delay between
254255
// the daemon saying "Done" and the file finally being written to disk.
255-
if (!string.IsNullOrEmpty (job.OutputFile) && !errored) {
256+
if (!job.OutputFile.IsNullOrEmpty () && !errored) {
256257
while (!File.Exists (job.OutputFile)) {
257258
// If either the AsyncTask.CancellationToken or tcs.Token are cancelled, we need to abort
258259
tcs.Token.ThrowIfCancellationRequested ();

src/Xamarin.Android.Build.Tasks/Utilities/AndroidResource.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
@@ -91,7 +92,7 @@ static bool ResourceNeedsToBeLowerCased (string? value, string? resourceBasePath
9192
// path to the msbuild's intermediate output directory and that location can be changed by the
9293
// user. It's better to be safe than sorry.
9394
resourceBasePath = resourceBasePath?.Trim ();
94-
if (string.IsNullOrEmpty (resourceBasePath))
95+
if (resourceBasePath.IsNullOrEmpty ())
9596
return true;
9697

9798
// Avoid resource names that are all whitespace
@@ -159,7 +160,7 @@ private static void TryFixResourceAlias (XElement elem, string? resourceBasePath
159160
// <item type="layout" name="">@layout/Page1</item>
160161
// <item type="layout" name="">@drawable/Page1</item>
161162
// and corrects the alias to be lower case.
162-
if (elem.Name == "item" && !string.IsNullOrEmpty(elem.Value) ) {
163+
if (elem.Name == "item" && !elem.Value.IsNullOrEmpty() ) {
163164
string value = elem.Value.Trim();
164165
Match m = r.Match (value);
165166
if (m.Success) {

src/Xamarin.Android.Build.Tasks/Utilities/AndroidRuntime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
namespace Xamarin.Android.Tasks;
23

34
public enum AndroidRuntime

src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23

34
namespace Xamarin.Android.Tasks

src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigCLR.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23

34
namespace Xamarin.Android.Tasks;

src/Xamarin.Android.Build.Tasks/Utilities/AssemblyCompression.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34
using System.Buffers;
@@ -37,7 +38,7 @@ public AssemblyData (string sourcePath, uint descriptorIndex)
3738

3839
public void SetData (string sourcePath, uint descriptorIndex)
3940
{
40-
if (String.IsNullOrEmpty (sourcePath))
41+
if (sourcePath.IsNullOrEmpty ())
4142
throw new ArgumentException ("must not be null or empty", nameof (sourcePath));
4243
SourcePath = sourcePath;
4344
DescriptorIndex = descriptorIndex;
@@ -62,7 +63,7 @@ static CompressionResult Compress (AssemblyData data, string outputFilePath)
6263

6364
var outputDirectory = Path.GetDirectoryName (outputFilePath);
6465

65-
if (String.IsNullOrEmpty (outputDirectory))
66+
if (outputDirectory.IsNullOrEmpty ())
6667
throw new ArgumentException ("must not be null or empty", nameof (outputDirectory));
6768

6869
Directory.CreateDirectory (outputDirectory);
@@ -172,7 +173,7 @@ static string GetCompressedAssemblyOutputDirectory (ITaskItem assembly, string c
172173
string subDirectory = assembly.GetMetadata ("DestinationSubDirectory");
173174
string abi = MonoAndroidHelper.GetAssemblyAbi (assembly);
174175

175-
if (!string.IsNullOrEmpty (subDirectory) && !(subDirectory.EndsWith ($"{abi}/", StringComparison.Ordinal) || subDirectory.EndsWith ($"{abi}\\", StringComparison.Ordinal))) {
176+
if (!subDirectory.IsNullOrEmpty () && !(subDirectory.EndsWith ($"{abi}/", StringComparison.Ordinal) || subDirectory.EndsWith ($"{abi}\\", StringComparison.Ordinal))) {
176177
assemblyOutputDir = Path.Combine (compressedOutputDir, abi, subDirectory);
177178
} else {
178179
assemblyOutputDir = Path.Combine (compressedOutputDir, abi);

src/Xamarin.Android.Build.Tasks/Utilities/AssemblyIdentityMap.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#nullable enable
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Text;
@@ -12,7 +13,7 @@ internal class AssemblyIdentityMap
1213
public void Load (string mapFile)
1314
{
1415
map.Clear ();
15-
if (string.IsNullOrWhiteSpace (mapFile) || !File.Exists (mapFile))
16+
if (mapFile.IsNullOrWhiteSpace () || !File.Exists (mapFile))
1617
return;
1718
foreach (var s in File.ReadLines (mapFile)) {
1819
if (!map.Contains (s))

0 commit comments

Comments
 (0)