diff --git a/Documentation/guides/MSBuildBestPractices.md b/Documentation/guides/MSBuildBestPractices.md index c617539d79e..103a59f1b37 100644 --- a/Documentation/guides/MSBuildBestPractices.md +++ b/Documentation/guides/MSBuildBestPractices.md @@ -106,6 +106,63 @@ abbreviation could be used if the name is quite long, such as: [msbuild-transforms]: https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-transforms [msbuild-metadata]: https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-well-known-item-metadata +## Conditions + +You can skip an MSBuild `` or task with a `Condition` such +as: + +```xml + + + +``` + +If you want to skip the target if an item group is empty, you might be +tempted to do: + +```xml + + + +``` + +If you think about what this does, it's doing a `string.Join()` on +`@(MyItems)` to compare if it matches an empty string. Luckily MSBuild +has a "fast path" for evaluating against an empty string, but it still +can generate the log message: + +``` +Target "Foo" skipped, due to false condition; ('@(MyItems)' != '') was evaluated as ('A;B;C' != '') +``` + +If `@(MyItems)` was 100 full paths to files, this would be a long log +message! + +The solution is you should generally do this instead: + +```xml + + + +``` + +This causes MSBuild to always generate a reasonable log message: + +``` +Target "Foo" skipped, due to false condition; ('@(MyItems->Count())' != '0') was evaluated as ('100' != '0') +``` + +`->Count()` will return 0 even if the item group does not exist. See +the [MSBuild Documentation][itemfunctions] for details. + +Some links around the logging behavior: + +* https://github.com/dotnet/msbuild/issues/5315 +* https://github.com/dotnet/msbuild/pull/5553 +* https://github.com/dotnet/roslyn/pull/46445 + +[itemfunctions]: https://docs.microsoft.com/visualstudio/msbuild/item-functions + ## Incremental Builds The MSBuild Github repo has some [documentation][msbuild] on this diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets index 794f0562318..9d16cd208fe 100644 --- a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets @@ -194,12 +194,12 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. + Condition=" '$(_AndroidUseAapt2)' == 'True' And '@(_ProcessedCustomViews->Count())' != '0' "> <_ItemsToFixup Include="@(_CompileResourcesInputs)" Condition=" '@(_ProcessedCustomViews->'%(Identity)')' == '%(Identity)' "/> - + + Condition=" '$(UsingAndroidNETSdk)' != 'true' Or '@(InputJar->Count())' != '0' Or '@(EmbeddedJar->Count())' != '0' "> true diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Documentation.targets b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Documentation.targets index ad1e824a394..c292195177d 100644 --- a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Documentation.targets +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Documentation.targets @@ -57,7 +57,7 @@ This file is only used by binding projects. .NET 5 can eventually use it, once ` diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 80f64e85dd3..761f4c94a2e 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -754,7 +754,7 @@ because xbuild doesn't support framework reference assemblies. Files="@(Content)" Code="XA0101" Text="%40(Content) build action is not supported" - Condition=" '@(Content)' != '' " + Condition=" '@(Content->Count())' != '0' " /> @@ -1008,7 +1008,7 @@ because xbuild doesn't support framework reference assemblies. - + @@ -1032,7 +1032,7 @@ because xbuild doesn't support framework reference assemblies. + Condition=" '$(Language)' == 'C#' And ('$(AndroidGenerateLayoutBindings)' == 'True' Or '@(AndroidBoundLayout->Count())' != '0') "> - + - + - + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.EmbeddedResource.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.EmbeddedResource.targets index 88895a7735f..6f36b2e973b 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.EmbeddedResource.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.EmbeddedResource.targets @@ -94,7 +94,7 @@ This file is used by all project types, including binding projects.