Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] FileWrites&libraryprojectimports.cache (d…
Browse files Browse the repository at this point in the history
…otnet#7780)

On occasion we see the following error when building a Maui app:

	MyMauiApp/Platforms/Android/AndroidManifest.xml : error APT2260: resource style/Maui.SplashTheme (aka com.companyname.mymauiapp:style/Maui.SplashTheme) not found.

Looking in detail at the build log we notice that *none* of the
resources in the `$(IntermediateOutputPath)\lp` folder are being
included in the call to `aapt2`.

Looking further we see:

	Skipping target "_ResolveLibraryProjectImports" because all output files are up-to-date with respect to the input files.
	…
	Task "ReadLibraryProjectImportsCache" (TaskId:184)
	Task Parameter:CacheFile=obj/Debug/net7.0-android/libraryprojectimports.cache (TaskId:184)
	Task ReadLibraryProjectImportsCache (TaskId:184)
	    CacheFile: obj/Debug/net7.0-android/libraryprojectimports.cache (TaskId:184)
	   obj/Debug/net7.0-android/libraryprojectimports.cache does not exist. No Project Library Imports found (TaskId:184)
	Done executing task "ReadLibraryProjectImportsCache". (TaskId:184)

In this case it seems that the `.cache` file which stores the list of
resource directories was created, but was then deleted later on!

A search of our build system shows that `libraryprojectimports.cache`
was never added to the `@(FileWrites)` ItemGroup.  As a result
`libraryprojectimports.cache` gets deleted, but the `.stamp` file
which controls `_ResolveLibraryProjectImports` is left in place.
We end up in a situation where the target which generates the cache
will *never* run again because its stamp file is present.

Only a full Clean will fix this issue.

Fix this situation by updating the `_ResolveLibraryProjectImports`
target to add `libraryprojectimports.cache` to the `@(FileWrites)`
group, which prevents MSBuild from (occasionally) deleting the file
on [incremental clean][0].

[0]: https://github.com/xamarin/xamarin-android/blob/6cd0d38989178e1c3e5e1d71505b4aef26a1739b/Documentation/guides/MSBuildBestPractices.md#filewrites-and-incrementalclean
  • Loading branch information
dellis1972 committed Feb 15, 2023
1 parent 6cd0d38 commit c9ff96f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ public void ResolveLibraryProjectImports ([Values (true, false)] bool useAapt2)

b.BuildLogFile = "build2.log";
Assert.IsTrue (b.Build (proj), "second build should have succeeded.");
FileAssert.Exists (cacheFile);
var actual = ReadCache (cacheFile);
CollectionAssert.AreEqual (actual.Jars.Select (j => j.ItemSpec),
expected.Jars.Select (j => j.ItemSpec));
Expand All @@ -871,6 +872,7 @@ public void ResolveLibraryProjectImports ([Values (true, false)] bool useAapt2)

b.BuildLogFile = "build3.log";
Assert.IsTrue (b.Build (proj), "third build should have succeeded.");
FileAssert.Exists (cacheFile);
actual = ReadCache (cacheFile);
Assert.AreEqual (expected.Jars.Length + 1, actual.Jars.Length,
$"{nameof (expected.Jars)} should have one more item");
Expand All @@ -885,6 +887,7 @@ public void ResolveLibraryProjectImports ([Values (true, false)] bool useAapt2)
// Build with no changes, checking we are skipping targets appropriately
b.BuildLogFile = "build4.log";
Assert.IsTrue (b.Build (proj), "fourth build should have succeeded.");
FileAssert.Exists (cacheFile);
var targets = new List<string> {
"_UpdateAndroidResgen",
"_CompileJava",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ This file is used by all project types, including binding projects.
OutputImportDirectory="$(_AndroidLibrayProjectIntermediatePath)"
/>
<Touch Files="$(_AndroidStampDirectory)_ResolveLibraryProjectImports.stamp" AlwaysCreate="true" />
<ItemGroup>
<FileWrites Include="$(_AndroidLibraryProjectImportsCache)"
Condition="Exists ('$(_AndroidLibraryProjectImportsCache)')"/>
</ItemGroup>
</Target>

<Target Name="_ExtractLibraryProjectImports" DependsOnTargets="_ResolveLibraryProjectImports">
Expand Down

0 comments on commit c9ff96f

Please sign in to comment.