Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AndroidResource Incremental build issue #9592

Merged
merged 3 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading