AAPT2: Support static lib referencing static lib

When a static library A references static library B,
and app C references both A and B, we get the following symbol merging,
symbols from library B get imported twice.

We must only check that symbol references to library B are valid
when building library A. We should only merge all the symbols
when building final app C.

Change-Id: I23cba33b0901dcbb5328d9c9dfaa6a979c073c36
diff --git a/tools/aapt2/TableFlattener.cpp b/tools/aapt2/TableFlattener.cpp
index aa0f1d5..539c48f 100644
--- a/tools/aapt2/TableFlattener.cpp
+++ b/tools/aapt2/TableFlattener.cpp
@@ -79,6 +79,7 @@
 
         // Write the key.
         if (!Res_INTERNALID(key.id.id) && !key.id.isValid()) {
+            assert(key.name.isValid());
             mSymbols->push_back(std::make_pair(ResourceNameRef(key.name),
                     mOut->size() - sizeof(*outMapEntry)));
         }
@@ -96,6 +97,23 @@
         outMapEntry->value.size = sizeof(outMapEntry->value);
     }
 
+    void flattenValueOnly(const Item& value) {
+        mMap->count++;
+
+        android::ResTable_map* outMapEntry = mOut->nextBlock<android::ResTable_map>();
+
+        // Write the value.
+        value.flatten(outMapEntry->value);
+
+        if (outMapEntry->value.data == 0x0) {
+            visitFunc<Reference>(value, [&](const Reference& reference) {
+                mSymbols->push_back(std::make_pair(ResourceNameRef(reference.name),
+                        mOut->size() - sizeof(outMapEntry->value.data)));
+            });
+        }
+        outMapEntry->value.size = sizeof(outMapEntry->value);
+    }
+
     static bool compareStyleEntries(const Style::Entry* lhs, const Style::Entry* rhs) {
         return lhs->key.id < rhs->key.id;
     }
@@ -139,7 +157,7 @@
 
     void visit(const Array& array, ValueVisitorArgs&) override {
         for (const auto& item : array.items) {
-            flattenEntry({}, *item);
+            flattenValueOnly(*item);
         }
     }
 
@@ -334,6 +352,10 @@
         spec->id = type->typeId;
         spec->entryCount = type->entries.size();
 
+        if (type->entries.empty()) {
+            continue;
+        }
+
         // Reserve space for the masks of each resource in this type. These
         // show for which configuration axis the resource changes.
         uint32_t* configMasks = typeBlock.nextBlock<uint32_t>(type->entries.size());