AAPT2: Remove the need for specifying package name in compile phase

The compile phase doesn't use the AndroidManifest, so we had to specify the
package name on the command line.

We can omit the package name, since we don't resolve external references
in the compile phase. Packages that reference the current package will be encoded
with no package name. When loaded by the link phase, the package name will be supplied
and all the references with no package name will use that one.

Change-Id: I9fe4902b747b06899b45c968f30ba1aa05c5cd69
diff --git a/tools/aapt2/BinaryResourceParser.cpp b/tools/aapt2/BinaryResourceParser.cpp
index 3559f43..4f1947a 100644
--- a/tools/aapt2/BinaryResourceParser.cpp
+++ b/tools/aapt2/BinaryResourceParser.cpp
@@ -116,9 +116,11 @@
 BinaryResourceParser::BinaryResourceParser(const std::shared_ptr<ResourceTable>& table,
                                            const std::shared_ptr<IResolver>& resolver,
                                            const Source& source,
+                                           const std::u16string& defaultPackage,
                                            const void* data,
                                            size_t len) :
-        mTable(table), mResolver(resolver), mSource(source), mData(data), mDataLen(len) {
+        mTable(table), mResolver(resolver), mSource(source), mDefaultPackage(defaultPackage),
+        mData(data), mDataLen(len) {
 }
 
 bool BinaryResourceParser::parse() {
@@ -177,6 +179,9 @@
             if (!type) {
                 return false;
             }
+            if (outSymbol->package.empty()) {
+                outSymbol->package = mTable->getPackage();
+            }
             outSymbol->type = *type;
 
             // Since we scan the symbol table in order, we can start looking for the
@@ -350,7 +355,22 @@
 
     size_t len = strnlen16(reinterpret_cast<const char16_t*>(packageHeader->name),
             sizeof(packageHeader->name) / sizeof(packageHeader->name[0]));
-    mTable->setPackage(StringPiece16(reinterpret_cast<const char16_t*>(packageHeader->name), len));
+    if (mTable->getPackage().empty() && len == 0) {
+        mTable->setPackage(mDefaultPackage);
+    } else if (len > 0) {
+        StringPiece16 thisPackage(reinterpret_cast<const char16_t*>(packageHeader->name), len);
+        if (mTable->getPackage().empty()) {
+            mTable->setPackage(thisPackage);
+        } else if (thisPackage != mTable->getPackage()) {
+            Logger::error(mSource)
+                    << "incompatible packages: "
+                    << mTable->getPackage()
+                    << " vs. "
+                    << thisPackage
+                    << std::endl;
+            return false;
+        }
+    }
 
     ResChunkPullParser parser(getChunkData(packageHeader->header),
                               getChunkDataLen(packageHeader->header));