Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Parse R.txt int styleable IDs as base-t…
Browse files Browse the repository at this point in the history
…en (#3396)

Fixes: #3395

For some reason, `aapt` and `aapt2` generate base-ten numbers instead of
hexadecimal numbers for the `int styleable` IDs in the `R.txt` file.
All the other IDs are hexadecimal.

Correct the managed resource parser to read the `int styleable` IDs as
base-ten numbers instead of base-sixteen numbers.  Adjust the generated
comment text for `int styleable` IDs accordingly too.

Add a test for this scenario, and update the expected outputs for the
existing tests to account for the adjusted comment text.
  • Loading branch information
brendanzagaeski authored and jonathanpeppers committed Aug 2, 2019
1 parent 1bb9b02 commit 7c5d475
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ public partial class Styleable
16842962,
2130903040};

// aapt resource value: 0x0
// aapt resource value: 0
public const int CustomFonts_android_scrollX = 0;

// aapt resource value: 0x1
// aapt resource value: 1
public const int CustomFonts_customFont = 1;

// aapt resource value: { 0x10100B2,0x10101F8,0x7F030001,0x7F030002 }
Expand All @@ -283,16 +283,16 @@ public partial class Styleable
2130903041,
2130903042};

// aapt resource value: 0x0
// aapt resource value: 0
public const int MultiSelectListPreference_android_entries = 0;

// aapt resource value: 0x1
// aapt resource value: 1
public const int MultiSelectListPreference_android_entryValues = 1;

// aapt resource value: 0x2
// aapt resource value: 2
public const int MultiSelectListPreference_entries = 2;

// aapt resource value: 0x3
// aapt resource value: 3
public const int MultiSelectListPreference_entryValues = 3;

static Styleable()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: global::Android.Runtime.ResourceDesignerAttribute("Foo.Foo.Resource", IsApplication=true)]

namespace Foo.Foo
{


[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
public partial class Resource
{

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

public partial class Attribute
{

// aapt resource value: 0x7F010000
public const int attr00 = 2130771968;

// aapt resource value: 0x7F010001
public const int attr01 = 2130771969;

// aapt resource value: 0x7F010002
public const int attr02 = 2130771970;

// aapt resource value: 0x7F010003
public const int attr03 = 2130771971;

// aapt resource value: 0x7F010004
public const int attr04 = 2130771972;

// aapt resource value: 0x7F010005
public const int attr05 = 2130771973;

// aapt resource value: 0x7F010006
public const int attr06 = 2130771974;

// aapt resource value: 0x7F010007
public const int attr07 = 2130771975;

// aapt resource value: 0x7F010008
public const int attr08 = 2130771976;

// aapt resource value: 0x7F010009
public const int attr09 = 2130771977;

// aapt resource value: 0x7F01000A
public const int attr10 = 2130771978;

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

private Attribute()
{
}
}

public partial class Styleable
{

// aapt resource value: { 0x7F010000,0x7F010001,0x7F010002,0x7F010003,0x7F010004,0x7F010005,0x7F010006,0x7F010007,0x7F010008,0x7F010009,0x7F01000A }
public static int[] ElevenAttributes = new int[] {
2130771968,
2130771969,
2130771970,
2130771971,
2130771972,
2130771973,
2130771974,
2130771975,
2130771976,
2130771977,
2130771978};

// aapt resource value: 0
public const int ElevenAttributes_attr00 = 0;

// aapt resource value: 1
public const int ElevenAttributes_attr01 = 1;

// aapt resource value: 2
public const int ElevenAttributes_attr02 = 2;

// aapt resource value: 3
public const int ElevenAttributes_attr03 = 3;

// aapt resource value: 4
public const int ElevenAttributes_attr04 = 4;

// aapt resource value: 5
public const int ElevenAttributes_attr05 = 5;

// aapt resource value: 6
public const int ElevenAttributes_attr06 = 6;

// aapt resource value: 7
public const int ElevenAttributes_attr07 = 7;

// aapt resource value: 8
public const int ElevenAttributes_attr08 = 8;

// aapt resource value: 9
public const int ElevenAttributes_attr09 = 9;

// aapt resource value: 10
public const int ElevenAttributes_attr10 = 10;

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

private Styleable()
{
}
}
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ public partial class Styleable
16842962,
2130903040};

// aapt resource value: 0x0
// aapt resource value: 0
public const int CustomFonts_android_scrollX = 0;

// aapt resource value: 0x1
// aapt resource value: 1
public const int CustomFonts_customFont = 1;

// aapt resource value: { 0x10100B2,0x10101F8,0x7F030001,0x7F030002 }
Expand All @@ -297,16 +297,16 @@ public partial class Styleable
2130903041,
2130903042};

// aapt resource value: 0x0
// aapt resource value: 0
public const int MultiSelectListPreference_android_entries = 0;

// aapt resource value: 0x1
// aapt resource value: 1
public const int MultiSelectListPreference_android_entryValues = 1;

// aapt resource value: 0x2
// aapt resource value: 2
public const int MultiSelectListPreference_entries = 2;

// aapt resource value: 0x3
// aapt resource value: 3
public const int MultiSelectListPreference_entryValues = 3;

static Styleable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,5 +338,75 @@ public void UpdateLayoutIdIsIncludedInDesigner ()
$"{task.NetResgenOutputFile} and {expectedWithNewId} do not match.");
Directory.Delete (Path.Combine (Root, path), recursive: true);
}

[Test]
public void GenerateDesignerFileWithElevenStyleableAttributesFromRtxt ()
{
var styleable = @"<resources>
<declare-styleable name = ""ElevenAttributes"">
<attr name = ""attr00"" format=""string"" />
<attr name = ""attr01"" format=""string"" />
<attr name = ""attr02"" format=""string"" />
<attr name = ""attr03"" format=""string"" />
<attr name = ""attr04"" format=""string"" />
<attr name = ""attr05"" format=""string"" />
<attr name = ""attr06"" format=""string"" />
<attr name = ""attr07"" format=""string"" />
<attr name = ""attr08"" format=""string"" />
<attr name = ""attr09"" format=""string"" />
<attr name = ""attr10"" format=""string"" />
</declare-styleable>
</resources>";
var rtxt = @"int attr attr00 0x7f010000
int attr attr01 0x7f010001
int attr attr02 0x7f010002
int attr attr03 0x7f010003
int attr attr04 0x7f010004
int attr attr05 0x7f010005
int attr attr06 0x7f010006
int attr attr07 0x7f010007
int attr attr08 0x7f010008
int attr attr09 0x7f010009
int attr attr10 0x7f01000a
int[] styleable ElevenAttributes { 0x7f010000, 0x7f010001, 0x7f010002, 0x7f010003, 0x7f010004, 0x7f010005, 0x7f010006, 0x7f010007, 0x7f010008, 0x7f010009, 0x7f01000a }
int styleable ElevenAttributes_attr00 0
int styleable ElevenAttributes_attr01 1
int styleable ElevenAttributes_attr02 2
int styleable ElevenAttributes_attr03 3
int styleable ElevenAttributes_attr04 4
int styleable ElevenAttributes_attr05 5
int styleable ElevenAttributes_attr06 6
int styleable ElevenAttributes_attr07 7
int styleable ElevenAttributes_attr08 8
int styleable ElevenAttributes_attr09 9
int styleable ElevenAttributes_attr10 10";

var path = Path.Combine ("temp", TestName);
Directory.CreateDirectory (Path.Combine (Root, path));
File.WriteAllText (Path.Combine (Root, path, "AndroidManifest.xml"), AndroidManifest);
Directory.CreateDirectory (Path.Combine (Root, path, "res"));
Directory.CreateDirectory (Path.Combine (Root, path, "res", "values"));
File.WriteAllText (Path.Combine (Root, path, "res", "values", "attrs.xml"), styleable);
File.WriteAllText (Path.Combine (Root, path, "R.txt"), rtxt);
IBuildEngine engine = new MockBuildEngine (TestContext.Out);
var task = new GenerateResourceDesigner {
BuildEngine = engine
};
task.UseManagedResourceGenerator = true;
task.DesignTimeBuild = true;
task.Namespace = "Foo.Foo";
task.NetResgenOutputFile = Path.Combine (Root, path, "Resource.designer.cs");
task.ProjectDir = Path.Combine (Root, path);
task.ResourceDirectory = Path.Combine (Root, path, "res");
task.Resources = new TaskItem [] {};
task.IsApplication = true;
task.JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", "android-27", "android.jar");
Assert.IsTrue (task.Execute (), "Task should have executed successfully.");
Assert.IsTrue (File.Exists (task.NetResgenOutputFile), $"{task.NetResgenOutputFile} should have been created.");
var expected = Path.Combine (Root, "Expected", "GenerateDesignerFileWithElevenStyleableAttributesExpected.cs");
Assert.IsTrue (FileCompare (task.NetResgenOutputFile, expected),
$"{task.NetResgenOutputFile} and {expected} do not match.");
Directory.Delete (Path.Combine (Root, path), recursive: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
<Content Include="Expected\GenerateDesignerFileExpected.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\GenerateDesignerFileWithElevenStyleableAttributesExpected.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\GenerateDesignerFileWithLibraryReferenceExpected.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void ProcessRtxtFile (string file)
var lines = System.IO.File.ReadLines (file);
foreach (var line in lines) {
var items = line.Split (new char [] { ' ' }, 4);
int value = items [0] != "int[]" ? Convert.ToInt32 (items [3], 16) : -1;
int value = items [1] != "styleable" ? Convert.ToInt32 (items [3], 16) : -1;
string itemName = items [2];
switch (items [1]) {
case "anim":
Expand Down Expand Up @@ -316,7 +316,7 @@ void ProcessRtxtFile (string file)
case "styleable":
switch (items [0]) {
case "int":
CreateIntField (styleable, itemName, value);
CreateIntField (styleable, itemName, Convert.ToInt32 (items [3], 10));
break;
case "int[]":
var arrayValues = items [3].Trim (new char [] { '{', '}' })
Expand Down Expand Up @@ -414,7 +414,8 @@ CodeMemberField CreateIntField (CodeTypeDeclaration parentType, string name, int
};
if (value != -1) {
f.InitExpression = new CodePrimitiveExpression (value);
f.Comments.Add (new CodeCommentStatement ($"aapt resource value: 0x{value.ToString ("X")}"));
string valueName = parentType.Name == "Styleable" ? value.ToString () : $"0x{value.ToString ("X")}";
f.Comments.Add (new CodeCommentStatement ($"aapt resource value: {valueName}"));
}
parentType.Members.Add (f);
return f;
Expand Down

0 comments on commit 7c5d475

Please sign in to comment.