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/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp
index 69bac04..163a526 100644
--- a/tools/aapt2/cmd/Link.cpp
+++ b/tools/aapt2/cmd/Link.cpp
@@ -111,6 +111,7 @@
 
   // Static lib options.
   bool no_static_lib_packages = false;
+  bool auto_namespace_static_lib = false;
 
   // AndroidManifest.xml massaging options.
   ManifestFixerOptions manifest_fixer_options;
@@ -193,6 +194,14 @@
     min_sdk_version_ = minSdk;
   }
 
+  bool IsAutoNamespace() override {
+    return auto_namespace_;
+  }
+
+  void SetAutoNamespace(bool val) {
+    auto_namespace_ = val;
+  }
+
  private:
   DISALLOW_COPY_AND_ASSIGN(LinkContext);
 
@@ -204,6 +213,7 @@
   SymbolTable symbols_;
   bool verbose_ = false;
   int min_sdk_version_ = 0;
+  bool auto_namespace_ = false;
 };
 
 // A custom delegate that generates compatible pre-O IDs for use with feature splits.
@@ -2112,6 +2122,10 @@
           .OptionalSwitch("--no-static-lib-packages",
                           "Merge all library resources under the app's package.",
                           &options.no_static_lib_packages)
+          .OptionalSwitch("--auto-namespace-static-lib",
+                          "Automatically namespace resource references when building a static\n"
+                          "library.",
+                          &options.auto_namespace_static_lib)
           .OptionalSwitch("--non-final-ids",
                           "Generates R.java without the final modifier. This is implied when\n"
                           "--static-lib is specified.",
@@ -2152,7 +2166,7 @@
           .OptionalFlagList("-0", "File extensions not to compress.",
                             &options.extensions_to_not_compress)
           .OptionalSwitch("--no-compress", "Do not compress any resources.",
-                            &options.do_not_compress_anything)
+                          &options.do_not_compress_anything)
           .OptionalSwitch("--warn-manifest-validation",
                           "Treat manifest validation errors as warnings.",
                           &options.manifest_fixer_options.warn_validation)
@@ -2221,6 +2235,15 @@
     options.output_format = OutputFormat::kProto;
   }
 
+  if (options.auto_namespace_static_lib) {
+    if (!static_lib) {
+      context.GetDiagnostics()->Error(
+          DiagMessage() << "--auto-namespace-static-lib can only be used with --static-lib");
+      return 1;
+    }
+    context.SetAutoNamespace(true);
+  }
+
   if (package_id) {
     if (context.GetPackageType() != PackageType::kApp) {
       context.GetDiagnostics()->Error(