Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Xamarin.Android.Build.Tasks] Fix Removal of Non Duplicate elements #856

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,56 @@ public void MergeLibraryManifest ()
References = {
new BuildItem.ProjectReference ("..\\Binding1\\Binding1.csproj", lib.ProjectGuid)
},
Packages = {
KnownPackages.SupportMediaCompat_25_4_0_1,
KnownPackages.SupportFragment_25_4_0_1,
KnownPackages.SupportCoreUtils_25_4_0_1,
KnownPackages.SupportCoreUI_25_4_0_1,
KnownPackages.SupportCompat_25_4_0_1,
KnownPackages.AndroidSupportV4_25_4_0_1,
KnownPackages.SupportV7AppCompat_25_4_0_1,
},
};
proj.Sources.Add (new BuildItem.Source ("TestActivity1.cs") {
TextContent = () => @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Android.Util;
[Activity (Label = ""TestActivity1"")]
[IntentFilter (new[]{Intent.ActionMain}, Categories = new[]{ ""com.xamarin.sample"" })]
public class TestActivity1 : FragmentActivity {
}
",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: these lines full of whitespace are weird.

});
proj.Sources.Add (new BuildItem.Source ("TestActivity2.cs") {
TextContent = () => @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Android.Util;
[Activity (Label = ""TestActivity2"")]
[IntentFilter (new[]{Intent.ActionMain}, Categories = new[]{ ""com.xamarin.sample"" })]
public class TestActivity2 : FragmentActivity {
}
",
});
using (var libbuilder = CreateDllBuilder (Path.Combine (path, "Binding1"))) {
Assert.IsTrue (libbuilder.Build (lib), "Build should have succeeded.");
using (var builder = CreateApkBuilder (Path.Combine (path, "App1"))) {
Expand All @@ -558,6 +607,15 @@ public void MergeLibraryManifest ()
".internal.FacebookInitProvider was not replaced with com.xamarin.test.internal.FacebookInitProvider");
Assert.AreEqual (manifest.IndexOf ("meta-data", StringComparison.OrdinalIgnoreCase),
manifest.LastIndexOf ("meta-data", StringComparison.OrdinalIgnoreCase), "There should be only one meta-data element");

var doc = XDocument.Parse (manifest);
var ns = XNamespace.Get ("http://schemas.android.com/apk/res/android");

var activities = doc.Element ("manifest")?.Element ("application")?.Elements ("activity");
var e = activities.FirstOrDefault (x => x.Attribute (ns.GetName ("label"))?.Value == "TestActivity2");
Assert.IsNotNull (e, "Manifest should contain an activity for TestActivity2");
Assert.IsNotNull (e.Element ("intent-filter"), "TestActivity2 should have an intent-filter");
Assert.IsNotNull (e.Element ("intent-filter").Element ("action"), "TestActivity2 should have an intent-filter/action");
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,18 @@ void MergeLibraryManifest (string mergedManifest)
}
}

public IEnumerable<XElement> ResolveDuplicates (IEnumerable<XElement> elements)
{
foreach (var e in elements)
foreach (var d in ResolveDuplicates (e.Elements ()))
yield return d;
foreach (var d in elements.GroupBy (x => x.ToFullString ()).SelectMany (x => x.Skip (1)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this method works. :-(

This part appears to be identical to what's being removed, i.e. this fragment is "buggy" (hence it needing to be fixed in the first place). Presumably then, the above foreach (var e in elements) loop fixes the bug, but I don't understand how these separate loops interact.

Assuming that the foreach (var e in elements) ultimately yield 0 elements, we'd still have this loop, which is known to be buggy. If the foreach (var e in elements) does yield elements, that's just more elements to remove!

How's this work? It's blowing my mind. :-(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous method used Descendants() which gets every single child element not just those that are immediate children. So given

<foo>
   <bar>
     <bug/>
   </bar>
   <dar>
     <bug/>
   </dar>
   <too/>
   <too/>
</foo>

In the previous method both bug elements would be removed because Descendants() gets every element including all children. Elements() gets the elements that are children for this exact parent. So the new system will remove duplicates which exist at the same level and ONLY those that exist at the same level. So in the sample above the bug will be left as they are as they have different parent elements. But the too items will be removed as they have the same parent.

yield return d;
}

void RemoveDuplicateElements ()
{
var duplicates = doc.Descendants ()
.GroupBy (x => x.ToFullString ())
.SelectMany (x => x.Skip (1));
var duplicates = ResolveDuplicates (doc.Elements ());
foreach (var duplicate in duplicates)
duplicate.Remove ();

Expand Down