Skip to content
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/Aapt2Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override int GetRequiredDaemonInstances ()

public async override System.Threading.Tasks.Task RunTaskAsync ()
{
await this.WhenAllWithLock (ResourcesToCompile ?? ResourceDirectories, ProcessDirectory);
await this.WhenAllWithLock (ResourcesToCompile ?? ResourceDirectories ?? [], ProcessDirectory);

ProcessOutput ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ string FindDotnet ()

string FindMono ()
{
string mono = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<string> (MonoKey, Lifetime);
string? mono = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<string> (MonoKey, Lifetime);
if (!mono.IsNullOrEmpty ()) {
Log.LogDebugMessage ($"Found cached mono via {nameof (BuildEngine4.RegisterTaskObject)}");
return mono;
Expand Down
5 changes: 3 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/AndroidError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public override bool Execute ()
{
Log.LogCodedError (
Code,
Properties.Resources.ResourceManager.GetString (ResourceName, Properties.Resources.Culture),
FormatArguments
Properties.Resources.ResourceManager.GetString (ResourceName, Properties.Resources.Culture)
?? $"(Missing resource: {ResourceName})",
FormatArguments ?? []
);
Comment thread
jonathanpeppers marked this conversation as resolved.
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/AndroidWarning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public override bool Execute ()
{
Log.LogCodedWarning (
Code,
Properties.Resources.ResourceManager.GetString (ResourceName, Properties.Resources.Culture),
FormatArguments
Properties.Resources.ResourceManager.GetString (ResourceName, Properties.Resources.Culture)
?? $"(Missing resource: {ResourceName})",
FormatArguments ?? []
);
Comment thread
jonathanpeppers marked this conversation as resolved.
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/BuildAppBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override bool RunTask ()
var mergedJson = json.Merge (jsonAdditionDoc);
var output = mergedJson?.ToJsonString (new JsonSerializerOptions { WriteIndented = true });

Log.LogDebugMessage ("BundleConfig.json: {0}", output);
Log.LogDebugMessage ($"BundleConfig.json: {output}");
File.WriteAllText (temp, output);

Comment thread
jonathanpeppers marked this conversation as resolved.
//NOTE: bundletool will not overwrite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ void CreateCodeBehindTaskItems (string groupName, List <ITaskItem> layoutItems,
string RegisterGroupWidgets (ICollection<LayoutWidget> widgets)
{
string key = Guid.NewGuid ().ToString ();
LogDebugMessage ($"Registering {widgets?.Count} widgets for key {key}");
LogDebugMessage ($"Registering {widgets.Count} widgets for key {key}");
BuildEngine4.RegisterTaskObjectAssemblyLocal (ProjectSpecificTaskObjectKey (key), widgets, RegisteredTaskObjectLifetime.Build, allowEarlyCollection: false);
return key;
}
Expand Down
10 changes: 8 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/ConvertCustomView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ public override bool RunTask ()
ITaskItem? resdir = ResourceDirectories?.FirstOrDefault (x => file.StartsWith (x.ItemSpec, StringComparison.OrdinalIgnoreCase));
switch (level) {
case TraceLevel.Error:
Log.FixupResourceFilenameAndLogCodedError ("XA1002", message, file, resdir?.ItemSpec, resource_name_case_map);
if (resdir != null)
Log.FixupResourceFilenameAndLogCodedError ("XA1002", message, file, resdir.ItemSpec, resource_name_case_map);
else
Log.LogCodedError ("XA1002", file: file, lineNumber: 0, message: message);
break;
case TraceLevel.Warning:
Log.FixupResourceFilenameAndLogCodedError ("XA1001", message, file, resdir?.ItemSpec, resource_name_case_map);
if (resdir != null)
Log.FixupResourceFilenameAndLogCodedError ("XA1001", message, file, resdir.ItemSpec, resource_name_case_map);
else
Log.LogCodedWarning ("XA1001", file: file, lineNumber: 0, message: message);
break;
default:
Log.LogDebugMessage (message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public override bool RunTask ()
RegisteredTaskObjectLifetime.Build
);

if (nativeCodeGenStates is null)
throw new InvalidOperationException ($"Internal error: {nameof (NativeCodeGenStateCollection)} not found");

// We only need the first architecture, since this task is architecture-agnostic
var templateCodeGenState = nativeCodeGenStates.States.First ().Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ void GenerateSourceForLayoutGroup (BindingGenerator generator, LayoutGroup group
if (!GetRequiredMetadata (item, CalculateLayoutCodeBehind.WidgetCollectionKeyMetadata, out collectionKey))
return;

ICollection<LayoutWidget> widgets = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<ICollection<LayoutWidget>> (ProjectSpecificTaskObjectKey (collectionKey), RegisteredTaskObjectLifetime.Build);
if ((widgets?.Count ?? 0) == 0) {
ICollection<LayoutWidget>? widgets = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal<ICollection<LayoutWidget>> (ProjectSpecificTaskObjectKey (collectionKey), RegisteredTaskObjectLifetime.Build);
if (widgets is null || widgets.Count == 0) {
string inputPaths = String.Join ("; ", resourceItems.Select (i => i.ItemSpec));
LogCodedWarning ("XA4222", Properties.Resources.XA4222, inputPaths);
return;
Expand Down Expand Up @@ -330,7 +330,7 @@ bool GenerateSource (StreamWriter writer, BindingGenerator generator, ICollectio
throw new InvalidOperationException ($"Widget {widget.Name} is of unknown type {widget.WidgetType}");
}
widget.Type = decayedType;
LogCodedWarning ("XA4225", Properties.Resources.XA4225, widget.Name, className, decayedType);
LogCodedWarning ("XA4225", Properties.Resources.XA4225, widget.Name ?? "", className, decayedType ?? "");
}

if (widget.Type.IsNullOrWhiteSpace ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public override bool RunTask ()
RegisteredTaskObjectLifetime.Build
);

if (nativeCodeGenStates is null)
throw new InvalidOperationException ($"Internal error: {nameof (NativeCodeGenState)} not found");

// We only need the first architecture, since this task is architecture-agnostic
var templateCodeGenState = nativeCodeGenStates.First ().Value;

Expand All @@ -72,6 +75,9 @@ public override bool RunTask ()
// it to a new object that doesn't require holding open Cecil AssemblyDefinitions.
var nativeCodeGenStateObject = MarshalMethodCecilAdapter.GetNativeCodeGenStateCollection (Log, nativeCodeGenStates);

if (nativeCodeGenStateObject is null)
throw new InvalidOperationException ($"Internal error: {nameof (NativeCodeGenStateCollection)} not created");

Log.LogDebugMessage ($"Saving {nameof (NativeCodeGenStateObject)} to {nameof (GenerateJavaStubs.NativeCodeGenStateObjectRegisterTaskKey)}");
BuildEngine4.RegisterTaskObjectAssemblyLocal (MonoAndroidHelper.GetProjectBuildSpecificTaskObjectKey (GenerateJavaStubs.NativeCodeGenStateObjectRegisterTaskKey, WorkingDirectory, IntermediateOutputDirectory), nativeCodeGenStateObject, RegisteredTaskObjectLifetime.Build);

Expand All @@ -82,7 +88,7 @@ public override bool RunTask ()
state.Resolver.Dispose ();
}

if (Log.HasLoggedErrors) {
if (Log.HasLoggedErrors && MergedAndroidManifestOutput != null) {
// Ensure that on a rebuild, we don't *skip* the `_GenerateJavaStubs` target,
// by ensuring that the target outputs have been deleted.
Files.DeleteFile (MergedAndroidManifestOutput, Log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public override bool RunTask ()
// Parse out the resources from the R.java file
CodeTypeDeclaration resources;
if (UseManagedResourceGenerator) {
var parser = new ManagedResourceParser () { Log = Log, JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile };
var parser = new ManagedResourceParser (Log) { JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile };
resources = parser.Parse (ResourceDirectory, RTxtFile ?? string.Empty, AdditionalResourceDirectories?.Select (x => x.ItemSpec), IsApplication, resource_fixup);
} else {
if (JavaResgenInputFile == null) {
throw new ArgumentNullException (nameof (JavaResgenInputFile));
}
var parser = new JavaResourceParser () { Log = Log };
var parser = new JavaResourceParser (Log);
resources = parser.Parse (JavaResgenInputFile, IsApplication, resource_fixup);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/GenerateRtxt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override bool RunTask ()
var resource_fixup = MonoAndroidHelper.LoadMapFile (BuildEngine4, Path.GetFullPath (CaseMapFile), StringComparer.OrdinalIgnoreCase);

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

// only update if it changed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ void GenerateAllTypeMappingsFromNativeState (bool useMarshalMethods)
RegisteredTaskObjectLifetime.Build
);

if (nativeCodeGenStates is null)
throw new InvalidOperationException ($"Internal error: {nameof (NativeCodeGenState)} not found");

NativeCodeGenState? templateCodeGenState = null;

foreach (var kvp in nativeCodeGenStates) {
Expand Down
12 changes: 6 additions & 6 deletions src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public override bool RunTask ()
var failedToParseMinSdk = !int.TryParse (min_sdk.Value, out int minSdkVersion);

if (failedToParseMinSdk || minSdkVersion < XABuildConfig.AndroidMinimumDotNetApiLevel.Major) {
Log.LogCodedError ("XA4216", Properties.Resources.XA4216_MinSdkVersion, min_sdk?.Value, XABuildConfig.AndroidMinimumDotNetApiLevel);
Log.LogCodedError ("XA4216", Properties.Resources.XA4216_MinSdkVersion, min_sdk.Value, XABuildConfig.AndroidMinimumDotNetApiLevel);
}

if (failedToParseMinSdk || minSdkVersion != supportedOsPlatformVersionAsInt) {
Log.LogCodedError ("XA1036", Properties.Resources.XA1036, min_sdk?.Value, SupportedOSPlatformVersion);
Log.LogCodedError ("XA1036", Properties.Resources.XA1036, min_sdk.Value, SupportedOSPlatformVersion ?? "");
}
}
if (target_sdk != null && (!int.TryParse (target_sdk.Value, out int targetSdkVersion) || targetSdkVersion < XABuildConfig.AndroidMinimumDotNetApiLevel.Major)) {
Expand Down Expand Up @@ -127,8 +127,8 @@ string GetTargetSdkVersion (string target, XAttribute? target_sdk)
// AndroidApiLevel is likely a *preview* API level; use it.
Log.LogWarningForXmlNode (
code: "XA4211",
file: AndroidManifest,
node: target_sdk,
file: AndroidManifest ?? "",
node: (object?) target_sdk ?? "",
message: Properties.Resources.XA4211,
messageArgs: new [] {
targetSdkVersion,
Expand All @@ -142,8 +142,8 @@ string GetTargetSdkVersion (string target, XAttribute? target_sdk)
targetSdk.Major < frameworkSdk.Major) {
Log.LogWarningForXmlNode (
code: "XA4211",
file: AndroidManifest,
node: target_sdk,
file: AndroidManifest ?? "",
node: (object?) target_sdk ?? "",
message: Properties.Resources.XA4211,
messageArgs: new [] {
targetSdkVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ protected override bool Validate ()
int apiLevel;
if (AndroidApplication && int.TryParse (AndroidApiLevel, out apiLevel)) {
if (apiLevel < 30)
Log.LogCodedWarning ("XA0113", Properties.Resources.XA0113, "v11.0", "30", TargetFrameworkVersion, AndroidApiLevel);
Log.LogCodedWarning ("XA0113", Properties.Resources.XA0113, "v11.0", "30", TargetFrameworkVersion ?? "", AndroidApiLevel ?? "");
if (apiLevel < 21)
Log.LogCodedWarning ("XA0117", Properties.Resources.XA0117, TargetFrameworkVersion);
Log.LogCodedWarning ("XA0117", Properties.Resources.XA0117, TargetFrameworkVersion ?? "");
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override bool ValidateJava (string javaExe, Regex versionRegex)
Log.LogCodedError ("XA0031", Properties.Resources.XA0031, requiredJavaForFrameworkVersion, $"$(TargetFrameworkVersion) {TargetFrameworkVersion}");
}
if (versionNumber < requiredJavaForBuildTools) {
Log.LogCodedError ("XA0032", Properties.Resources.XA0032, requiredJavaForBuildTools, AndroidSdkBuildToolsVersion);
Log.LogCodedError ("XA0032", Properties.Resources.XA0032, requiredJavaForBuildTools, AndroidSdkBuildToolsVersion ?? "");
}
var latest = Version.Parse (LatestSupportedJavaVersion);
if (versionNumber > latest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public override bool RunTask ()

if (AndroidSdkBuildToolsPath.IsNullOrEmpty ()) {
Log.LogCodedError ("XA5205", Properties.Resources.XA5205,
Aapt2, AndroidSdkPath, Path.DirectorySeparatorChar, Android);
Aapt2, AndroidSdkPath ?? "", Path.DirectorySeparatorChar, Android);
return false;
}

Expand All @@ -160,7 +160,7 @@ public override bool RunTask ()
}
}
if (Aapt2ToolPath.IsNullOrEmpty () || !File.Exists (Path.Combine (Aapt2ToolPath, aapt2Exe))) {
Log.LogCodedError ("XA0112", Properties.Resources.XA0112, Aapt2ToolPath);
Log.LogCodedError ("XA0112", Properties.Resources.XA0112, Aapt2ToolPath ?? "");
} else if (!GetAapt2Version (aapt2Exe)) {
Log.LogCodedError ("XA0111", Properties.Resources.XA0111, Aapt2ToolPath);
}
Expand All @@ -176,7 +176,7 @@ public override bool RunTask ()
}
if (ZipAlignPath.IsNullOrEmpty ()) {
Log.LogCodedError ("XA5205", Properties.Resources.XA5205,
ZipAlign, AndroidSdkPath, Path.DirectorySeparatorChar, Android);
ZipAlign, AndroidSdkPath ?? "", Path.DirectorySeparatorChar, Android);
return false;
}

Expand All @@ -185,7 +185,7 @@ public override bool RunTask ()

SequencePointsMode mode;
if (!Aot.TryGetSequencePointsMode (SequencePointsMode ?? "None", out mode))
Log.LogCodedError ("XA0104", Properties.Resources.XA0104, SequencePointsMode);
Log.LogCodedError ("XA0104", Properties.Resources.XA0104, SequencePointsMode ?? "");
AndroidSequencePointsMode = mode.ToString ();

AndroidApiLevelName = MonoAndroidHelper.SupportedVersions.GetIdFromApiLevel (AndroidApiLevel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.Android.Build.Tasks;
Expand Down Expand Up @@ -84,6 +85,9 @@ public override bool RunTask ()
RegisteredTaskObjectLifetime.Build
);

if (nativeCodeGenStates is null)
throw new InvalidOperationException ($"Internal error: {nameof (NativeCodeGenState)} not found");

// Parse environment files to determine configuration settings
// We need to parse the environment files supplied by the user to see if they want to use broken exception transitions. This information is needed
// in order to properly generate wrapper methods in the marshal methods assembly rewriter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,7 @@ public void CheckPerformanceOfManagedParser ()
long totalMS = 0;
int i;
for (i = 0; i < 100; i++) {
var parser = new ManagedResourceParser () {
Log = loggingHelper,
var parser = new ManagedResourceParser (loggingHelper) {
JavaPlatformDirectory = Path.Combine (AndroidSdkDirectory, "platforms", $"android-{platform.Major}"),
ResourceFlagFile = flagFile,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Xamarin.Android.Tasks
{
class FileResourceParser : ResourceParser
{
public FileResourceParser (TaskLoggingHelper log) : base (log) { }

public string? JavaPlatformDirectory { get; set; }

public string? ResourceFlagFile { get; set; }
Expand Down Expand Up @@ -389,7 +391,7 @@ void ProcessXmlFile (XmlReader reader, Dictionary<string, ICollection<R>> resour
try {
ProcessStyleable (reader.ReadSubtree (), resources);
} catch (Exception ex) {
Log?.LogErrorFromException (ex);
Log.LogErrorFromException (ex);
}
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public CodeTypeDeclaration Parse (string file, bool isApp, Dictionary<string, st
if (!File.Exists (file))
throw new InvalidOperationException ("Specified Java resource file was not found: " + file);

if (Log == null)
throw new InvalidOperationException ("Log property must be set before calling Parse");

CodeTypeDeclaration? resources = null;

using (var reader = File.OpenText (file)) {
Expand Down Expand Up @@ -53,7 +50,7 @@ public CodeTypeDeclaration Parse (string file, bool isApp, Dictionary<string, st
// }
List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration>>> Parser;

public JavaResourceParser ()
public JavaResourceParser (TaskLoggingHelper log) : base (log)
{
Parser = new List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration>>> () {
Parse ("^public final class R {",
Expand Down Expand Up @@ -90,7 +87,7 @@ public JavaResourceParser ()
(m, app, g, map) => {
g ??= new CodeTypeDeclaration ("Resource") { IsPartial = true };
var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name;
var f = new CodeMemberField (typeof (int), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log!)) {
var f = new CodeMemberField (typeof (int), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log)) {
Attributes = app ? MemberAttributes.Const | MemberAttributes.Public : MemberAttributes.Static | MemberAttributes.Public,
InitExpression = new CodePrimitiveExpression (ToInt32 (m.Groups [2].Value, m.Groups [2].Value.IndexOf ("0x", StringComparison.Ordinal) == 0 ? 16 : 10)),
Comments = {
Expand All @@ -104,7 +101,7 @@ public JavaResourceParser ()
(m, app, g, map) => {
g ??= new CodeTypeDeclaration ("Resource") { IsPartial = true };
var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name;
var f = new CodeMemberField (typeof (int[]), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log!)) {
var f = new CodeMemberField (typeof (int[]), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log)) {
// pity I can't make the member readonly...
Attributes = MemberAttributes.Public | MemberAttributes.Static,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
using System.Text.RegularExpressions;
using System.Text;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Utilities;


namespace Xamarin.Android.Tasks
{
class ManagedResourceParser : FileResourceParser
{
public ManagedResourceParser (TaskLoggingHelper log) : base (log) { }

class CompareTuple : IComparer<(int Key, CodeMemberField Value)>
Comment on lines +22 to 24
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 💡 Formatting — Missing blank line between the new constructor and the CompareTuple nested class declaration.

Rule: Minimal diffs / consistent formatting

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, added the blank line.

{
public int Compare((int Key, CodeMemberField Value) x, (int Key, CodeMemberField Value) y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ namespace Xamarin.Android.Tasks
{
class ResourceParser
{
public TaskLoggingHelper? Log { get; set; }
public TaskLoggingHelper Log { get; }

public ResourceParser (TaskLoggingHelper log)
{
Log = log;
}

internal int ToInt32 (string value, int @base)
{
Expand Down
Loading