Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Design-Time Build <dimen/> support (#1786)
Browse files Browse the repository at this point in the history
Fixes: https://devdiv.visualstudio.com/0bdbc590-a062-4c3f-b0f6-9383f67865ee/_workitems/edit/628467
Fixes: #1784

We were not correctly processing `<dimen/>` resource items when
parsing the actual resources, via the Design-Time Build and
ManagedResourceParser.  We did however process `dimen` resources
correctly when processing the `R.txt` or `R.java` files, so *full*
builds worked as expected.

Update the `ManagedResourceParser` to handle `<dimen/>` resources.
  • Loading branch information
dellis1972 committed Jun 18, 2018
1 parent c743376 commit 6d7d8a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1125,13 +1125,20 @@ public void BuildAppWithManagedResourceParserAndLibraries ()
<resources>
<color name=""theme_devicedefault_background"">#ffffffff</color>
<color name=""SomeColor"">#ffffffff</color>
</resources>",
};
var dimen = new AndroidItem.AndroidResource ("Resources\\values\\dimen.xml") {
TextContent = () => @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<dimen name=""main_text_item_size"">17dp</dimen>
</resources>",
};
var libProj = new XamarinAndroidLibraryProject () {
IsRelease = true,
ProjectName = "Lib1",
AndroidResources = {
theme,
dimen,
},
};
libProj.SetProperty ("AndroidUseManagedDesignTimeResourceGenerator", "True");
Expand Down Expand Up @@ -1178,6 +1185,7 @@ public void BuildAppWithManagedResourceParserAndLibraries ()
StringAssert.Contains ("Icon", designerContents, $"{designerFile} should contain Resources.Drawable.Icon");
StringAssert.Contains ("Main", designerContents, $"{designerFile} should contain Resources.Layout.Main");
StringAssert.Contains ("material_grey_50", designerContents, $"{designerFile} should contain Resources.Color.material_grey_50");
StringAssert.DoesNotContain ("main_text_item_size", designerContents, $"{designerFile} should not contain Resources.Dimension.main_text_item_size");
StringAssert.DoesNotContain ("theme_devicedefault_background", designerContents, $"{designerFile} should not contain Resources.Color.theme_devicedefault_background");
libBuilder.Target = "Build";
Assert.IsTrue (libBuilder.Build (libProj), "Library project should have built");
Expand All @@ -1197,7 +1205,9 @@ public void BuildAppWithManagedResourceParserAndLibraries ()
StringAssert.Contains ("Icon", designerContents, $"{designerFile} should contain Resources.Drawable.Icon");
StringAssert.Contains ("Main", designerContents, $"{designerFile} should contain Resources.Layout.Main");
StringAssert.Contains ("material_grey_50", designerContents, $"{designerFile} should contain Resources.Color.material_grey_50");
StringAssert.Contains ("main_text_item_size", designerContents, $"{designerFile} should contain Resources.Dimension.main_text_item_size");
StringAssert.Contains ("theme_devicedefault_background", designerContents, $"{designerFile} should contain Resources.Color.theme_devicedefault_background");
StringAssert.Contains ("main_text_item_size", designerContents, $"{designerFile} should contain Resources.Dimension.main_text_item_size");
StringAssert.Contains ("SomeColor", designerContents, $"{designerFile} should contain Resources.Color.SomeColor");

appBuilder.Target = "SignAndroidPackage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ private Animator()
}
}

public partial class Dimension
{

// aapt resource value: 0x7F090002
public const int main_text_item_size = 2131296258;

static Dimension()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}

private Dimension()
{
}
}

public partial class Drawable
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class ManagedResourceParserTests : BaseTest {
android:valueTo=""0""
android:valueType=""floatType"" />";

const string Dimen = @"<?xml version=""1.0"" encoding=""utf-8""?>
<resources>
<dimen name=""main_text_item_size"">17dp</dimen>
</resources>";

[Test]
public void GenerateDesignerFile ()
{
Expand All @@ -66,6 +71,7 @@ public void GenerateDesignerFile ()
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "animator", "slide_in_bottom.xml"), Animator);
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "font", "arial.ttf"), "");
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "values", "strings.xml"), StringsXml2);
File.WriteAllText (Path.Combine (Root, path, "lp", "res", "values", "dimen.xml"), Dimen);
using (var stream = typeof (XamarinAndroidCommonProject).Assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Base.Icon.png")) {
var icon_binary_mdpi = new byte [stream.Length];
stream.Read (icon_binary_mdpi, 0, (int)stream.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ void CreateResourceField (string root, string fieldName, XmlReader element = nul
CreateIntField (drawable, fieldName);
break;
case "dimen":
CreateIntField (dimension, fieldName);
break;
case "font":
CreateIntField (font, fieldName);
break;
Expand Down

0 comments on commit 6d7d8a4

Please sign in to comment.