Skip to content

Commit e113bc1

Browse files
[Xamarin.Android.Build.Tasks] .gitignore files in __AndroidLibraryProjects__.zip (dotnet#5113)
Fixes: https://developercommunity.visualstudio.com/content/problem/1141659/android-build-process-never-stops.html Context: https://www.nuget.org/packages/Xamarin.PdfView.Android/ In addition to: <AndroidResource Include="Resources\values\.gitignore" /> The Xamarin.PdfView.Android NuGet package causes the `<Aapt2Compile/>` MSBuild task to hang indefinitely. This is a package last updated 11/26/2015 that has a very weird file layout: Xamarin.PdfView.Android.1.0.4.nupkg lib\PdfViewBinding.dll __AndroidLibraryProjects__.zip library_project_imports\res\.gitignore The `res` directory is otherwise empty! The `<CollectNonEmptyDirectories/>` MSBuild task returned: _LibraryResourceFiles obj\Debug\lp\0\jl\res\.gitignore FilesCache = obj\Debug\lp\0\jl\res\..\files.cache Hash = obj\Debug\lp\0.stamp ResourceDirectory = obj\Debug\lp\0\jl\res StampFile = obj\Debug\lp\0\jl\res\.gitignore _ArchiveDirectory = obj\Debug\lp\0\jl\res\..\flat\ _FlatFile = res_.gitignore.flat And so `obj\Debug\lp\0\jl\res\.gitignore` is passed to `aapt2 compile`. a7f7ae6 successfully solved the issue for `.gitignore` (or any `.` prefix) files present in a project, but not NuGet packages. I think we should fix two things here: 1. The `aapt2 compile` command should skip files that start with `.`, in case somehow one is passed in. We should do this because it causes the `<Aapt2Compile/>` to hang indefinitely. 2. The `<CollectNonEmptyDirectories/>` MSBuild task should also ignore files that start with `.` I updated the `AndroidUpdateResourcesTest.InvalidFilenames` test so it checks this scenario.
1 parent e5d2576 commit e113bc1

File tree

7 files changed

+42
-4
lines changed

7 files changed

+42
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#### Application and library build and deployment
2+
3+
* [Developer Community 1141659][0]: Starting in Xamarin.Android
4+
11.0, the *Updating Resources...* build step could get stuck
5+
indefinitely if a NuGet package containing an **AndroidResource**
6+
file name starting with `.` was included in a project.
7+
8+
[0]: https://developercommunity.visualstudio.com/content/problem/1141659/android-build-process-never-stops.html

src/Xamarin.Android.Build.Tasks/Tasks/Aapt2.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public abstract class Aapt2 : AndroidAsyncTask {
4444

4545
public string ToolExe { get; set; }
4646

47+
/// <summary>
48+
/// Returns true if a filename starts with a . character.
49+
/// </summary>
50+
public static bool IsInvalidFilename (string path) =>
51+
Path.GetFileName (path).StartsWith (".", StringComparison.Ordinal);
52+
4753
protected string ResourceDirectoryFullPath (string resourceDirectory)
4854
{
4955
return (Path.IsPathRooted (resourceDirectory) ? resourceDirectory : Path.Combine (WorkingDirectory, resourceDirectory)).TrimEnd ('\\');

src/Xamarin.Android.Build.Tasks/Tasks/Aapt2Compile.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ void ProcessDirectory (ITaskItem item, object lockObject)
8585
outputArchive = Path.Combine (outputArchive, filename);
8686
expectedOutputFile = outputArchive;
8787
} else {
88+
if (IsInvalidFilename (fileOrDirectory)) {
89+
LogDebugMessage ($"Invalid filename, ignoring: {fileOrDirectory}");
90+
return;
91+
}
8892
expectedOutputFile = Path.Combine (outputArchive, flatFile);
8993
}
9094
RunAapt (GenerateCommandLineCommands (fileOrDirectory, isDirectory, outputArchive), expectedOutputFile);
@@ -120,4 +124,4 @@ protected string[] GenerateCommandLineCommands (string fileOrDirectory, bool isD
120124
}
121125

122126
}
123-
}
127+
}

src/Xamarin.Android.Build.Tasks/Tasks/AndroidComputeResPaths.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public override bool RunTask ()
117117
}
118118

119119
string baseFileName = LowercaseFilenames ? rel.ToLowerInvariant () : rel;
120-
if (Path.GetFileName (baseFileName).StartsWith (".", StringComparison.Ordinal)) {
121-
Log.LogDebugMessage ($"Skipping ignored file: {baseFileName}");
120+
if (Aapt2.IsInvalidFilename (baseFileName)) {
121+
Log.LogDebugMessage ($"Invalid filename, ignoring: {baseFileName}");
122122
continue;
123123
}
124124
if (Path.GetExtension (baseFileName) == ".axml")

src/Xamarin.Android.Build.Tasks/Tasks/CollectNonEmptyDirectories.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public override bool RunTask ()
100100
}
101101
output.Add (taskItem);
102102
foreach (var file in files) {
103+
if (Aapt2.IsInvalidFilename (file)) {
104+
Log.LogDebugMessage ($"Invalid filename, ignoring: {file}");
105+
continue;
106+
}
103107
var fileTaskItem = new TaskItem (file, new Dictionary<string, string> () {
104108
{ "ResourceDirectory", directory.ItemSpec },
105109
{ "StampFile", generateArchive ? stampFile : file },

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,10 @@ BuildItem CreateItem (string include) =>
13861386
};
13871387

13881388
var proj = new XamarinAndroidApplicationProject ();
1389+
// Package requires packages.config
1390+
if (!Builder.UseDotNet) {
1391+
proj.Packages.Add (KnownPackages.Xamarin_PdfView_Android);
1392+
}
13891393
proj.AndroidResources.Add (CreateItem ("Resources\\raw\\.foo"));
13901394
proj.AndroidResources.Add (CreateItem ("Resources\\raw\\.git"));
13911395
proj.AndroidResources.Add (CreateItem ("Resources\\raw\\.svn"));

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownPackages.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,19 @@ public static void AddDotNetCompatPackages (this IShortFormProject project)
636636
Version = "6.0.30",
637637
TargetFramework = "netstandard2.0",
638638
};
639-
639+
/// <summary>
640+
/// A NuGet package that has an EmbeddedResource in PdfViewBinding.dll:
641+
/// __AndroidLibraryProjects__.zip\library_project_imports\res\.gitignore
642+
/// </summary>
643+
public static Package Xamarin_PdfView_Android = new Package {
644+
Id = "Xamarin.PdfView.Android",
645+
Version = "1.0.4",
646+
References = {
647+
new BuildItem.Reference ("Xamarin.PdfView.Android") {
648+
MetadataValues = "HintPath=..\\packages\\Xamarin.PdfView.Android.1.0.4\\lib\\PdfViewBinding.dll"
649+
}
650+
},
651+
};
640652
public static Package ZXing_Net_Mobile = new Package {
641653
Id = "ZXing.Net.Mobile",
642654
Version = "2.4.1",

0 commit comments

Comments
 (0)