From 6623c7d4a66e94cbf528a07881835b3a87e88fbe Mon Sep 17 00:00:00 2001 From: Brendan Zagaeski Date: Wed, 10 Aug 2016 16:02:48 -0400 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Improve error text Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=43245 I wouldn't be surprised if this download mechanism gets redesigned at some point in the future. That could make these suggested changes obsolete. But I suspect the current download mechanism will be around for at least a little while longer, so adding these corrections and improvements will still be helpful. - `ErrorMessage()`: - Check for `InstallInstructions` first. If custom instructions are available via that property, they should take precedence over the more generic message for `PackageName`. - Rewrite the message for `PackageName`. The old error was incorrect. (For example, there was no "SDK installer" that would help resolve the error.) Additionally the new `if` statement in `AddAttributeValue()` removes some redundant appearances of this error, so the message can now provide more specific instructions. - `AddAttributeValue()`: - Add `if (sourceUrl.Name == null)` to avoid logging unnecessary additional errors. `MakeSureLibraryIsInPlace()` now provides appropriate errors for each possible failure when `sourceUrl.Name != null`, so additional errors are only needed if `sourceUrl.Name == null`. - `ExtractArchive()`: - Update the error message to account for the MD5 file names and the "zips" folder that were added in July 2015 (monodroid/master e8737f15c5e6f71f01f566475865a2cb34bfdfc9). Also remove the phrase "Please download" because re-downloading will not necessarily help with problems unzipping the file. - `MakeSureLibraryIsInPlace()`: - Update the error message to account for the MD5 file names and the "zips" folder that were added in July 2015 (monodroid/master e8737f15c5e6f71f01f566475865a2cb34bfdfc9). - Add a new explicit error with code "XA5217" for the case where a .zip file is extracted without error but is missing an expected nested archive. The old logic allowed the build process to continue even if all 3 attempts failed, and that could lead to some confusing error messages later in the build. - `Execute()`: - Add parentheses and "Details:" to the second part of each error message because the file name of the missing item is usually much less important than the first part of the error message. Examples of how to hit each error message ========================================= Setup ----- 1. Create a new "Android > App > Blank Android App" in Xamarin Studio on Mac. 2. Add the Xamarin.Android.Support.v4 NuGet package, version 23.4.0.1. 3. Move any existing cached downloads of the Java library dependencies out of the way (or delete them): - "$HOME/.local/share/Xamarin/Xamarin.Android.Support.v4/23.4.0.0" - "$HOME/.local/share/Xamarin/zips/F16A3455987DBAE5783F058F19F7FCDF.zip" Results when the download fails due to an exception --------------------------------------------------- For example, disconnect the computer from the internet and then build the project. > error XA5208: Download failed. Please download > https://dl-ssl.google.com/android/repository/android_m2repository_r32.zip > and copy it to > /Users/macuser/.local/share/Xamarin/zips/F16A3455987DBAE5783F058F19F7FCDF.zip > error XA5208: Reason: Error: NameResolutionFailure Results when the downloaded file can't be unzipped -------------------------------------------------- For example, manually download the file, copy it into place, and then disable read permissions (`chmod a-r`) on the file. > error XA5209: Unzipping failed while attempting to extract > /Users/macuser/.local/share/Xamarin/zips/F16A3455987DBAE5783F058F19F7FCDF.zip > into > /Users/macuser/.local/share/Xamarin/Xamarin.Android.Support.v4/23.4.0.0/content > error XA5209: Reason: File > /Users/macuser/.local/share/Xamarin/zips/F16A3455987DBAE5783F058F19F7FCDF.zip > could not be opened Results when extracting an embeddedArchive fails 3 times -------------------------------------------------------- For example, replace "$HOME/.local/share/Xamarin/zips/F16A3455987DBAE5783F058F19F7FCDF.zip" with a dummy `.zip` archive that doesn't contain any of the correct files. > error XA5217: Expected file > /Users/macuser/.local/share/Xamarin/Xamarin.Android.Support.v4/23.4.0.0/content/m2repository/com/android/support/support-v4/23.4.0/support-v4-23.4.0.aar > does not exist after extracting > /Users/macuser/.local/share/Xamarin/zips/F16A3455987DBAE5783F058F19F7FCDF.zip Results when a custom attribute contains a PackageName but no SourceUrl or InstallInstructions ---------------------------------------------------------------------------------------------- For example, remove the Xamarin.Android.Support.v4 package and add a reference to a blank Xamarin.Android library project that includes the following lines in the `AssemblyInfo.cs` file: [assembly: Java.Interop.JavaLibraryReference ("foo.jar", PackageName = "Foo", Version = "0.0.0.0")] > error XA5207: Unable to restore Java resources for package 'Foo'. The > authors of the package should set the SourceUrl property or > InstallInstructions property on the > Java.Interop.JavaLibraryReferenceAttribute. (Details: Java library > file foo.jar doesn't exist.) --- .../GetAdditionalResourcesFromAssemblies.cs | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs index 1c4d1620b41..bf0c4c10c00 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs @@ -80,18 +80,18 @@ internal static string ErrorMessage (CustomAttribute attr) { if (!attr.HasProperties) return ""; - CustomAttributeNamedArgument arg = attr.Properties.FirstOrDefault (p => p.Name == "PackageName"); - if (arg.Name != null) { - string packageName = arg.Argument.Value as string; - if (packageName != null) - return string.Format ("Please install package: '{0}' available in SDK installer", packageName); - } - arg = attr.Properties.FirstOrDefault (p => p.Name == "InstallInstructions"); + CustomAttributeNamedArgument arg = attr.Properties.FirstOrDefault (p => p.Name == "InstallInstructions"); if (arg.Name != null) { string instInstructs = arg.Argument.Value as string; if (instInstructs != null) return "Installation instructions: " + instInstructs; } + arg = attr.Properties.FirstOrDefault (p => p.Name == "PackageName"); + if (arg.Name != null) { + string packageName = arg.Argument.Value as string; + if (packageName != null) + return string.Format ("Unable to restore Java resources for package '{0}'. The authors of the package should set the SourceUrl property or InstallInstructions property on the {1}", packageName, attr.AttributeType); + } return null; } @@ -147,7 +147,8 @@ void AddAttributeValue (ICollection items, CustomAttribute attr, string items.Add (Path.GetFullPath (path).TrimEnd (Path.DirectorySeparatorChar)); return; } - LogCodedError (errorCode, errorFmt, ErrorMessage (attr), path); + if (sourceUrl.Name == null) + LogCodedError (errorCode, errorFmt, ErrorMessage (attr), path); } bool ExtractArchive (string url, string file, string contentDir) @@ -172,7 +173,7 @@ bool ExtractArchive (string url, string file, string contentDir) } } catch (Exception e) { - LogCodedError ("XA5209", "Unzipping failed. Please download {0} and extract it to the {1} directory.", url, contentDir); + LogCodedError ("XA5209", "Unzipping failed while attempting to extract {0} into {1}{2}", file, contentDir, Path.DirectorySeparatorChar); LogCodedError ("XA5209", "Reason: {0}", e.Message); Log.LogMessage (MessageImportance.Low, e.ToString ()); Directory.Delete (contentDir, true); @@ -230,7 +231,7 @@ string MakeSureLibraryIsInPlace (string destinationBase, string url, string vers client.DownloadFileTaskAsync (url, file).Wait (Token); LogMessage (" Downloading Complete"); } catch (Exception e) { - LogCodedError ("XA5208", "Download failed. Please download {0} and put it to the {1} directory.", url, destinationDir); + LogCodedError ("XA5208", "Download failed. Please download {0} and copy it to {1}", url, file); LogCodedError ("XA5208", "Reason: {0}", e.GetBaseException ().Message); Log.LogMessage (MessageImportance.Low, e.ToString ()); if (File.Exists (file)) @@ -260,7 +261,10 @@ string MakeSureLibraryIsInPlace (string destinationBase, string url, string vers if (Log.HasLoggedErrors) break; if (!success) { - Log.LogWarning ("Expected File {0} does not exist. Trying to extract again.", Path.Combine (contentDir, embeddedArchive)); + if (attempt < 2) + Log.LogWarning ("Expected file {0} does not exist. Trying to extract again.", Path.Combine (contentDir, embeddedArchive)); + else + LogCodedError ("XA5217", "Expected file {0} does not exist after extracting {1}", Path.Combine (contentDir, embeddedArchive), file); if (Directory.Exists (contentDir)) Directory.Delete (contentDir, recursive: true); } @@ -319,13 +323,13 @@ public override bool Execute () foreach (var ca in resolver.GetAssembly (assemblyItem.ItemSpec).CustomAttributes) { switch (ca.AttributeType.FullName) { case "Android.IncludeAndroidResourcesFromAttribute": - AddAttributeValue (androidResources, ca, "XA5206", "{0}. Android resource directory {1} doesn't exist.", true, fullPath); + AddAttributeValue (androidResources, ca, "XA5206", "{0}. (Details: Android resource directory {1} doesn't exist.)", true, fullPath); break; case "Java.Interop.JavaLibraryReferenceAttribute": - AddAttributeValue (javaLibraries, ca, "XA5207", "{0}. Java library file {1} doesn't exist.", false, fullPath); + AddAttributeValue (javaLibraries, ca, "XA5207", "{0}. (Details: Java library file {1} doesn't exist.)", false, fullPath); break; case "Android.NativeLibraryReferenceAttribute": - AddAttributeValue (nativeLibraries, ca, "XA5210", "{0}. Native library file {1} doesn't exist.", false, fullPath); + AddAttributeValue (nativeLibraries, ca, "XA5210", "{0}. (Details: Native library file {1} doesn't exist.)", false, fullPath); break; } }