AAPT2: Add library support

Change-Id: I307f56d9631784ab29ee4156d94886f9b2f25b30
diff --git a/tools/aapt2/StringPool.cpp b/tools/aapt2/StringPool.cpp
index b159a00..b983a53 100644
--- a/tools/aapt2/StringPool.cpp
+++ b/tools/aapt2/StringPool.cpp
@@ -175,6 +175,25 @@
     return StyleRef(styleEntry);
 }
 
+StringPool::StyleRef StringPool::makeRef(const StyleRef& ref) {
+    Entry* entry = new Entry();
+    entry->value = *ref.mEntry->str;
+    entry->context = ref.mEntry->str.mEntry->context;
+    entry->index = mStrings.size();
+    entry->ref = 0;
+    mStrings.emplace_back(entry);
+    mIndexedStrings.insert(std::make_pair(StringPiece16(entry->value), entry));
+
+    StyleEntry* styleEntry = new StyleEntry();
+    styleEntry->str = Ref(entry);
+    for (const Span& span : ref.mEntry->spans) {
+        styleEntry->spans.emplace_back(Span{ makeRef(*span.name), span.firstChar, span.lastChar });
+    }
+    styleEntry->ref = 0;
+    mStyles.emplace_back(styleEntry);
+    return StyleRef(styleEntry);
+}
+
 void StringPool::merge(StringPool&& pool) {
     mIndexedStrings.insert(pool.mIndexedStrings.begin(), pool.mIndexedStrings.end());
     pool.mIndexedStrings.clear();
@@ -266,7 +285,7 @@
     header->stringCount = pool.size();
     header->flags |= android::ResStringPool_header::UTF8_FLAG;
 
-    uint32_t* indices = out->nextBlock<uint32_t>(pool.size());
+    uint32_t* indices = pool.size() != 0 ? out->nextBlock<uint32_t>(pool.size()) : nullptr;
 
     uint32_t* styleIndices = nullptr;
     if (!pool.mStyles.empty()) {