Skip to content

Commit

Permalink
Fix AndroidResource Incremental build issue (#9592)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dellis1972 authored Dec 7, 2024
1 parent 0cff647 commit 0434024
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITaskItem> (ResourceFiles.Length);
Expand All @@ -77,6 +80,7 @@ public override bool RunTask ()
}

var nameCaseMap = new Dictionary<string, string> (ResourceFiles.Length, StringComparer.Ordinal);
var sb = new StringBuilder ();

for (int i = 0; i < ResourceFiles.Length; i++) {
var item = ResourceFiles [i];
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => "<?xml version=\"1.0\" encoding=\"utf-8\" ?><resources></resources>"
};

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 ()
Expand Down
14 changes: 13 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<_AndroidBuildIdFile>$(IntermediateOutputPath)buildid.txt</_AndroidBuildIdFile>
<_AndroidApplicationSharedLibraryPath>$(IntermediateOutputPath)app_shared_libraries\</_AndroidApplicationSharedLibraryPath>
<_ResolvedUserAssembliesHashFile>$(IntermediateOutputPath)resolvedassemblies.hash</_ResolvedUserAssembliesHashFile>
<_AndroidResolvedResourcesHashFile>$(IntermediateOutputPath)_AndroidResolvedResources.hash</_AndroidResolvedResourcesHashFile>

<AndroidDexTool Condition=" '$(AndroidDexTool)' != 'dx' ">d8</AndroidDexTool>
<_AndroidXA1027 Condition=" '$(EnableProguard)' == 'true' And '$(AndroidEnableProguard)' == '' And '$(AndroidDexTool)' == 'd8' And $(AndroidLinkTool) == '' ">true</_AndroidXA1027>
Expand Down Expand Up @@ -1057,13 +1058,24 @@ because xbuild doesn't support framework reference assemblies.
>
<Output ItemName="_AndroidResourceDest" TaskParameter="IntermediateFiles" />
<Output ItemName="_AndroidResolvedResources" TaskParameter="ResolvedResourceFiles" />
<Output PropertyName="_AndroidResolvedResourcesFilesHash" TaskParameter="FilesHash" />
</AndroidComputeResPaths>

<WriteLinesToFile
File="$(_AndroidResolvedResourcesHashFile)"
Lines="$(_AndroidResolvedResourcesFilesHash)"
Overwrite="True"
WriteOnlyWhenDifferent="True"
/>
<ItemGroup>
<FileWrites Include="$(_AndroidResolvedResourcesHashFile)"/>
</ItemGroup>

<MakeDir Directories="$(MonoAndroidResDirIntermediate)" />
</Target>

<Target Name="_GenerateAndroidResourceDir"
Inputs="$(MSBuildProjectFullPath);@(_AndroidMSBuildAllProjects);@(_AndroidResolvedResources);$(_AndroidBuildPropertiesCache)"
Inputs="$(MSBuildProjectFullPath);@(_AndroidMSBuildAllProjects);@(_AndroidResolvedResources);$(_AndroidBuildPropertiesCache);$(_AndroidResolvedResourcesHashFile)"
Outputs="$(_AndroidResFlagFile)"
DependsOnTargets="$(_OnResolveMonoAndroidSdks)">
<CheckForInvalidResourceFileNames
Expand Down

0 comments on commit 0434024

Please sign in to comment.