forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] skip XA1034 logic in some cases (dotnet…
…#7680) After profiling a build for a `dotnet new maui` app, I noticed: 193.41ms xamarin.android.build.tasks!Xamarin.Android.Tasks.MonoAndroidHelper.HasResourceDesignerAssemblyReference() Which equates to: Top 10 most expensive tasks ... AppendCustomMetadataToItemGroup = 204 ms In dc3ccf2, we added this check to emit `XA1034` when `$(AndroidUseDesignerAssembly)` is false. And so we don't actually need to validate this value when it is `true` (the default). I moved the logic from the `<AppendCustomMetadataToItemGroup/>` MSBuild task to a new `<CheckForInvalidDesignerConfig/>` task. In most projects, it will be completely skipped: Target "_CheckForInvalidDesignerConfig" skipped, due to false condition; ( '$(AndroidUseDesignerAssembly)' != 'True' ) was evaluated as ( 'true' != 'True' ). After these changes, I get a much better number in projects with default settings: AppendCustomMetadataToItemGroup = 2 ms Just by moving the task around, it is faster in general: CheckForInvalidDesignerConfig = 11 ms This is due to less assemblies passed in than before: Assemblies="@(_MonoAndroidReferencePath);@(_MonoAndroidReferenceDependencyPaths)" While previously, we were checking *all* assemblies.
- Loading branch information
1 parent
c92ae5e
commit 8c24b8f
Showing
4 changed files
with
58 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Xamarin.Android.Build.Tasks/Tasks/CheckForInvalidDesignerConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
using System.IO; | ||
|
||
namespace Xamarin.Android.Tasks | ||
{ | ||
/// <summary> | ||
/// Emits XA1034 is an assembly has a reference to _Microsoft.Android.Resource.Designer.dll | ||
/// * NOTE: only called when $(AndroidUseDesignerAssembly) is false | ||
/// </summary> | ||
public class CheckForInvalidDesignerConfig : AndroidTask | ||
{ | ||
public override string TaskPrefix => "CIRF"; | ||
|
||
public ITaskItem[] Assemblies { get; set; } | ||
|
||
public override bool RunTask () | ||
{ | ||
if (Assemblies == null) | ||
return true; | ||
|
||
foreach (var assembly in Assemblies) { | ||
if (HasResourceDesignerAssemblyReference (assembly)) { | ||
Log.LogCodedError ("XA1034", Properties.Resources.XA1034, assembly); | ||
} | ||
} | ||
|
||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
static bool HasResourceDesignerAssemblyReference (ITaskItem assembly) | ||
{ | ||
if (!File.Exists (assembly.ItemSpec)) { | ||
return false; | ||
} | ||
using var pe = new PEReader (File.OpenRead (assembly.ItemSpec)); | ||
var reader = pe.GetMetadataReader (); | ||
return HasResourceDesignerAssemblyReference (reader); | ||
} | ||
|
||
static bool HasResourceDesignerAssemblyReference (MetadataReader reader) | ||
{ | ||
foreach (var handle in reader.AssemblyReferences) { | ||
var reference = reader.GetAssemblyReference (handle); | ||
var name = reader.GetString (reference.Name); | ||
if (string.CompareOrdinal (name, "_Microsoft.Android.Resource.Designer") == 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters