forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Xamarin.Android.Build.Tasks] Emit an Error if a custom view cannot b…
…e fixed up. Context dotnet#1711 When using a custom view within a layout file we replace the `namespace.classname` with an `md5hash.classname`. We do this by using the `acwmap.txt` file to make known types onto the `md5` hashed ones. We do this in a case sensitive manner. We also only support Camel case and lower case type names. ClassLibrary1.CustomView=md5d6f7135293df7527c983d45d07471c5e.CustomTextView classlibrary1.CustomView=md5d6f7135293df7527c983d45d07471c5e.CustomTextView Given that a user is able to type these manually it is highly probable that typo's will occur. If for example a user types Classlibrary1.CustomView this will NOT be fixed up. Instead the user will recieve the following error at runtime. FATAL UNHANDLED EXCEPTION: Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class Classlibrary1.CustomTextView ---> Android.Views.InflateException: Binary XML file line #1: Error inflating class Classlibrary1.CustomTextView ---> Java.Lang.ClassNotFoundException: Didn't find class "Classlibrary1.CustomTextView" if you are using the control in a number of places this runtime error does nothing to help track down the problem. Instead what we should be doing is detecting these issues and emitting a build error. This will provide the user not only the problem but also a link to the file causing the probem. TODO ---- [ ] Fix up the name so it points to the file in `Resources` not `res` [ ] Add a Unit test. [ ] Add Error code and document.
- Loading branch information
1 parent
b4abdd9
commit 5ab03c2
Showing
11 changed files
with
177 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Compiler Warning XA1001 | ||
|
||
AndroidResgen: Warning while updating Resource XML '{filename}': {Message} | ||
|
||
This warning is raised if we encounter an unknown issue when processing | ||
the layout and resources. It is a generic warning that does not describe | ||
any specific problem. The details will be in the `{Message}`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Compiler Error XA1000 | ||
|
||
This error will be emitted when we are unable to find a matching custom new in the | ||
ResourceCaseMap string. | ||
|
||
As part of the build process `Namespace.CustomViewFoo` items in layout files are | ||
replaced with `{MD5Hash}.CustomViewFoo`. We attempt to replace a couple of variants | ||
of the `Namespace.CustomViewFoo`. One is the original casing found in the C# source | ||
files. The other is all lowercase. If we cannot find a match we will do a case | ||
insensitive lookup to see if there are items which do match. If we find one this | ||
error will be raised. | ||
|
||
We found a matching key 'Namespace.CustomViewFoo' for 'NameSpace.CustomViewFoo'. But the casing was incorrect. Please correct the casing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...marin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ConvertResourcesCasesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using Xamarin.ProjectTools; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Build.Framework; | ||
using System.Text; | ||
using Xamarin.Android.Tasks; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Xamarin.Android.Build.Tests { | ||
|
||
[TestFixture] | ||
[Parallelizable (ParallelScope.Self)] | ||
public class ConvertResourcesCasesTests : BaseTest { | ||
[Test] | ||
public void CheckClassIsReplacedWithMd5 () | ||
{ | ||
var path = Path.Combine (Root, "temp", "CheckClassIsReplacedWithMd5"); | ||
Directory.CreateDirectory (path); | ||
var resPath = Path.Combine (path, "res"); | ||
Directory.CreateDirectory (Path.Combine (resPath, "layout")); | ||
File.WriteAllText (Path.Combine (resPath, "layout", "main.xml"), @"<?xml version='1.0' ?> | ||
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<ClassLibrary1.CustomView xmlns:android='http://schemas.android.com/apk/res/android' /> | ||
<classlibrary1.CustomView xmlns:android='http://schemas.android.com/apk/res/android' /> | ||
</LinearLayout> | ||
"); | ||
var errors = new List<BuildErrorEventArgs> (); | ||
IBuildEngine engine = new MockBuildEngine (TestContext.Out, errors); | ||
var task = new ConvertResourcesCases { | ||
BuildEngine = engine | ||
}; | ||
task.ResourceDirectories = new ITaskItem [] { | ||
new TaskItem (resPath), | ||
}; | ||
task.AcwMapFile = Path.Combine (path, "acwmap.txt"); | ||
File.WriteAllLines (task.AcwMapFile, new string [] { | ||
"ClassLibrary1.CustomView;md5d6f7135293df7527c983d45d07471c5e.CustomTextView", | ||
"classlibrary1.CustomView;md5d6f7135293df7527c983d45d07471c5e.CustomTextView", | ||
}); | ||
Assert.IsTrue (task.Execute (), "Task should have executed successfully"); | ||
var output = File.ReadAllText (Path.Combine (resPath, "layout", "main.xml")); | ||
StringAssert.Contains ("md5d6f7135293df7527c983d45d07471c5e.CustomTextView", output, "md5d6f7135293df7527c983d45d07471c5e.CustomTextView should exist in the main.xml"); | ||
StringAssert.DoesNotContain ("ClassLibrary1.CustomView", output, "ClassLibrary1.CustomView should have been replaced."); | ||
StringAssert.DoesNotContain ("classlibrary1.CustomView", output, "classlibrary1.CustomView should have been replaced."); | ||
Directory.Delete (path, recursive: true); | ||
} | ||
|
||
[Test] | ||
public void CheckClassIsNotReplacedWithMd5 () | ||
{ | ||
var path = Path.Combine (Root, "temp", "CheckClassIsNotReplacedWithMd5"); | ||
Directory.CreateDirectory (path); | ||
var resPath = Path.Combine (path, "res"); | ||
Directory.CreateDirectory (Path.Combine (resPath, "layout")); | ||
File.WriteAllText (Path.Combine (resPath, "layout", "main.xml"), @"<?xml version='1.0' ?> | ||
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'> | ||
<ClassLibrary1.CustomView xmlns:android='http://schemas.android.com/apk/res/android' /> | ||
<classLibrary1.CustomView xmlns:android='http://schemas.android.com/apk/res/android' /> | ||
</LinearLayout> | ||
"); | ||
var errors = new List<BuildErrorEventArgs> (); | ||
IBuildEngine engine = new MockBuildEngine (TestContext.Out, errors); | ||
var task = new ConvertResourcesCases { | ||
BuildEngine = engine | ||
}; | ||
task.ResourceDirectories = new ITaskItem [] { | ||
new TaskItem (resPath), | ||
}; | ||
task.AcwMapFile = Path.Combine (path, "acwmap.txt"); | ||
File.WriteAllLines (task.AcwMapFile, new string [] { | ||
"ClassLibrary1.CustomView;md5d6f7135293df7527c983d45d07471c5e.CustomTextView", | ||
"classlibrary1.CustomView;md5d6f7135293df7527c983d45d07471c5e.CustomTextView", | ||
}); | ||
Assert.IsTrue (task.Execute (), "Task should have executed successfully"); | ||
var output = File.ReadAllText (Path.Combine (resPath, "layout", "main.xml")); | ||
StringAssert.Contains ("md5d6f7135293df7527c983d45d07471c5e.CustomTextView", output, "md5d6f7135293df7527c983d45d07471c5e.CustomTextView should exist in the main.xml"); | ||
StringAssert.DoesNotContain ("ClassLibrary1.CustomView", output, "ClassLibrary1.CustomView should have been replaced."); | ||
StringAssert.Contains ("classLibrary1.CustomView", output, "classLibrary1.CustomView should have been replaced."); | ||
Assert.AreEqual (1, errors.Count, "One Error should have been raised."); | ||
Assert.AreEqual ("XA1000", errors [0].Code, "XA1000 should have been raised."); | ||
Directory.Delete (path, recursive: true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters