AAPT2: Automatic Static Library Namespacing.

Introduces a link flag --auto-namespace-static-lib for use when linking
static libraries.

When linking a static library with compiled sources that have references
to resources in provided libraries without an explicit package name,
the flag enables automatic inference of the package.

If a resource is present in the package that is being compiled, that is
used, otherwise the reference is rewritten to the highest precedence
resource with matching name and type.

Test: m out/host/linux-x86/nativetest64/aapt2_tests/aapt2_tests && \
      $ANDROID_HOST_OUT/nativetest64/aapt2_tests/aapt2_tests
Test: m frameworks/base/tools/aapt2/integration-tests
Change-Id: I6c6017e054654d1f60782d0a428a7a2a47f8952b
diff --git a/tools/aapt2/test/Context.h b/tools/aapt2/test/Context.h
index 0564db0..a07d79f 100644
--- a/tools/aapt2/test/Context.h
+++ b/tools/aapt2/test/Context.h
@@ -81,6 +81,10 @@
     return min_sdk_version_;
   }
 
+  bool IsAutoNamespace() override {
+    return auto_namespace_;
+  }
+
  private:
   DISALLOW_COPY_AND_ASSIGN(Context);
 
@@ -93,6 +97,7 @@
   NameMangler name_mangler_;
   SymbolTable symbols_;
   int min_sdk_version_;
+  bool auto_namespace_;
 };
 
 class ContextBuilder {
@@ -127,6 +132,11 @@
     return *this;
   }
 
+  ContextBuilder& SetAutoNamespace(bool auto_namespace) {
+    context_->auto_namespace_ = auto_namespace;
+    return *this;
+  }
+
   std::unique_ptr<Context> Build() { return std::move(context_); }
 
  private:
@@ -172,6 +182,15 @@
       return nullptr;
     }
 
+    std::string GetPackageForSymbol(const ResourceName& name) override {
+      for (auto const& imap : name_map_) {
+        if (imap.first.type == name.type && imap.first.entry == name.entry) {
+          return imap.first.package;
+        }
+      }
+      return "";
+    }
+
     std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override {
       auto iter = id_map_.find(id);
       if (iter != id_map_.end()) {