AAPT2: Add a APK filtering.

Allow resource files to be removed from the final artifact based on the
density and locale configuration in the config file. The APK is split
along the density, locale and ABI axis. Each split is generated from the
original APK without modifying the original. The new resource table is
written back to the file system with unneeded assets etc removed.

Test: Unit tests
Test: Manually run optimize command against an APK and inspect results
Test: Installed split searchlite APK (after resigning) and ran on N6

Change-Id: If73597dcfd88c02d2616518585d0e25a5c6a84d1
diff --git a/tools/aapt2/LoadedApk.cpp b/tools/aapt2/LoadedApk.cpp
index abc0e4c..b80780e 100644
--- a/tools/aapt2/LoadedApk.cpp
+++ b/tools/aapt2/LoadedApk.cpp
@@ -58,14 +58,15 @@
 bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
                                IArchiveWriter* writer) {
   FilterChain empty;
-  return WriteToArchive(context, options, &empty, writer);
+  return WriteToArchive(context, table_.get(), options, &empty, writer);
 }
 
-bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
-                               FilterChain* filters, IArchiveWriter* writer) {
+bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table,
+                               const TableFlattenerOptions& options, FilterChain* filters,
+                               IArchiveWriter* writer) {
   std::set<std::string> referenced_resources;
   // List the files being referenced in the resource table.
-  for (auto& pkg : table_->packages) {
+  for (auto& pkg : split_table->packages) {
     for (auto& type : pkg->types) {
       for (auto& entry : type->entries) {
         for (auto& config_value : entry->values) {
@@ -108,7 +109,7 @@
       // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
       // with sparse entries) b/35389232.
       TableFlattener flattener(options, &buffer);
-      if (!flattener.Consume(context, table_.get())) {
+      if (!flattener.Consume(context, split_table)) {
         return false;
       }