From 04340244c3cb1753a987b6809a91631dc883b035 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Sat, 7 Dec 2024 12:55:42 +0000 Subject: [PATCH] Fix AndroidResource Incremental build issue (#9592) Fixes #9588 Adding a new Android resource to a project causes the incremental build to error with: ``` Resources\layout\activity_two.xml error APT2126: file not found. ``` This error comes from the `Aapt2Compile` task. It is looking for `\obj\Debug\net9.0-android\res\layout\activity_two.xml` but that file doesn't exist. What was happening is the new file had the same modified date as the flag/stamp file that was used to generate it. The user was using `copy Resources\layout\activity_main.xml Resources\layout\activity_two.xml`. So the modified date was not "new". This caused the _GenerateAndroidResourceDir target to get skipped. What we should do is copy what we do in other places, and hash all the input files (and their destinations) into one hash. We can then use that hash file as an input to the target. This allows us to detect when a new file is added, even if it has an old timestamp. A unit test was added as well. --- .../Tasks/AndroidComputeResPaths.cs | 7 ++++++ .../IncrementalBuildTest.cs | 23 +++++++++++++++++++ .../Xamarin.Android.Common.targets | 14 ++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs index 4115073151c..8edd9df3e2c 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs @@ -61,6 +61,9 @@ public class AndroidComputeResPaths : AndroidTask [Output] public ITaskItem [] ResolvedResourceFiles { get; set; } + [Output] + public string FilesHash { get; set; } + public override bool RunTask () { var intermediateFiles = new List (ResourceFiles.Length); @@ -77,6 +80,7 @@ public override bool RunTask () } var nameCaseMap = new Dictionary (ResourceFiles.Length, StringComparer.Ordinal); + var sb = new StringBuilder (); for (int i = 0; i < ResourceFiles.Length; i++) { var item = ResourceFiles [i]; @@ -151,10 +155,13 @@ public override bool RunTask () item.CopyMetadataTo (newItem); intermediateFiles.Add (newItem); resolvedFiles.Add (item); + // write both files so we handle changes in destination also + sb.AppendLine ($"{item.ItemSpec};{newItem.ItemSpec}"); } IntermediateFiles = intermediateFiles.ToArray (); ResolvedResourceFiles = resolvedFiles.ToArray (); + FilesHash = Files.HashString (sb.ToString ()); MonoAndroidHelper.SaveResourceCaseMap (BuildEngine4, nameCaseMap, ProjectSpecificTaskObjectKey); return !Log.HasLoggedErrors; } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs index a06312f2fb3..b54406422bc 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/IncrementalBuildTest.cs @@ -902,6 +902,29 @@ ReadLibraryProjectImportsCache ReadCache (string cacheFile) return task; } + [Test] + [NonParallelizable] + public void AddNewAndroidResourceOnSecondBuild () + { + var xml = new AndroidItem.AndroidResource (@"Resources\values\emptyvalues.xml") { + TextContent = () => "" + }; + + var proj = new XamarinAndroidApplicationProject (); + using (var b = CreateApkBuilder (Path.Combine ("temp", TestName))) { + var projectFile = Path.Combine (Root, b.ProjectDirectory, proj.ProjectFilePath); + b.ThrowOnBuildFailure = false; + Assert.IsTrue (b.Build (proj), "Build should have succeeded."); + b.Output.AssertTargetIsNotSkipped ("_GenerateAndroidResourceDir"); + proj.OtherBuildItems.Add (xml); + b.Save (proj, doNotCleanupOnUpdate: true); + var modified = File.GetLastWriteTimeUtc (Path.Combine (Root, b.ProjectDirectory, "Resources","layout","Main.axml")); + File.SetLastWriteTimeUtc (Path.Combine (Root, b.ProjectDirectory, "Resources","values", "emptyvalues.xml"), modified); + Assert.IsTrue (b.Build (proj), "Build should have succeeded."); + b.Output.AssertTargetIsNotSkipped ("_GenerateAndroidResourceDir"); + } + } + [Test] [NonParallelizable] public void InvalidAndroidResource () diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 19a844eab71..74b3b78a750 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -232,6 +232,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. <_AndroidBuildIdFile>$(IntermediateOutputPath)buildid.txt <_AndroidApplicationSharedLibraryPath>$(IntermediateOutputPath)app_shared_libraries\ <_ResolvedUserAssembliesHashFile>$(IntermediateOutputPath)resolvedassemblies.hash + <_AndroidResolvedResourcesHashFile>$(IntermediateOutputPath)_AndroidResolvedResources.hash d8 <_AndroidXA1027 Condition=" '$(EnableProguard)' == 'true' And '$(AndroidEnableProguard)' == '' And '$(AndroidDexTool)' == 'd8' And $(AndroidLinkTool) == '' ">true @@ -1057,13 +1058,24 @@ because xbuild doesn't support framework reference assemblies. > + + + + + +