AAPT2: Insert <uses-sdk> element before <application>

PackageParser on the device uses the targetSdkVersion of the
app while it parses <application>. That means that if the
<uses-sdk> tag comes after <application>, the targetSdkVersion
is assumed to be 0.

Test: make libaapt2_tests
Change-Id: I60f2179a7ff44e7419217afb53f3d24f8c030f6e
diff --git a/tools/aapt2/xml/XmlDom.cpp b/tools/aapt2/xml/XmlDom.cpp
index 567418e..960d361 100644
--- a/tools/aapt2/xml/XmlDom.cpp
+++ b/tools/aapt2/xml/XmlDom.cpp
@@ -66,7 +66,7 @@
 
   Node* this_node = node.get();
   if (!stack->node_stack.empty()) {
-    stack->node_stack.top()->AddChild(std::move(node));
+    stack->node_stack.top()->AppendChild(std::move(node));
   } else {
     stack->root = std::move(node);
   }
@@ -325,7 +325,7 @@
         root = std::move(new_node);
       } else {
         CHECK(!node_stack.empty()) << "node stack should not be empty";
-        node_stack.top()->AddChild(std::move(new_node));
+        node_stack.top()->AppendChild(std::move(new_node));
       }
 
       if (!NodeCast<Text>(this_node)) {
@@ -346,7 +346,7 @@
 
   ns->children.reserve(children.size());
   for (const std::unique_ptr<xml::Node>& child : children) {
-    ns->AddChild(child->Clone());
+    ns->AppendChild(child->Clone());
   }
   return std::move(ns);
 }
@@ -372,11 +372,16 @@
   return el;
 }
 
-void Node::AddChild(std::unique_ptr<Node> child) {
+void Node::AppendChild(std::unique_ptr<Node> child) {
   child->parent = this;
   children.push_back(std::move(child));
 }
 
+void Node::InsertChild(size_t index, std::unique_ptr<Node> child) {
+  child->parent = this;
+  children.insert(children.begin() + index, std::move(child));
+}
+
 Attribute* Element::FindAttribute(const StringPiece& ns,
                                   const StringPiece& name) {
   for (auto& attr : attributes) {
@@ -456,7 +461,7 @@
 
   el->children.reserve(children.size());
   for (const std::unique_ptr<xml::Node>& child : children) {
-    el->AddChild(child->Clone());
+    el->AppendChild(child->Clone());
   }
   return std::move(el);
 }