AAPT2: Introduce notion of 'product' to ResourceTable

This allows us to preserve the various product definitions during the compile
phase, and allows us to select the product in the link phase.

This allows compiled files to remain product-independent, so that they do not need
to be recompiled when switching targets.

Bug:25958912
Change-Id: Iaa7eed25c834b67a39cdc9be43613e8b5ab6cdd7
diff --git a/tools/aapt2/link/Link.cpp b/tools/aapt2/link/Link.cpp
index 8e32179..3437ac0 100644
--- a/tools/aapt2/link/Link.cpp
+++ b/tools/aapt2/link/Link.cpp
@@ -31,6 +31,7 @@
 #include "java/ManifestClassGenerator.h"
 #include "java/ProguardRules.h"
 #include "link/Linkers.h"
+#include "link/ProductFilter.h"
 #include "link/ReferenceLinker.h"
 #include "link/ManifestFixer.h"
 #include "link/TableMerger.h"
@@ -70,6 +71,7 @@
     Maybe<std::u16string> privateSymbols;
     ManifestFixerOptions manifestFixerOptions;
     IConfigFilter* configFilter = nullptr;
+    std::unordered_set<std::string> products;
 };
 
 struct LinkContext : public IAaptContext {
@@ -292,16 +294,16 @@
                         for (const auto& configValue : entry->values) {
                             // Special case the occurrence of an ID that is being generated for the
                             // 'android' package. This is due to legacy reasons.
-                            if (valueCast<Id>(configValue.value.get()) &&
+                            if (valueCast<Id>(configValue->value.get()) &&
                                     package->name == u"android") {
                                 mContext->getDiagnostics()->warn(
-                                        DiagMessage(configValue.value->getSource())
+                                        DiagMessage(configValue->value->getSource())
                                         << "generated id '" << resName
                                         << "' for external package '" << package->name
                                         << "'");
                             } else {
                                 mContext->getDiagnostics()->error(
-                                        DiagMessage(configValue.value->getSource())
+                                        DiagMessage(configValue->value->getSource())
                                         << "defined resource '" << resName
                                         << "' for external package '" << package->name
                                         << "'");
@@ -512,7 +514,10 @@
 
             std::unique_ptr<Id> id = util::make_unique<Id>();
             id->setSource(fileDesc->source.withLine(exportedSymbol.line));
-            bool result = mFinalTable.addResourceAllowMangled(resName, {}, std::move(id),
+            bool result = mFinalTable.addResourceAllowMangled(resName,
+                                                              ConfigDescription::defaultConfig(),
+                                                              std::string(),
+                                                              std::move(id),
                                                               mContext->getDiagnostics());
             if (!result) {
                 return false;
@@ -681,6 +686,12 @@
                 mContext->getDiagnostics()->error(DiagMessage() << "failed linking references");
                 return 1;
             }
+
+            ProductFilter productFilter(mOptions.products);
+            if (!productFilter.consume(mContext, &mFinalTable)) {
+                mContext->getDiagnostics()->error(DiagMessage() << "failed stripping products");
+                return 1;
+            }
         }
 
         proguard::KeepSet proguardKeepSet;
@@ -931,6 +942,7 @@
     Maybe<std::string> customJavaPackage;
     std::vector<std::string> extraJavaPackages;
     Maybe<std::string> configs;
+    Maybe<std::string> productList;
     bool legacyXFlag = false;
     bool requireLocalization = false;
     Flags flags = Flags()
@@ -954,6 +966,8 @@
                             &requireLocalization)
             .optionalFlag("-c", "Comma separated list of configurations to include. The default\n"
                                 "is all configurations", &configs)
+            .optionalFlag("--product", "Comma separated list of product names to keep",
+                          &productList)
             .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified "
                             "by -o",
                             &options.outputToDirectory)
@@ -1039,6 +1053,14 @@
         }
     }
 
+    if (productList) {
+        for (StringPiece product : util::tokenize<char>(productList.value(), ',')) {
+            if (product != "" && product != "default") {
+                options.products.insert(product.toString());
+            }
+        }
+    }
+
     AxisConfigFilter filter;
     if (configs) {
         for (const StringPiece& configStr : util::tokenize<char>(configs.value(), ',')) {