Skip to content

Commit 633a98e

Browse files
[One .NET] fix build ordering & BuildItem.Deleted (#4933)
The `AndroidUpdateResourcesTest.CheckFilesAreRemoved` test was failing with: Resources\values\Theme.xml error XA2001: Source file 'Resources\values\Theme.xml' could not be found. When looking through logs, I noticed lots of differences in build ordering from legacy projects. 1. `$(BuildDependsOn)` ordering - In legacy projects `ImplicitlyExpandDesignTimeFacades` is listed last, while it was in the middle for .NET 5+ projects. This was caused by the ordering of `<Import/>`. `Microsoft.Android.Sdk.BuildOrder.targets` needs to be imported before `Xamarin.Android.Common.targets`. 2. `$(BuildDependsOn)` also listed these targets in legacy projects: _CheckTargetFramework; _RemoveLegacyDesigner; `_CheckTargetFramework` is not needed, but it needs to be moved to `Xamarin.Android.Legacy.targets`. `_RemoveLegacyDesigner` should be listed in `$(BuildDependsOn)`. 3. `_UpdateAndroidResources` had different values for `DependsOnTargets`. In c87ef95, I removed `$(CoreResolveReferencesDependsOn)` to just simplify things. It turns out `_UpdateAndroidResources` uses this property. I put things back the way they were. After fixing the build ordering, the test still failed. Then I noticed the *actual* cause to the failing test... There were additional `@(AndroidResource)` items in the `.csproj` file. `BuildItem.Deleted` wasn't being checked when short-form `.csproj` files are generated. We should omit build items with this flag set. After doing this, we can run a couple additional tests under `dotnet` context.
1 parent 277be16 commit 633a98e

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
7777
_CreateNativeLibraryArchive;
7878
_AddAndroidEnvironmentToCompile;
7979
_CheckForContent;
80+
_RemoveLegacyDesigner;
8081
_ValidateAndroidPackageProperties;
8182
$(BuildDependsOn);
8283
</BuildDependsOn>
@@ -87,10 +88,13 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
8788

8889
<!-- Targets that run unless we are running _ComputeFilesToPublishForRuntimeIdentifiers -->
8990
<PropertyGroup Condition=" '$(_ComputeFilesToPublishForRuntimeIdentifiers)' != 'true' ">
90-
<ResolveReferencesDependsOn>
91+
<CoreResolveReferencesDependsOn>
9192
_SeparateAppExtensionReferences;
9293
$(ResolveReferencesDependsOn);
9394
_AddAndroidCustomMetaData;
95+
</CoreResolveReferencesDependsOn>
96+
<ResolveReferencesDependsOn>
97+
$(CoreResolveReferencesDependsOn);
9498
UpdateAndroidInterfaceProxies;
9599
UpdateAndroidResources;
96100
AddBindingsToCompile;

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222

2323
<!-- Default item includes (globs and implicit references) -->
2424
<Import Project="Microsoft.Android.Sdk.DefaultItems.targets" />
25+
<!-- Build ordering, should be imported before Xamarin.Android.Common.targets -->
26+
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.BuildOrder.targets" />
2527

2628
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.targets" />
2729
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Bindings.Core.targets" />
2830
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Bindings.ClassParse.targets" />
2931
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.AssemblyResolution.targets" />
30-
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.BuildOrder.targets" />
3132
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.Linker.targets" />
3233
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.NuGet.targets" />
3334
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.Publish.targets" />

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AndroidUpdateResourcesTest.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ namespace Xamarin.Android.Build.Tests
1717
[Parallelizable (ParallelScope.Children)]
1818
public class AndroidUpdateResourcesTest : BaseTest
1919
{
20+
string GetResourceDesignerPath (ProjectBuilder builder, XamarinAndroidProject project)
21+
{
22+
string path;
23+
if (Builder.UseDotNet) {
24+
path = Path.Combine (Root, builder.ProjectDirectory, project.IntermediateOutputPath);
25+
} else {
26+
path = Path.Combine (Root, builder.ProjectDirectory, "Resources");
27+
}
28+
return Path.Combine (path, "Resource.designer" + project.Language.DefaultDesignerExtension);
29+
}
30+
2031
[Test]
32+
[Category ("dotnet")]
2133
public void CheckMultipleLibraryProjectReferenceAlias ([Values (true, false)] bool withGlobal)
2234
{
2335
var path = Path.Combine (Root, "temp", TestName);
@@ -47,7 +59,8 @@ public void CheckMultipleLibraryProjectReferenceAlias ([Values (true, false)] bo
4759
using (var b = CreateApkBuilder (Path.Combine (path, proj.ProjectName), cleanupAfterSuccessfulBuild: false, cleanupOnDispose: false)) {
4860
b.ThrowOnBuildFailure = false;
4961
Assert.IsTrue (b.Build (proj), "Project should have built.");
50-
string[] text = File.ReadAllLines (Path.Combine (Root, path, proj.ProjectName, "Resources", "Resource.designer.cs"));
62+
string resource_designer_cs = GetResourceDesignerPath (b, proj);
63+
string [] text = File.ReadAllLines (resource_designer_cs);
5164
Assert.IsTrue (text.Count (x => x.Contains ("Library1.Resource.String.library_name")) == 2, "library_name resource should be present exactly once for each library");
5265
Assert.IsTrue (text.Count (x => x == "extern alias Lib1A;" || x == "extern alias Lib1B;") <= 1, "No more than one extern alias should be present for each library.");
5366
}
@@ -799,6 +812,7 @@ public void CheckAaptErrorRaisedForDuplicateResourceinApp ()
799812
}
800813

801814
[Test]
815+
[Category ("dotnet")]
802816
public void CheckFilesAreRemoved () {
803817

804818
var proj = new XamarinAndroidApplicationProject () {
@@ -818,9 +832,9 @@ public void CheckFilesAreRemoved () {
818832
KnownPackages.AndroidSupportV4_27_0_2_1,
819833
},
820834
};
821-
proj.SetProperty (KnownProperties.TargetFrameworkVersion, "v5.1");
822-
using (var builder = CreateApkBuilder ("temp/CheckFilesAreRemoved", false, false)) {
823-
builder.Verbosity = LoggerVerbosity.Diagnostic;
835+
if (!Builder.UseDotNet)
836+
proj.SetProperty (KnownProperties.TargetFrameworkVersion, "v5.1");
837+
using (var builder = CreateApkBuilder ()) {
824838
Assert.IsTrue (builder.Build (proj), "Build should have succeeded");
825839

826840
var theme = proj.AndroidResources.First (x => x.Include () == "Resources\\values\\Theme.xml");
@@ -834,9 +848,10 @@ public void CheckFilesAreRemoved () {
834848
}
835849

836850
[Test]
851+
[Category ("dotnet")]
837852
public void CheckDontUpdateResourceIfNotNeeded ()
838853
{
839-
var path = Path.Combine ("temp", "CheckDontUpdateResourceIfNotNeeded");
854+
var path = Path.Combine ("temp", TestName);
840855
var foo = new BuildItem.Source ("Foo.cs") {
841856
TextContent = () => @"using System;
842857
namespace Lib1 {
@@ -879,10 +894,8 @@ public string GetFoo () {
879894
},
880895
};
881896
using (var libBuilder = CreateDllBuilder (Path.Combine (path, libProj.ProjectName), false, false)) {
882-
libBuilder.Verbosity = LoggerVerbosity.Diagnostic;
883897
Assert.IsTrue (libBuilder.Build (libProj), "Library project should have built");
884898
using (var appBuilder = CreateApkBuilder (Path.Combine (path, appProj.ProjectName), false, false)) {
885-
appBuilder.Verbosity = LoggerVerbosity.Diagnostic;
886899
Assert.IsTrue (appBuilder.Build (appProj), "Application Build should have succeeded.");
887900
Assert.IsFalse (appBuilder.Output.IsTargetSkipped ("_UpdateAndroidResgen"), "_UpdateAndroidResgen target not should be skipped.");
888901
foo.Timestamp = DateTimeOffset.UtcNow;
@@ -912,8 +925,9 @@ public string GetFoo () {
912925
Assert.IsFalse (libBuilder.Output.IsTargetSkipped ("_CreateManagedLibraryResourceArchive"), "_CreateManagedLibraryResourceArchive should not be skipped.");
913926
appBuilder.BuildLogFile = "build2.log";
914927
Assert.IsTrue (appBuilder.Build (appProj, doNotCleanupOnUpdate: true, saveProject: false), "Application Build should have succeeded.");
915-
string text = File.ReadAllText (Path.Combine (Root, path, appProj.ProjectName, "Resources", "Resource.designer.cs"));
916-
Assert.IsTrue (text.Contains ("theme_devicedefault_background2"), "Resource.designer.cs was not updated.");
928+
string resource_designer_cs = GetResourceDesignerPath (appBuilder, appProj);
929+
string text = File.ReadAllText (resource_designer_cs);
930+
StringAssert.Contains ("theme_devicedefault_background2", text, "Resource.designer.cs was not updated.");
917931
Assert.IsFalse (appBuilder.Output.IsTargetSkipped ("_UpdateAndroidResgen"), "_UpdateAndroidResgen target should NOT be skipped.");
918932
Assert.IsFalse (appBuilder.Output.IsTargetSkipped ("_CreateBaseApk"), "_CreateBaseApk target should NOT be skipped.");
919933
Assert.IsFalse (appBuilder.Output.IsTargetSkipped ("_BuildApkEmbed"), "_BuildApkEmbed target should NOT be skipped.");
@@ -926,7 +940,7 @@ public string GetFoo () {
926940
rawToDelete.Timestamp = DateTimeOffset.UtcNow;
927941
theme.Deleted = true;
928942
theme.Timestamp = DateTimeOffset.UtcNow;
929-
Assert.IsTrue (libBuilder.Build (libProj, saveProject: true), "Library project should have built");
943+
Assert.IsTrue (libBuilder.Build (libProj, doNotCleanupOnUpdate: true, saveProject: true), "Library project should have built");
930944
var themeFile = Path.Combine (Root, path, libProj.ProjectName, libProj.IntermediateOutputPath, "res", "values", "theme.xml");
931945
Assert.IsTrue (!File.Exists (themeFile), $"{themeFile} should have been deleted.");
932946
var archive = Path.Combine (Root, path, libProj.ProjectName, libProj.IntermediateOutputPath, "__AndroidLibraryProjects__.zip");

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Utilities/XmlUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public static string ToXml (IShortFormProject project)
5959

6060
static void AppendBuildItem (StringBuilder sb, BuildItem bi)
6161
{
62+
if (bi.Deleted)
63+
return;
6264
sb.Append ($"\t\t<{bi.BuildAction} ");
6365
if (bi.Include != null) sb.Append ($"Include=\"{bi.Include ()}\" ");
6466
if (bi.Update != null) sb.Append ($"Update=\"{bi.Update ()}\" ");

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,10 +754,6 @@ because xbuild doesn't support framework reference assemblies.
754754
EmbeddedNativeLibraries="@(EmbeddedNativeLibrary)" />
755755
</Target>
756756

757-
<Target Name="_CheckTargetFramework">
758-
<Warning Code="XA0109" Text="Unsupported or invalid %24(TargetFrameworkVersion) value of 'v4.5'. Please update your Project Options." Condition=" '$(TargetFrameworkVersion)' == 'v4.5' "/>
759-
</Target>
760-
761757
<Target Name="_CheckForContent">
762758
<LogWarningsForFiles
763759
Files="@(Content)"

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Legacy.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ projects. .NET 5 projects will not import this file.
198198
<UsingTask TaskName="Xamarin.Android.Tasks.Legacy.ResolveAndroidTooling" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
199199
<UsingTask TaskName="Xamarin.Android.Tasks.LinkAssemblies" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
200200

201+
<Target Name="_CheckTargetFramework">
202+
<Warning Code="XA0109" Text="Unsupported or invalid %24(TargetFrameworkVersion) value of 'v4.5'. Please update your Project Options." Condition=" '$(TargetFrameworkVersion)' == 'v4.5' "/>
203+
</Target>
204+
201205
<Target Name="_GetReferenceAssemblyPaths">
202206
<GetReferenceAssemblyPaths
203207
TargetFrameworkMoniker="$(TargetFrameworkIdentifier),Version=v1.0"

0 commit comments

Comments
 (0)