Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Improve Aapt error message handling (do…
Browse files Browse the repository at this point in the history
…tnet#703)

Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=36485

Under some conditions apps produces an error which does NOT
contain "error <something>:". As a result our regex was not picking
up some errors and was just printing the default error of

	"aapt.exe" exited with code 1

This commit updates the regex to allow for errors which do
not include "error <something>:". And adds a unit test for the
specific test case.
  • Loading branch information
dellis1972 authored and jonpryor committed Jul 27, 2017
1 parent 0a2f008 commit 7dadd00
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ protected void LogEventsFromTextOutput (string singleLine, MessageImportance mes

if (match.Success) {
var file = match.Groups["file"].Value;
var line = int.Parse (match.Groups["line"].Value) + 1;
int line = 0;
if (!string.IsNullOrEmpty (match.Groups["line"]?.Value))
line = int.Parse (match.Groups["line"].Value) + 1;
var error = match.Groups["message"].Value;

// Try to map back to the original resource file, so when the user
Expand Down
3 changes: 2 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/AndroidToolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ protected virtual Regex ErrorRegex {
// res\layout\main.axml:7: error: No resource identifier found for attribute 'id2' in package 'android' (TaskId:22)
// Resources/values/theme.xml(2): error APT0000: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.AppCompat'.
// Resources/values/theme.xml:2: error APT0000: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.AppCompat'.
// res/drawable/foo-bar.jpg: Invalid file name: must contain only [a-z0-9_.]
// Look for them and convert them to MSBuild compatible errors.
static Regex androidErrorRegex;
internal static Regex AndroidErrorRegex {
get {
if (androidErrorRegex == null)
androidErrorRegex = new Regex (@"^(?<file>.+?)(([:(](?<line>\d+)[:)]):?\s*(error)\s*(?<level>\w*(?=:))):?(?<message>.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
androidErrorRegex = new Regex (@"^(?<file>.+?)([:(](?<line>\d+)[:)])?:+\s*((error)\s*(?<level>\w*(?=:)):?)?(?<message>.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);
return androidErrorRegex;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,23 @@ public void CheckAaptErrorRaisedForInvalidDirectoryName ()
}
}

[Test]
public void CheckAaptErrorRaisedForInvalidFileName ()
{
var proj = new XamarinAndroidApplicationProject ();
proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\drawable\\icon-2.png") {
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_hdpi,
});
var projectPath = string.Format ("temp/CheckAaptErrorRaisedForInvalidDirectoryName");
using (var b = CreateApkBuilder (Path.Combine (projectPath, "UnamedApp"), false, false)) {
b.Verbosity = LoggerVerbosity.Diagnostic;
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed");
StringAssert.Contains ("Invalid file name:", b.LastBuildOutput);
StringAssert.Contains ("1 Error(s)", b.LastBuildOutput);
}
}

[Test]
public void CheckAaptErrorRaisedForDuplicateResourceinApp ()
{
Expand All @@ -487,7 +504,7 @@ public void CheckAaptErrorRaisedForDuplicateResourceinApp ()
b.ThrowOnBuildFailure = false;
Assert.IsFalse (b.Build (proj), "Build should have failed");
StringAssert.Contains ("APT0000: ", b.LastBuildOutput);
StringAssert.Contains ("1 Error(s)", b.LastBuildOutput);
StringAssert.Contains ("2 Error(s)", b.LastBuildOutput);
}
}

Expand Down

0 comments on commit 7dadd00

Please sign in to comment.