Move StringPool to libandroidfw

Test: verified affected tests pass
Bug: 232940948
Change-Id: I22089893d7e5013f759c39ce190bec07fa6435db
diff --git a/tools/aapt2/format/Archive.cpp b/tools/aapt2/format/Archive.cpp
index c20b053..80c1618 100644
--- a/tools/aapt2/format/Archive.cpp
+++ b/tools/aapt2/format/Archive.cpp
@@ -25,9 +25,9 @@
 #include "android-base/macros.h"
 #include "android-base/utf8.h"
 #include "androidfw/StringPiece.h"
-#include "ziparchive/zip_writer.h"
-
 #include "util/Files.h"
+#include "util/Util.h"
+#include "ziparchive/zip_writer.h"
 
 using ::android::StringPiece;
 using ::android::base::SystemErrorCodeToString;
@@ -256,21 +256,21 @@
 
 }  // namespace
 
-std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag,
+std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(android::IDiagnostics* diag,
                                                              const StringPiece& path) {
   std::unique_ptr<DirectoryWriter> writer = util::make_unique<DirectoryWriter>();
   if (!writer->Open(path)) {
-    diag->Error(DiagMessage(path) << writer->GetError());
+    diag->Error(android::DiagMessage(path) << writer->GetError());
     return {};
   }
   return std::move(writer);
 }
 
-std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag,
+std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(android::IDiagnostics* diag,
                                                            const StringPiece& path) {
   std::unique_ptr<ZipFileWriter> writer = util::make_unique<ZipFileWriter>();
   if (!writer->Open(path)) {
-    diag->Error(DiagMessage(path) << writer->GetError());
+    diag->Error(android::DiagMessage(path) << writer->GetError());
     return {};
   }
   return std::move(writer);
diff --git a/tools/aapt2/format/Archive.h b/tools/aapt2/format/Archive.h
index 4e8a39d..55b0b2f 100644
--- a/tools/aapt2/format/Archive.h
+++ b/tools/aapt2/format/Archive.h
@@ -22,12 +22,11 @@
 #include <string>
 #include <vector>
 
+#include "androidfw/BigBuffer.h"
+#include "androidfw/IDiagnostics.h"
 #include "androidfw/StringPiece.h"
 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
-
-#include "Diagnostics.h"
 #include "io/Io.h"
-#include "util/BigBuffer.h"
 #include "util/Files.h"
 
 namespace aapt {
@@ -70,10 +69,10 @@
   virtual std::string GetError() const = 0;
 };
 
-std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag,
+std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(android::IDiagnostics* diag,
                                                              const android::StringPiece& path);
 
-std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag,
+std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(android::IDiagnostics* diag,
                                                            const android::StringPiece& path);
 
 }  // namespace aapt
diff --git a/tools/aapt2/format/Container.h b/tools/aapt2/format/Container.h
index aa5c82c..121c592 100644
--- a/tools/aapt2/format/Container.h
+++ b/tools/aapt2/format/Container.h
@@ -19,14 +19,13 @@
 
 #include <inttypes.h>
 
-#include "google/protobuf/io/coded_stream.h"
-#include "google/protobuf/io/zero_copy_stream.h"
-
 #include "Resources.pb.h"
 #include "ResourcesInternal.pb.h"
+#include "androidfw/BigBuffer.h"
+#include "google/protobuf/io/coded_stream.h"
+#include "google/protobuf/io/zero_copy_stream.h"
 #include "io/Io.h"
 #include "io/Util.h"
-#include "util/BigBuffer.h"
 
 namespace aapt {
 
diff --git a/tools/aapt2/format/binary/BinaryResourceParser.cpp b/tools/aapt2/format/binary/BinaryResourceParser.cpp
index eea7efc..d9e379d 100644
--- a/tools/aapt2/format/binary/BinaryResourceParser.cpp
+++ b/tools/aapt2/format/binary/BinaryResourceParser.cpp
@@ -24,12 +24,12 @@
 #include "ResourceTable.h"
 #include "ResourceUtils.h"
 #include "ResourceValues.h"
-#include "Source.h"
 #include "ValueVisitor.h"
 #include "android-base/logging.h"
 #include "android-base/macros.h"
 #include "android-base/stringprintf.h"
 #include "androidfw/ResourceTypes.h"
+#include "androidfw/Source.h"
 #include "androidfw/TypeWrappers.h"
 #include "format/binary/ResChunkPullParser.h"
 #include "util/Util.h"
@@ -50,7 +50,7 @@
   std::u16string dst;
   dst.resize(utf16_len);
   for (size_t i = 0; i < utf16_len; i++) {
-    dst[i] = util::DeviceToHost16(src[i]);
+    dst[i] = android::util::DeviceToHost16(src[i]);
   }
   return dst;
 }
@@ -87,8 +87,8 @@
 }  // namespace
 
 BinaryResourceParser::BinaryResourceParser(IDiagnostics* diag, ResourceTable* table,
-                                           const Source& source, const void* data, size_t len,
-                                           io::IFileCollection* files)
+                                           const android::Source& source, const void* data,
+                                           size_t len, io::IFileCollection* files)
     : diag_(diag), table_(table), source_(source), data_(data), data_len_(len), files_(files) {
 }
 
@@ -96,13 +96,13 @@
   ResChunkPullParser parser(data_, data_len_);
 
   if (!ResChunkPullParser::IsGoodEvent(parser.Next())) {
-    diag_->Error(DiagMessage(source_) << "corrupt resources.arsc: " << parser.error());
+    diag_->Error(android::DiagMessage(source_) << "corrupt resources.arsc: " << parser.error());
     return false;
   }
 
   if (parser.chunk()->type != android::RES_TABLE_TYPE) {
-    diag_->Error(DiagMessage(source_) << StringPrintf("unknown chunk of type 0x%02x",
-                                                      static_cast<int>(parser.chunk()->type)));
+    diag_->Error(android::DiagMessage(source_) << StringPrintf(
+                     "unknown chunk of type 0x%02x", static_cast<int>(parser.chunk()->type)));
     return false;
   }
 
@@ -112,18 +112,18 @@
 
   if (parser.Next() != ResChunkPullParser::Event::kEndDocument) {
     if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
-      diag_->Warn(DiagMessage(source_)
+      diag_->Warn(android::DiagMessage(source_)
                   << "invalid chunk trailing RES_TABLE_TYPE: " << parser.error());
     } else {
-      diag_->Warn(DiagMessage(source_)
+      diag_->Warn(android::DiagMessage(source_)
                   << StringPrintf("unexpected chunk of type 0x%02x trailing RES_TABLE_TYPE",
                                   static_cast<int>(parser.chunk()->type)));
     }
   }
 
   if (!staged_entries_to_remove_.empty()) {
-    diag_->Error(DiagMessage(source_) << "didn't find " << staged_entries_to_remove_.size()
-                                      << " original staged resources");
+    diag_->Error(android::DiagMessage(source_) << "didn't find " << staged_entries_to_remove_.size()
+                                               << " original staged resources");
     return false;
   }
 
@@ -134,20 +134,20 @@
 bool BinaryResourceParser::ParseTable(const ResChunk_header* chunk) {
   const ResTable_header* table_header = ConvertTo<ResTable_header>(chunk);
   if (!table_header) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_header chunk");
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_header chunk");
     return false;
   }
 
   ResChunkPullParser parser(GetChunkData(&table_header->header),
                             GetChunkDataLen(&table_header->header));
   while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
-    switch (util::DeviceToHost16(parser.chunk()->type)) {
+    switch (android::util::DeviceToHost16(parser.chunk()->type)) {
       case android::RES_STRING_POOL_TYPE:
         if (value_pool_.getError() == NO_INIT) {
-          status_t err =
-              value_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
+          status_t err = value_pool_.setTo(parser.chunk(),
+                                           android::util::DeviceToHost32(parser.chunk()->size));
           if (err != NO_ERROR) {
-            diag_->Error(DiagMessage(source_)
+            diag_->Error(android::DiagMessage(source_)
                          << "corrupt string pool in ResTable: " << value_pool_.getError());
             return false;
           }
@@ -155,7 +155,7 @@
           // Reserve some space for the strings we are going to add.
           table_->string_pool.HintWillAdd(value_pool_.size(), value_pool_.styleCount());
         } else {
-          diag_->Warn(DiagMessage(source_) << "unexpected string pool in ResTable");
+          diag_->Warn(android::DiagMessage(source_) << "unexpected string pool in ResTable");
         }
         break;
 
@@ -166,15 +166,15 @@
         break;
 
       default:
-        diag_->Warn(DiagMessage(source_)
+        diag_->Warn(android::DiagMessage(source_)
                     << "unexpected chunk type "
-                    << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
+                    << static_cast<int>(android::util::DeviceToHost16(parser.chunk()->type)));
         break;
     }
   }
 
   if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
-    diag_->Error(DiagMessage(source_) << "corrupt resource table: " << parser.error());
+    diag_->Error(android::DiagMessage(source_) << "corrupt resource table: " << parser.error());
     return false;
   }
   return true;
@@ -185,13 +185,13 @@
       sizeof(ResTable_package) - sizeof(ResTable_package::typeIdOffset);
   const ResTable_package* package_header = ConvertTo<ResTable_package, kMinPackageSize>(chunk);
   if (!package_header) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_package chunk");
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_package chunk");
     return false;
   }
 
-  uint32_t package_id = util::DeviceToHost32(package_header->id);
+  uint32_t package_id = android::util::DeviceToHost32(package_header->id);
   if (package_id > std::numeric_limits<uint8_t>::max()) {
-    diag_->Error(DiagMessage(source_) << "package ID is too big (" << package_id << ")");
+    diag_->Error(android::DiagMessage(source_) << "package ID is too big (" << package_id << ")");
     return false;
   }
 
@@ -199,9 +199,10 @@
   std::u16string package_name = strcpy16_dtoh((const char16_t*)package_header->name,
                                               arraysize(package_header->name));
 
-  ResourceTablePackage* package = table_->FindOrCreatePackage(util::Utf16ToUtf8(package_name));
+  ResourceTablePackage* package =
+      table_->FindOrCreatePackage(android::util::Utf16ToUtf8(package_name));
   if (!package) {
-    diag_->Error(DiagMessage(source_)
+    diag_->Error(android::DiagMessage(source_)
                  << "incompatible package '" << package_name << "' with ID " << package_id);
     return false;
   }
@@ -214,26 +215,28 @@
   ResChunkPullParser parser(GetChunkData(&package_header->header),
                             GetChunkDataLen(&package_header->header));
   while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
-    switch (util::DeviceToHost16(parser.chunk()->type)) {
+    switch (android::util::DeviceToHost16(parser.chunk()->type)) {
       case android::RES_STRING_POOL_TYPE:
         if (type_pool_.getError() == NO_INIT) {
           status_t err =
-              type_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
+              type_pool_.setTo(parser.chunk(), android::util::DeviceToHost32(parser.chunk()->size));
           if (err != NO_ERROR) {
-            diag_->Error(DiagMessage(source_) << "corrupt type string pool in "
-                                              << "ResTable_package: " << type_pool_.getError());
+            diag_->Error(android::DiagMessage(source_)
+                         << "corrupt type string pool in "
+                         << "ResTable_package: " << type_pool_.getError());
             return false;
           }
         } else if (key_pool_.getError() == NO_INIT) {
           status_t err =
-              key_pool_.setTo(parser.chunk(), util::DeviceToHost32(parser.chunk()->size));
+              key_pool_.setTo(parser.chunk(), android::util::DeviceToHost32(parser.chunk()->size));
           if (err != NO_ERROR) {
-            diag_->Error(DiagMessage(source_) << "corrupt key string pool in "
-                                              << "ResTable_package: " << key_pool_.getError());
+            diag_->Error(android::DiagMessage(source_)
+                         << "corrupt key string pool in "
+                         << "ResTable_package: " << key_pool_.getError());
             return false;
           }
         } else {
-          diag_->Warn(DiagMessage(source_) << "unexpected string pool");
+          diag_->Warn(android::DiagMessage(source_) << "unexpected string pool");
         }
         break;
 
@@ -268,15 +271,15 @@
         break;
 
       default:
-        diag_->Warn(DiagMessage(source_)
+        diag_->Warn(android::DiagMessage(source_)
                     << "unexpected chunk type "
-                    << static_cast<int>(util::DeviceToHost16(parser.chunk()->type)));
+                    << static_cast<int>(android::util::DeviceToHost16(parser.chunk()->type)));
         break;
     }
   }
 
   if (parser.event() == ResChunkPullParser::Event::kBadDocument) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_package: " << parser.error());
     return false;
   }
 
@@ -290,18 +293,19 @@
 bool BinaryResourceParser::ParseTypeSpec(const ResourceTablePackage* package,
                                          const ResChunk_header* chunk, uint8_t package_id) {
   if (type_pool_.getError() != NO_ERROR) {
-    diag_->Error(DiagMessage(source_) << "missing type string pool");
+    diag_->Error(android::DiagMessage(source_) << "missing type string pool");
     return false;
   }
 
   const ResTable_typeSpec* type_spec = ConvertTo<ResTable_typeSpec>(chunk);
   if (!type_spec) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_typeSpec chunk");
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_typeSpec chunk");
     return false;
   }
 
   if (type_spec->id == 0) {
-    diag_->Error(DiagMessage(source_) << "ResTable_typeSpec has invalid id: " << type_spec->id);
+    diag_->Error(android::DiagMessage(source_)
+                 << "ResTable_typeSpec has invalid id: " << type_spec->id);
     return false;
   }
 
@@ -312,25 +316,26 @@
   // There can only be 2^16 entries in a type, because that is the ID
   // space for entries (EEEE) in the resource ID 0xPPTTEEEE.
   if (entry_count > std::numeric_limits<uint16_t>::max()) {
-    diag_->Error(DiagMessage(source_)
+    diag_->Error(android::DiagMessage(source_)
                  << "ResTable_typeSpec has too many entries (" << entry_count << ")");
     return false;
   }
 
-  const size_t data_size = util::DeviceToHost32(type_spec->header.size) -
-                           util::DeviceToHost16(type_spec->header.headerSize);
+  const size_t data_size = android::util::DeviceToHost32(type_spec->header.size) -
+                           android::util::DeviceToHost16(type_spec->header.headerSize);
   if (entry_count * sizeof(uint32_t) > data_size) {
-    diag_->Error(DiagMessage(source_) << "ResTable_typeSpec too small to hold entries.");
+    diag_->Error(android::DiagMessage(source_) << "ResTable_typeSpec too small to hold entries.");
     return false;
   }
 
   // Record the type_spec_flags for later. We don't know resource names yet, and we need those
   // to mark resources as overlayable.
   const uint32_t* type_spec_flags = reinterpret_cast<const uint32_t*>(
-      reinterpret_cast<uintptr_t>(type_spec) + util::DeviceToHost16(type_spec->header.headerSize));
+      reinterpret_cast<uintptr_t>(type_spec) +
+      android::util::DeviceToHost16(type_spec->header.headerSize));
   for (size_t i = 0; i < entry_count; i++) {
     ResourceId id(package_id, type_spec->id, static_cast<size_t>(i));
-    entry_type_spec_flags_[id] = util::DeviceToHost32(type_spec_flags[i]);
+    entry_type_spec_flags_[id] = android::util::DeviceToHost32(type_spec_flags[i]);
   }
   return true;
 }
@@ -338,12 +343,12 @@
 bool BinaryResourceParser::ParseType(const ResourceTablePackage* package,
                                      const ResChunk_header* chunk, uint8_t package_id) {
   if (type_pool_.getError() != NO_ERROR) {
-    diag_->Error(DiagMessage(source_) << "missing type string pool");
+    diag_->Error(android::DiagMessage(source_) << "missing type string pool");
     return false;
   }
 
   if (key_pool_.getError() != NO_ERROR) {
-    diag_->Error(DiagMessage(source_) << "missing key string pool");
+    diag_->Error(android::DiagMessage(source_) << "missing key string pool");
     return false;
   }
 
@@ -351,22 +356,23 @@
   // a lot and has its own code to handle variable size.
   const ResTable_type* type = ConvertTo<ResTable_type, kResTableTypeMinSize>(chunk);
   if (!type) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_type chunk");
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_type chunk");
     return false;
   }
 
   if (type->id == 0) {
-    diag_->Error(DiagMessage(source_) << "ResTable_type has invalid id: " << (int)type->id);
+    diag_->Error(android::DiagMessage(source_)
+                 << "ResTable_type has invalid id: " << (int)type->id);
     return false;
   }
 
   ConfigDescription config;
   config.copyFromDtoH(type->config);
 
-  const std::string type_str = util::GetString(type_pool_, type->id - 1);
+  const std::string type_str = android::util::GetString(type_pool_, type->id - 1);
   std::optional<ResourceNamedTypeRef> parsed_type = ParseResourceNamedType(type_str);
   if (!parsed_type) {
-    diag_->Warn(DiagMessage(source_)
+    diag_->Warn(android::DiagMessage(source_)
                 << "invalid type name '" << type_str << "' for type with ID " << type->id);
     return true;
   }
@@ -378,8 +384,9 @@
       continue;
     }
 
-    const ResourceName name(package->name, *parsed_type,
-                            util::GetString(key_pool_, util::DeviceToHost32(entry->key.index)));
+    const ResourceName name(
+        package->name, *parsed_type,
+        android::util::GetString(key_pool_, android::util::DeviceToHost32(entry->key.index)));
     const ResourceId res_id(package_id, type->id, static_cast<uint16_t>(it.index()));
 
     std::unique_ptr<Value> resource_value;
@@ -390,13 +397,14 @@
       resource_value = ParseMapEntry(name, config, mapEntry);
     } else {
       const Res_value* value =
-          (const Res_value*)((const uint8_t*)entry + util::DeviceToHost32(entry->size));
+          (const Res_value*)((const uint8_t*)entry + android::util::DeviceToHost32(entry->size));
       resource_value = ParseValue(name, config, *value);
     }
 
     if (!resource_value) {
-      diag_->Error(DiagMessage(source_) << "failed to parse value for resource " << name << " ("
-                                        << res_id << ") with configuration '" << config << "'");
+      diag_->Error(android::DiagMessage(source_)
+                   << "failed to parse value for resource " << name << " (" << res_id
+                   << ") with configuration '" << config << "'");
       return false;
     }
 
@@ -450,7 +458,7 @@
   const size_t count = entries.size();
   for (size_t i = 0; i < count; i++) {
     table_->included_packages_[entries.valueAt(i)] =
-        util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
+        android::util::Utf16ToUtf8(StringPiece16(entries.keyAt(i).string()));
   }
   return true;
 }
@@ -458,36 +466,39 @@
 bool BinaryResourceParser::ParseOverlayable(const ResChunk_header* chunk) {
   const ResTable_overlayable_header* header = ConvertTo<ResTable_overlayable_header>(chunk);
   if (!header) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_category_header chunk");
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_category_header chunk");
     return false;
   }
 
   auto overlayable = std::make_shared<Overlayable>();
-  overlayable->name = util::Utf16ToUtf8(strcpy16_dtoh((const char16_t*)header->name,
-                                                      arraysize(header->name)));
-  overlayable->actor = util::Utf16ToUtf8(strcpy16_dtoh((const char16_t*)header->actor,
-                                                       arraysize(header->name)));
+  overlayable->name = android::util::Utf16ToUtf8(
+      strcpy16_dtoh((const char16_t*)header->name, arraysize(header->name)));
+  overlayable->actor = android::util::Utf16ToUtf8(
+      strcpy16_dtoh((const char16_t*)header->actor, arraysize(header->name)));
 
   ResChunkPullParser parser(GetChunkData(chunk),
                             GetChunkDataLen(chunk));
   while (ResChunkPullParser::IsGoodEvent(parser.Next())) {
-    if (util::DeviceToHost16(parser.chunk()->type) == android::RES_TABLE_OVERLAYABLE_POLICY_TYPE) {
+    if (android::util::DeviceToHost16(parser.chunk()->type) ==
+        android::RES_TABLE_OVERLAYABLE_POLICY_TYPE) {
       const ResTable_overlayable_policy_header* policy_header =
           ConvertTo<ResTable_overlayable_policy_header>(parser.chunk());
 
       const ResTable_ref* const ref_begin = reinterpret_cast<const ResTable_ref*>(
-          ((uint8_t *)policy_header) + util::DeviceToHost32(policy_header->header.headerSize));
-      const ResTable_ref* const ref_end = ref_begin
-          + util::DeviceToHost32(policy_header->entry_count);
+          ((uint8_t*)policy_header) +
+          android::util::DeviceToHost32(policy_header->header.headerSize));
+      const ResTable_ref* const ref_end =
+          ref_begin + android::util::DeviceToHost32(policy_header->entry_count);
       for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) {
-        ResourceId res_id(util::DeviceToHost32(ref_iter->ident));
+        ResourceId res_id(android::util::DeviceToHost32(ref_iter->ident));
         const auto iter = id_index_.find(res_id);
 
         // If the overlayable chunk comes before the type chunks, the resource ids and resource name
         // pairing will not exist at this point.
         if (iter == id_index_.cend()) {
-          diag_->Error(DiagMessage(source_) << "failed to find resource name for overlayable"
-                                            << " resource " << res_id);
+          diag_->Error(android::DiagMessage(source_)
+                       << "failed to find resource name for overlayable"
+                       << " resource " << res_id);
           return false;
         }
 
@@ -511,23 +522,23 @@
 bool BinaryResourceParser::ParseStagedAliases(const ResChunk_header* chunk) {
   auto header = ConvertTo<ResTable_staged_alias_header>(chunk);
   if (!header) {
-    diag_->Error(DiagMessage(source_) << "corrupt ResTable_staged_alias_header chunk");
+    diag_->Error(android::DiagMessage(source_) << "corrupt ResTable_staged_alias_header chunk");
     return false;
   }
 
   const auto ref_begin = reinterpret_cast<const ResTable_staged_alias_entry*>(
-      ((uint8_t*)header) + util::DeviceToHost32(header->header.headerSize));
-  const auto ref_end = ref_begin + util::DeviceToHost32(header->count);
+      ((uint8_t*)header) + android::util::DeviceToHost32(header->header.headerSize));
+  const auto ref_end = ref_begin + android::util::DeviceToHost32(header->count);
   for (auto ref_iter = ref_begin; ref_iter != ref_end; ++ref_iter) {
-    const auto staged_id = ResourceId(util::DeviceToHost32(ref_iter->stagedResId));
-    const auto finalized_id = ResourceId(util::DeviceToHost32(ref_iter->finalizedResId));
+    const auto staged_id = ResourceId(android::util::DeviceToHost32(ref_iter->stagedResId));
+    const auto finalized_id = ResourceId(android::util::DeviceToHost32(ref_iter->finalizedResId));
 
     // If the staged alias chunk comes before the type chunks, the resource ids and resource name
     // pairing will not exist at this point.
     const auto iter = id_index_.find(finalized_id);
     if (iter == id_index_.cend()) {
-      diag_->Error(DiagMessage(source_) << "failed to find resource name for finalized"
-                                        << " resource ID " << finalized_id);
+      diag_->Error(android::DiagMessage(source_) << "failed to find resource name for finalized"
+                                                 << " resource ID " << finalized_id);
       return false;
     }
 
@@ -563,9 +574,9 @@
     if (file_ref != nullptr) {
       file_ref->file = files_->FindFile(*file_ref->path);
       if (file_ref->file == nullptr) {
-        diag_->Warn(DiagMessage() << "resource " << name << " for config '" << config
-                                  << "' is a file reference to '" << *file_ref->path
-                                  << "' but no such path exists");
+        diag_->Warn(android::DiagMessage() << "resource " << name << " for config '" << config
+                                           << "' is a file reference to '" << *file_ref->path
+                                           << "' but no such path exists");
       }
     }
   }
@@ -594,8 +605,8 @@
       // We can ignore the value here.
       return util::make_unique<Id>();
     default:
-      diag_->Error(DiagMessage() << "illegal map type '" << name.type << "' ("
-                                 << (int)name.type.type << ")");
+      diag_->Error(android::DiagMessage()
+                   << "illegal map type '" << name.type << "' (" << (int)name.type.type << ")");
       break;
   }
   return {};
@@ -605,18 +616,18 @@
                                                         const ConfigDescription& config,
                                                         const ResTable_map_entry* map) {
   std::unique_ptr<Style> style = util::make_unique<Style>();
-  if (util::DeviceToHost32(map->parent.ident) != 0) {
+  if (android::util::DeviceToHost32(map->parent.ident) != 0) {
     // The parent is a regular reference to a resource.
-    style->parent = Reference(util::DeviceToHost32(map->parent.ident));
+    style->parent = Reference(android::util::DeviceToHost32(map->parent.ident));
   }
 
   for (const ResTable_map& map_entry : map) {
-    if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
+    if (Res_INTERNALID(android::util::DeviceToHost32(map_entry.name.ident))) {
       continue;
     }
 
     Style::Entry style_entry;
-    style_entry.key = Reference(util::DeviceToHost32(map_entry.name.ident));
+    style_entry.key = Reference(android::util::DeviceToHost32(map_entry.name.ident));
     style_entry.value = ParseValue(name, config, map_entry.value);
     if (!style_entry.value) {
       return {};
@@ -630,20 +641,20 @@
                                                            const ConfigDescription& config,
                                                            const ResTable_map_entry* map) {
   std::unique_ptr<Attribute> attr = util::make_unique<Attribute>();
-  attr->SetWeak((util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0);
+  attr->SetWeak((android::util::DeviceToHost16(map->flags) & ResTable_entry::FLAG_WEAK) != 0);
 
   // First we must discover what type of attribute this is. Find the type mask.
   auto type_mask_iter = std::find_if(begin(map), end(map), [](const ResTable_map& entry) -> bool {
-    return util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE;
+    return android::util::DeviceToHost32(entry.name.ident) == ResTable_map::ATTR_TYPE;
   });
 
   if (type_mask_iter != end(map)) {
-    attr->type_mask = util::DeviceToHost32(type_mask_iter->value.data);
+    attr->type_mask = android::util::DeviceToHost32(type_mask_iter->value.data);
   }
 
   for (const ResTable_map& map_entry : map) {
-    if (Res_INTERNALID(util::DeviceToHost32(map_entry.name.ident))) {
-      switch (util::DeviceToHost32(map_entry.name.ident)) {
+    if (Res_INTERNALID(android::util::DeviceToHost32(map_entry.name.ident))) {
+      switch (android::util::DeviceToHost32(map_entry.name.ident)) {
         case ResTable_map::ATTR_MIN:
           attr->min_int = static_cast<int32_t>(map_entry.value.data);
           break;
@@ -656,9 +667,9 @@
 
     if (attr->type_mask & (ResTable_map::TYPE_ENUM | ResTable_map::TYPE_FLAGS)) {
       Attribute::Symbol symbol;
-      symbol.value = util::DeviceToHost32(map_entry.value.data);
+      symbol.value = android::util::DeviceToHost32(map_entry.value.data);
       symbol.type = map_entry.value.dataType;
-      symbol.symbol = Reference(util::DeviceToHost32(map_entry.name.ident));
+      symbol.symbol = Reference(android::util::DeviceToHost32(map_entry.name.ident));
       attr->symbols.push_back(std::move(symbol));
     }
   }
@@ -687,7 +698,7 @@
       return {};
     }
 
-    switch (util::DeviceToHost32(map_entry.name.ident)) {
+    switch (android::util::DeviceToHost32(map_entry.name.ident)) {
       case ResTable_map::ATTR_ZERO:
         plural->values[Plural::Zero] = std::move(item);
         break;
diff --git a/tools/aapt2/format/binary/BinaryResourceParser.h b/tools/aapt2/format/binary/BinaryResourceParser.h
index 1c83166..8f6949e 100644
--- a/tools/aapt2/format/binary/BinaryResourceParser.h
+++ b/tools/aapt2/format/binary/BinaryResourceParser.h
@@ -19,13 +19,13 @@
 
 #include <string>
 
+#include "ResourceTable.h"
+#include "ResourceValues.h"
 #include "android-base/macros.h"
 #include "androidfw/ConfigDescription.h"
 #include "androidfw/ResourceTypes.h"
-
-#include "ResourceTable.h"
-#include "ResourceValues.h"
-#include "Source.h"
+#include "androidfw/Source.h"
+#include "androidfw/Util.h"
 #include "process/IResourceTableConsumer.h"
 #include "util/Util.h"
 
@@ -40,8 +40,9 @@
  public:
   // Creates a parser, which will read `len` bytes from `data`, and add any resources parsed to
   // `table`. `source` is for logging purposes.
-  BinaryResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
-                       const void* data, size_t data_len, io::IFileCollection* files = nullptr);
+  BinaryResourceParser(android::IDiagnostics* diag, ResourceTable* table,
+                       const android::Source& source, const void* data, size_t data_len,
+                       io::IFileCollection* files = nullptr);
 
   // Parses the binary resource table and returns true if successful.
   bool Parse();
@@ -91,10 +92,10 @@
    */
   bool CollectMetaData(const android::ResTable_map& map_entry, Value* value);
 
-  IDiagnostics* diag_;
+  android::IDiagnostics* diag_;
   ResourceTable* table_;
 
-  const Source source_;
+  const android::Source source_;
 
   const void* data_;
   const size_t data_len_;
@@ -132,11 +133,11 @@
 // Iterator functionality for ResTable_map_entry.
 
 inline const ResTable_map* begin(const ResTable_map_entry* map) {
-  return (const ResTable_map*)((const uint8_t*)map + ::aapt::util::DeviceToHost32(map->size));
+  return (const ResTable_map*)((const uint8_t*)map + android::util::DeviceToHost32(map->size));
 }
 
 inline const ResTable_map* end(const ResTable_map_entry* map) {
-  return begin(map) + aapt::util::DeviceToHost32(map->count);
+  return begin(map) + android::util::DeviceToHost32(map->count);
 }
 
 }  // namespace android
diff --git a/tools/aapt2/format/binary/ChunkWriter.h b/tools/aapt2/format/binary/ChunkWriter.h
index 1892a29..e1a4034 100644
--- a/tools/aapt2/format/binary/ChunkWriter.h
+++ b/tools/aapt2/format/binary/ChunkWriter.h
@@ -18,16 +18,15 @@
 #define AAPT_FORMAT_BINARY_CHUNKWRITER_H
 
 #include "android-base/macros.h"
+#include "androidfw/BigBuffer.h"
 #include "androidfw/ResourceTypes.h"
-
-#include "util/BigBuffer.h"
 #include "util/Util.h"
 
 namespace aapt {
 
 class ChunkWriter {
  public:
-  explicit inline ChunkWriter(BigBuffer* buffer) : buffer_(buffer) {
+  explicit inline ChunkWriter(android::BigBuffer* buffer) : buffer_(buffer) {
   }
   ChunkWriter(ChunkWriter&&) = default;
   ChunkWriter& operator=(ChunkWriter&&) = default;
@@ -37,8 +36,8 @@
     start_size_ = buffer_->size();
     T* chunk = buffer_->NextBlock<T>();
     header_ = &chunk->header;
-    header_->type = util::HostToDevice16(type);
-    header_->headerSize = util::HostToDevice16(sizeof(T));
+    header_->type = android::util::HostToDevice16(type);
+    header_->headerSize = android::util::HostToDevice16(sizeof(T));
     return chunk;
   }
 
@@ -47,7 +46,7 @@
     return buffer_->NextBlock<T>(count);
   }
 
-  inline BigBuffer* buffer() {
+  inline android::BigBuffer* buffer() {
     return buffer_;
   }
 
@@ -61,14 +60,14 @@
 
   inline android::ResChunk_header* Finish() {
     buffer_->Align4();
-    header_->size = util::HostToDevice32(buffer_->size() - start_size_);
+    header_->size = android::util::HostToDevice32(buffer_->size() - start_size_);
     return header_;
   }
 
  private:
   DISALLOW_COPY_AND_ASSIGN(ChunkWriter);
 
-  BigBuffer* buffer_;
+  android::BigBuffer* buffer_;
   size_t start_size_ = 0;
   android::ResChunk_header* header_ = nullptr;
 };
@@ -77,8 +76,8 @@
 inline android::ResChunk_header* ChunkWriter::StartChunk(uint16_t type) {
   start_size_ = buffer_->size();
   header_ = buffer_->NextBlock<android::ResChunk_header>();
-  header_->type = util::HostToDevice16(type);
-  header_->headerSize = util::HostToDevice16(sizeof(android::ResChunk_header));
+  header_->type = android::util::HostToDevice16(type);
+  header_->headerSize = android::util::HostToDevice16(sizeof(android::ResChunk_header));
   return header_;
 }
 
diff --git a/tools/aapt2/format/binary/ResChunkPullParser.cpp b/tools/aapt2/format/binary/ResChunkPullParser.cpp
index fd6919d..2f3df5c 100644
--- a/tools/aapt2/format/binary/ResChunkPullParser.cpp
+++ b/tools/aapt2/format/binary/ResChunkPullParser.cpp
@@ -17,12 +17,13 @@
 #include "format/binary/ResChunkPullParser.h"
 
 #include <inttypes.h>
+
 #include <cstddef>
 
 #include "android-base/logging.h"
 #include "android-base/stringprintf.h"
 #include "androidfw/ResourceTypes.h"
-
+#include "androidfw/Util.h"
 #include "util/Util.h"
 
 namespace aapt {
@@ -32,8 +33,9 @@
 
 static std::string ChunkHeaderDump(const ResChunk_header* header) {
   return StringPrintf("(type=%02" PRIx16 " header_size=%" PRIu16 " size=%" PRIu32 ")",
-                      util::DeviceToHost16(header->type), util::DeviceToHost16(header->headerSize),
-                      util::DeviceToHost32(header->size));
+                      android::util::DeviceToHost16(header->type),
+                      android::util::DeviceToHost16(header->headerSize),
+                      android::util::DeviceToHost32(header->size));
 }
 
 ResChunkPullParser::Event ResChunkPullParser::Next() {
@@ -45,7 +47,7 @@
     current_chunk_ = data_;
   } else {
     current_chunk_ = (const ResChunk_header*)(((const char*)current_chunk_) +
-                                              util::DeviceToHost32(current_chunk_->size));
+                                              android::util::DeviceToHost32(current_chunk_->size));
   }
 
   const std::ptrdiff_t diff = (const char*)current_chunk_ - (const char*)data_;
@@ -61,16 +63,16 @@
     return (event_ = Event::kBadDocument);
   }
 
-  if (util::DeviceToHost16(current_chunk_->headerSize) < sizeof(ResChunk_header)) {
+  if (android::util::DeviceToHost16(current_chunk_->headerSize) < sizeof(ResChunk_header)) {
     error_ = "chunk has too small header";
     current_chunk_ = nullptr;
     return (event_ = Event::kBadDocument);
-  } else if (util::DeviceToHost32(current_chunk_->size) <
-             util::DeviceToHost16(current_chunk_->headerSize)) {
+  } else if (android::util::DeviceToHost32(current_chunk_->size) <
+             android::util::DeviceToHost16(current_chunk_->headerSize)) {
     error_ = "chunk's total size is smaller than header " + ChunkHeaderDump(current_chunk_);
     current_chunk_ = nullptr;
     return (event_ = Event::kBadDocument);
-  } else if (offset + util::DeviceToHost32(current_chunk_->size) > len_) {
+  } else if (offset + android::util::DeviceToHost32(current_chunk_->size) > len_) {
     error_ = "chunk's data extends past the end of the document " + ChunkHeaderDump(current_chunk_);
     current_chunk_ = nullptr;
     return (event_ = Event::kBadDocument);
diff --git a/tools/aapt2/format/binary/ResChunkPullParser.h b/tools/aapt2/format/binary/ResChunkPullParser.h
index 5ff1359..0f46db1 100644
--- a/tools/aapt2/format/binary/ResChunkPullParser.h
+++ b/tools/aapt2/format/binary/ResChunkPullParser.h
@@ -21,7 +21,7 @@
 
 #include "android-base/macros.h"
 #include "androidfw/ResourceTypes.h"
-
+#include "androidfw/Util.h"
 #include "util/Util.h"
 
 namespace aapt {
@@ -69,18 +69,19 @@
 
 template <typename T, size_t MinSize = sizeof(T)>
 inline static const T* ConvertTo(const android::ResChunk_header* chunk) {
-  if (util::DeviceToHost16(chunk->headerSize) < MinSize) {
+  if (android::util::DeviceToHost16(chunk->headerSize) < MinSize) {
     return nullptr;
   }
   return reinterpret_cast<const T*>(chunk);
 }
 
 inline static const uint8_t* GetChunkData(const android::ResChunk_header* chunk) {
-  return reinterpret_cast<const uint8_t*>(chunk) + util::DeviceToHost16(chunk->headerSize);
+  return reinterpret_cast<const uint8_t*>(chunk) + android::util::DeviceToHost16(chunk->headerSize);
 }
 
 inline static uint32_t GetChunkDataLen(const android::ResChunk_header* chunk) {
-  return util::DeviceToHost32(chunk->size) - util::DeviceToHost16(chunk->headerSize);
+  return android::util::DeviceToHost32(chunk->size) -
+         android::util::DeviceToHost16(chunk->headerSize);
 }
 
 //
diff --git a/tools/aapt2/format/binary/TableFlattener.cpp b/tools/aapt2/format/binary/TableFlattener.cpp
index b42e7d0..cd07b1e 100644
--- a/tools/aapt2/format/binary/TableFlattener.cpp
+++ b/tools/aapt2/format/binary/TableFlattener.cpp
@@ -21,19 +21,18 @@
 #include <sstream>
 #include <type_traits>
 
-#include "android-base/logging.h"
-#include "android-base/macros.h"
-#include "android-base/stringprintf.h"
-#include "androidfw/ResourceUtils.h"
-
 #include "ResourceTable.h"
 #include "ResourceValues.h"
 #include "SdkConstants.h"
 #include "ValueVisitor.h"
+#include "android-base/logging.h"
+#include "android-base/macros.h"
+#include "android-base/stringprintf.h"
+#include "androidfw/BigBuffer.h"
+#include "androidfw/ResourceUtils.h"
 #include "format/binary/ChunkWriter.h"
 #include "format/binary/ResourceTypeExtensions.h"
 #include "trace/TraceBuffer.h"
-#include "util/BigBuffer.h"
 
 using namespace android;
 
@@ -54,7 +53,7 @@
   size_t i;
   const char16_t* src_data = src.data();
   for (i = 0; i < len - 1 && i < src.size(); i++) {
-    dst[i] = util::HostToDevice16((uint16_t)src_data[i]);
+    dst[i] = android::util::HostToDevice16((uint16_t)src_data[i]);
   }
   dst[i] = 0;
 }
@@ -116,7 +115,7 @@
     if (style->parent) {
       const Reference& parent_ref = style->parent.value();
       CHECK(bool(parent_ref.id)) << "parent has no ID";
-      out_entry_->parent.ident = util::HostToDevice32(parent_ref.id.value().id);
+      out_entry_->parent.ident = android::util::HostToDevice32(parent_ref.id.value().id);
     }
 
     // Sort the style.
@@ -195,7 +194,7 @@
    * needs to be done to prepare the entry.
    */
   void Finish() {
-    out_entry_->count = util::HostToDevice32(entry_count_);
+    out_entry_->count = android::util::HostToDevice32(entry_count_);
   }
 
  private:
@@ -203,7 +202,7 @@
 
   void FlattenKey(const Reference* key, ResTable_map* out_entry) {
     CHECK(bool(key->id)) << "key has no ID";
-    out_entry->name.ident = util::HostToDevice32(key->id.value().id);
+    out_entry->name.ident = android::util::HostToDevice32(key->id.value().id);
   }
 
   void FlattenValue(const Item* value, ResTable_map* out_entry) {
@@ -214,7 +213,7 @@
     ResTable_map* out_entry = buffer_->NextBlock<ResTable_map>();
     FlattenKey(key, out_entry);
     FlattenValue(value, out_entry);
-    out_entry->value.size = util::HostToDevice16(sizeof(out_entry->value));
+    out_entry->value.size = android::util::HostToDevice16(sizeof(out_entry->value));
     entry_count_++;
   }
 
@@ -225,7 +224,7 @@
 
 struct OverlayableChunk {
   std::string actor;
-  Source source;
+  android::Source source;
   std::map<PolicyFlags, std::set<ResourceId>> policy_ids;
 };
 
@@ -248,31 +247,33 @@
     TRACE_CALL();
     ChunkWriter pkg_writer(buffer);
     ResTable_package* pkg_header = pkg_writer.StartChunk<ResTable_package>(RES_TABLE_PACKAGE_TYPE);
-    pkg_header->id = util::HostToDevice32(package_.id.value());
+    pkg_header->id = android::util::HostToDevice32(package_.id.value());
 
     // AAPT truncated the package name, so do the same.
     // Shared libraries require full package names, so don't truncate theirs.
     if (context_->GetPackageType() != PackageType::kApp &&
         package_.name.size() >= arraysize(pkg_header->name)) {
-      diag_->Error(DiagMessage() << "package name '" << package_.name
-                                 << "' is too long. "
-                                    "Shared libraries cannot have truncated package names");
+      diag_->Error(android::DiagMessage()
+                   << "package name '" << package_.name
+                   << "' is too long. "
+                      "Shared libraries cannot have truncated package names");
       return false;
     }
 
     // Copy the package name in device endianness.
-    strcpy16_htod(pkg_header->name, arraysize(pkg_header->name), util::Utf8ToUtf16(package_.name));
+    strcpy16_htod(pkg_header->name, arraysize(pkg_header->name),
+                  android::util::Utf8ToUtf16(package_.name));
 
     // Serialize the types. We do this now so that our type and key strings
     // are populated. We write those first.
-    BigBuffer type_buffer(1024);
+    android::BigBuffer type_buffer(1024);
     FlattenTypes(&type_buffer);
 
-    pkg_header->typeStrings = util::HostToDevice32(pkg_writer.size());
-    StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_, diag_);
+    pkg_header->typeStrings = android::util::HostToDevice32(pkg_writer.size());
+    android::StringPool::FlattenUtf16(pkg_writer.buffer(), type_pool_, diag_);
 
-    pkg_header->keyStrings = util::HostToDevice32(pkg_writer.size());
-    StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_, diag_);
+    pkg_header->keyStrings = android::util::HostToDevice32(pkg_writer.size());
+    android::StringPool::FlattenUtf8(pkg_writer.buffer(), key_pool_, diag_);
 
     // Append the types.
     buffer->AppendBuffer(std::move(type_buffer));
@@ -317,9 +318,9 @@
       out_entry->flags |= ResTable_entry::FLAG_COMPLEX;
     }
 
-    out_entry->flags = util::HostToDevice16(out_entry->flags);
-    out_entry->key.index = util::HostToDevice32(entry->entry_key);
-    out_entry->size = util::HostToDevice16(sizeof(T));
+    out_entry->flags = android::util::HostToDevice16(out_entry->flags);
+    out_entry->key.index = android::util::HostToDevice32(entry->entry_key);
+    out_entry->size = android::util::HostToDevice16(sizeof(T));
     return result;
   }
 
@@ -328,7 +329,7 @@
       WriteEntry<ResTable_entry, true>(entry, buffer);
       Res_value* outValue = buffer->NextBlock<Res_value>();
       CHECK(item->Flatten(outValue)) << "flatten failed";
-      outValue->size = util::HostToDevice16(sizeof(*outValue));
+      outValue->size = android::util::HostToDevice16(sizeof(*outValue));
     } else {
       ResTable_entry_ext* out_entry = WriteEntry<ResTable_entry_ext, false>(entry, buffer);
       MapFlattenVisitor visitor(out_entry, buffer);
@@ -353,12 +354,12 @@
     std::vector<uint32_t> offsets;
     offsets.resize(num_total_entries, 0xffffffffu);
 
-    BigBuffer values_buffer(512);
+    android::BigBuffer values_buffer(512);
     for (FlatEntry& flat_entry : *entries) {
       CHECK(static_cast<size_t>(flat_entry.entry->id.value()) < num_total_entries);
       offsets[flat_entry.entry->id.value()] = values_buffer.size();
       if (!FlattenValue(&flat_entry, &values_buffer)) {
-        diag_->Error(DiagMessage()
+        diag_->Error(android::DiagMessage()
                      << "failed to flatten resource '"
                      << ResourceNameRef(package_.name, type.named_type, flat_entry.entry->name)
                      << "' for configuration '" << config << "'");
@@ -382,27 +383,27 @@
         sparse_encode && ((100 * entries->size()) / num_total_entries) < kSparseEncodingThreshold;
 
     if (sparse_encode) {
-      type_header->entryCount = util::HostToDevice32(entries->size());
+      type_header->entryCount = android::util::HostToDevice32(entries->size());
       type_header->flags |= ResTable_type::FLAG_SPARSE;
       ResTable_sparseTypeEntry* indices =
           type_writer.NextBlock<ResTable_sparseTypeEntry>(entries->size());
       for (size_t i = 0; i < num_total_entries; i++) {
         if (offsets[i] != ResTable_type::NO_ENTRY) {
           CHECK((offsets[i] & 0x03) == 0);
-          indices->idx = util::HostToDevice16(i);
-          indices->offset = util::HostToDevice16(offsets[i] / 4u);
+          indices->idx = android::util::HostToDevice16(i);
+          indices->offset = android::util::HostToDevice16(offsets[i] / 4u);
           indices++;
         }
       }
     } else {
-      type_header->entryCount = util::HostToDevice32(num_total_entries);
+      type_header->entryCount = android::util::HostToDevice32(num_total_entries);
       uint32_t* indices = type_writer.NextBlock<uint32_t>(num_total_entries);
       for (size_t i = 0; i < num_total_entries; i++) {
-        indices[i] = util::HostToDevice32(offsets[i]);
+        indices[i] = android::util::HostToDevice32(offsets[i]);
       }
     }
 
-    type_header->entriesStart = util::HostToDevice32(type_writer.size());
+    type_header->entriesStart = android::util::HostToDevice32(type_writer.size());
     type_writer.buffer()->AppendBuffer(std::move(values_buffer));
     type_writer.Finish();
     return true;
@@ -416,12 +417,12 @@
     ChunkWriter alias_writer(buffer);
     auto header =
         alias_writer.StartChunk<ResTable_staged_alias_header>(RES_TABLE_STAGED_ALIAS_TYPE);
-    header->count = util::HostToDevice32(aliases_.size());
+    header->count = android::util::HostToDevice32(aliases_.size());
 
     auto mapping = alias_writer.NextBlock<ResTable_staged_alias_entry>(aliases_.size());
     for (auto& p : aliases_) {
-      mapping->stagedResId = util::HostToDevice32(p.first);
-      mapping->finalizedResId = util::HostToDevice32(p.second);
+      mapping->stagedResId = android::util::HostToDevice32(p.first);
+      mapping->finalizedResId = android::util::HostToDevice32(p.second);
       ++mapping;
     }
     alias_writer.Finish();
@@ -461,11 +462,11 @@
           OverlayableChunk& chunk = iter->second;
           if (!(chunk.source == item.overlayable->source)) {
             // The name of an overlayable set of resources must be unique
-            context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source)
-                                                  << "duplicate overlayable name"
-                                                  << item.overlayable->name << "'");
-            context_->GetDiagnostics()->Error(DiagMessage(chunk.source)
-                                                  << "previous declaration here");
+            context_->GetDiagnostics()->Error(android::DiagMessage(item.overlayable->source)
+                                              << "duplicate overlayable name"
+                                              << item.overlayable->name << "'");
+            context_->GetDiagnostics()->Error(android::DiagMessage(chunk.source)
+                                              << "previous declaration here");
             return false;
           }
 
@@ -474,7 +475,7 @@
         }
 
         if (item.policies == 0) {
-          context_->GetDiagnostics()->Error(DiagMessage(item.overlayable->source)
+          context_->GetDiagnostics()->Error(android::DiagMessage(item.overlayable->source)
                                             << "overlayable " << entry.name
                                             << " does not specify policy");
           return false;
@@ -499,38 +500,36 @@
       auto* overlayable_type =
           overlayable_writer.StartChunk<ResTable_overlayable_header>(RES_TABLE_OVERLAYABLE_TYPE);
       if (name.size() >= arraysize(overlayable_type->name)) {
-        diag_->Error(DiagMessage() << "overlayable name '" << name
-                                   << "' exceeds maximum length ("
-                                   << arraysize(overlayable_type->name)
-                                   << " utf16 characters)");
+        diag_->Error(android::DiagMessage()
+                     << "overlayable name '" << name << "' exceeds maximum length ("
+                     << arraysize(overlayable_type->name) << " utf16 characters)");
         return false;
       }
       strcpy16_htod(overlayable_type->name, arraysize(overlayable_type->name),
-                    util::Utf8ToUtf16(name));
+                    android::util::Utf8ToUtf16(name));
 
       if (overlayable.actor.size() >= arraysize(overlayable_type->actor)) {
-        diag_->Error(DiagMessage() << "overlayable name '" << overlayable.actor
-                                   << "' exceeds maximum length ("
-                                   << arraysize(overlayable_type->actor)
-                                   << " utf16 characters)");
+        diag_->Error(android::DiagMessage()
+                     << "overlayable name '" << overlayable.actor << "' exceeds maximum length ("
+                     << arraysize(overlayable_type->actor) << " utf16 characters)");
         return false;
       }
       strcpy16_htod(overlayable_type->actor, arraysize(overlayable_type->actor),
-                    util::Utf8ToUtf16(overlayable.actor));
+                    android::util::Utf8ToUtf16(overlayable.actor));
 
       // Write each policy block for the overlayable
       for (auto& policy_ids : overlayable.policy_ids) {
         ChunkWriter policy_writer(buffer);
         auto* policy_type = policy_writer.StartChunk<ResTable_overlayable_policy_header>(
             RES_TABLE_OVERLAYABLE_POLICY_TYPE);
-        policy_type->policy_flags =
-            static_cast<PolicyFlags>(util::HostToDevice32(static_cast<uint32_t>(policy_ids.first)));
-        policy_type->entry_count = util::HostToDevice32(static_cast<uint32_t>(
-                                                            policy_ids.second.size()));
+        policy_type->policy_flags = static_cast<PolicyFlags>(
+            android::util::HostToDevice32(static_cast<uint32_t>(policy_ids.first)));
+        policy_type->entry_count =
+            android::util::HostToDevice32(static_cast<uint32_t>(policy_ids.second.size()));
         // Write the ids after the policy header
         auto* id_block = policy_writer.NextBlock<ResTable_ref>(policy_ids.second.size());
         for (const ResourceId& id : policy_ids.second) {
-          id_block->ident = util::HostToDevice32(id.id);
+          id_block->ident = android::util::HostToDevice32(id.id);
           id_block++;
         }
         policy_writer.Finish();
@@ -559,7 +558,7 @@
     // Since the entries are sorted by ID, the last one will be the biggest.
     const size_t num_entries = sorted_entries.back().id.value() + 1;
 
-    spec_header->entryCount = util::HostToDevice32(num_entries);
+    spec_header->entryCount = android::util::HostToDevice32(num_entries);
 
     // Reserve space for the masks of each resource in this type. These
     // show for which configuration axis the resource changes.
@@ -571,17 +570,18 @@
       // Populate the config masks for this entry.
       uint32_t& entry_config_masks = config_masks[entry_id];
       if (entry.visibility.level == Visibility::Level::kPublic) {
-        entry_config_masks |= util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
+        entry_config_masks |= android::util::HostToDevice32(ResTable_typeSpec::SPEC_PUBLIC);
       }
       if (entry.visibility.staged_api) {
-        entry_config_masks |= util::HostToDevice32(ResTable_typeSpec::SPEC_STAGED_API);
+        entry_config_masks |= android::util::HostToDevice32(ResTable_typeSpec::SPEC_STAGED_API);
       }
 
       const size_t config_count = entry.values.size();
       for (size_t i = 0; i < config_count; i++) {
         const ConfigDescription& config = entry.values[i]->config;
         for (size_t j = i + 1; j < config_count; j++) {
-          config_masks[entry_id] |= util::HostToDevice32(config.diff(entry.values[j]->config));
+          config_masks[entry_id] |=
+              android::util::HostToDevice32(config.diff(entry.values[j]->config));
         }
       }
     }
@@ -668,33 +668,33 @@
     const size_t num_entries = (package_.id.value() == 0x00 ? 1 : 0) + shared_libs_->size();
     CHECK(num_entries > 0);
 
-    lib_header->count = util::HostToDevice32(num_entries);
+    lib_header->count = android::util::HostToDevice32(num_entries);
 
     ResTable_lib_entry* lib_entry = buffer->NextBlock<ResTable_lib_entry>(num_entries);
     if (package_.id.value() == 0x00) {
       // Add this package
-      lib_entry->packageId = util::HostToDevice32(0x00);
+      lib_entry->packageId = android::util::HostToDevice32(0x00);
       strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
-                    util::Utf8ToUtf16(package_.name));
+                    android::util::Utf8ToUtf16(package_.name));
       ++lib_entry;
     }
 
     for (auto& map_entry : *shared_libs_) {
-      lib_entry->packageId = util::HostToDevice32(map_entry.first);
+      lib_entry->packageId = android::util::HostToDevice32(map_entry.first);
       strcpy16_htod(lib_entry->packageName, arraysize(lib_entry->packageName),
-                    util::Utf8ToUtf16(map_entry.second));
+                    android::util::Utf8ToUtf16(map_entry.second));
       ++lib_entry;
     }
     lib_writer.Finish();
   }
 
   IAaptContext* context_;
-  IDiagnostics* diag_;
+  android::IDiagnostics* diag_;
   const ResourceTablePackageView package_;
   const std::map<size_t, std::string>* shared_libs_;
   bool use_sparse_entries_;
-  StringPool type_pool_;
-  StringPool key_pool_;
+  android::StringPool type_pool_;
+  android::StringPool key_pool_;
   bool collapse_key_stringpool_;
   const std::set<ResourceName>& name_collapse_exemptions_;
   std::map<uint32_t, uint32_t> aliases_;
@@ -706,26 +706,27 @@
   TRACE_CALL();
   // We must do this before writing the resources, since the string pool IDs may change.
   table->string_pool.Prune();
-  table->string_pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
-    int diff = util::compare(a.priority, b.priority);
-    if (diff == 0) {
-      diff = a.config.compare(b.config);
-    }
-    return diff;
-  });
+  table->string_pool.Sort(
+      [](const android::StringPool::Context& a, const android::StringPool::Context& b) -> int {
+        int diff = util::compare(a.priority, b.priority);
+        if (diff == 0) {
+          diff = a.config.compare(b.config);
+        }
+        return diff;
+      });
 
   // Write the ResTable header.
   const auto& table_view =
       table->GetPartitionedView(ResourceTableViewOptions{.create_alias_entries = true});
   ChunkWriter table_writer(buffer_);
   ResTable_header* table_header = table_writer.StartChunk<ResTable_header>(RES_TABLE_TYPE);
-  table_header->packageCount = util::HostToDevice32(table_view.packages.size());
+  table_header->packageCount = android::util::HostToDevice32(table_view.packages.size());
 
   // Flatten the values string pool.
-  StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool,
-      context->GetDiagnostics());
+  android::StringPool::FlattenUtf8(table_writer.buffer(), table->string_pool,
+                                   context->GetDiagnostics());
 
-  BigBuffer package_buffer(1024);
+  android::BigBuffer package_buffer(1024);
 
   // Flatten each package.
   for (auto& package : table_view.packages) {
@@ -738,7 +739,7 @@
         if (!result.second && result.first->second != package.name) {
           // A mapping for this package ID already exists, and is a different package. Error!
           context->GetDiagnostics()->Error(
-              DiagMessage() << android::base::StringPrintf(
+              android::DiagMessage() << android::base::StringPrintf(
                   "can't map package ID %02x to '%s'. Already mapped to '%s'", package_id,
                   package.name.c_str(), result.first->second.c_str()));
           return false;
diff --git a/tools/aapt2/format/binary/TableFlattener.h b/tools/aapt2/format/binary/TableFlattener.h
index 4360db1..1eec0e4 100644
--- a/tools/aapt2/format/binary/TableFlattener.h
+++ b/tools/aapt2/format/binary/TableFlattener.h
@@ -17,12 +17,11 @@
 #ifndef AAPT_FORMAT_BINARY_TABLEFLATTENER_H
 #define AAPT_FORMAT_BINARY_TABLEFLATTENER_H
 
-#include "android-base/macros.h"
-
 #include "Resource.h"
 #include "ResourceTable.h"
+#include "android-base/macros.h"
+#include "androidfw/BigBuffer.h"
 #include "process/IResourceTableConsumer.h"
-#include "util/BigBuffer.h"
 
 namespace aapt {
 
@@ -51,7 +50,7 @@
 
 class TableFlattener : public IResourceTableConsumer {
  public:
-  explicit TableFlattener(const TableFlattenerOptions& options, BigBuffer* buffer)
+  explicit TableFlattener(const TableFlattenerOptions& options, android::BigBuffer* buffer)
       : options_(options), buffer_(buffer) {
   }
 
@@ -61,7 +60,7 @@
   DISALLOW_COPY_AND_ASSIGN(TableFlattener);
 
   TableFlattenerOptions options_;
-  BigBuffer* buffer_;
+  android::BigBuffer* buffer_;
 };
 
 }  // namespace aapt
diff --git a/tools/aapt2/format/binary/TableFlattener_test.cpp b/tools/aapt2/format/binary/TableFlattener_test.cpp
index c73bbb5..1dd2468 100644
--- a/tools/aapt2/format/binary/TableFlattener_test.cpp
+++ b/tools/aapt2/format/binary/TableFlattener_test.cpp
@@ -45,7 +45,7 @@
 
   ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options,
                                      ResourceTable* table, std::string* out_content) {
-    BigBuffer buffer(1024);
+    android::BigBuffer buffer(1024);
     TableFlattener flattener(options, &buffer);
     if (!flattener.Consume(context, table)) {
       return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
@@ -254,13 +254,13 @@
   // Parse the flattened resource table
   ResChunkPullParser parser(result.data(), result.size());
   ASSERT_TRUE(parser.IsGoodEvent(parser.Next()));
-  ASSERT_EQ(util::DeviceToHost16(parser.chunk()->type), RES_TABLE_TYPE);
+  ASSERT_EQ(android::util::DeviceToHost16(parser.chunk()->type), RES_TABLE_TYPE);
 
   // Retrieve the package of the entry
   ResChunkPullParser table_parser(GetChunkData(parser.chunk()), GetChunkDataLen(parser.chunk()));
   const ResChunk_header* package_chunk = nullptr;
   while (table_parser.IsGoodEvent(table_parser.Next())) {
-    if (util::DeviceToHost16(table_parser.chunk()->type) == RES_TABLE_PACKAGE_TYPE) {
+    if (android::util::DeviceToHost16(table_parser.chunk()->type) == RES_TABLE_PACKAGE_TYPE) {
       package_chunk = table_parser.chunk();
       break;
     }
@@ -272,7 +272,7 @@
                                     GetChunkDataLen(table_parser.chunk()));
   const ResChunk_header* type_chunk = nullptr;
   while (package_parser.IsGoodEvent(package_parser.Next())) {
-    if (util::DeviceToHost16(package_parser.chunk()->type) == RES_TABLE_TYPE_TYPE) {
+    if (android::util::DeviceToHost16(package_parser.chunk()->type) == RES_TABLE_TYPE_TYPE) {
       type_chunk = package_parser.chunk();
       break;
     }
@@ -282,7 +282,7 @@
   ASSERT_NE(type_chunk, nullptr);
   TypeVariant typeVariant((const ResTable_type*) type_chunk);
   auto entry = (const ResTable_map_entry*)*typeVariant.beginEntries();
-  ASSERT_EQ(util::DeviceToHost16(entry->count), 2u);
+  ASSERT_EQ(android::util::DeviceToHost16(entry->count), 2u);
 
   // Check that the value and name of the array entries are correct
   auto values = (const ResTable_map*)(((const uint8_t *)entry) + entry->size);
diff --git a/tools/aapt2/format/binary/XmlFlattener.cpp b/tools/aapt2/format/binary/XmlFlattener.cpp
index cdbe882..983e646 100644
--- a/tools/aapt2/format/binary/XmlFlattener.cpp
+++ b/tools/aapt2/format/binary/XmlFlattener.cpp
@@ -64,11 +64,11 @@
  public:
   using xml::ConstVisitor::Visit;
 
-  StringPool pool;
-  std::map<uint8_t, StringPool> package_pools;
+  android::StringPool pool;
+  std::map<uint8_t, android::StringPool> package_pools;
 
   struct StringFlattenDest {
-    StringPool::Ref ref;
+    android::StringPool::Ref ref;
     ResStringPool_ref* dest;
   };
 
@@ -96,8 +96,8 @@
 
     ChunkWriter writer(buffer_);
     ResXMLTree_node* flat_node = writer.StartChunk<ResXMLTree_node>(RES_XML_CDATA_TYPE);
-    flat_node->lineNumber = util::HostToDevice32(node->line_number);
-    flat_node->comment.index = util::HostToDevice32(-1);
+    flat_node->lineNumber = android::util::HostToDevice32(node->line_number);
+    flat_node->comment.index = android::util::HostToDevice32(-1);
 
     ResXMLTree_cdataExt* flat_text = writer.NextBlock<ResXMLTree_cdataExt>();
     AddString(text, kLowPriority, &flat_text->data);
@@ -116,8 +116,8 @@
       ChunkWriter start_writer(buffer_);
       ResXMLTree_node* flat_node =
           start_writer.StartChunk<ResXMLTree_node>(RES_XML_START_ELEMENT_TYPE);
-      flat_node->lineNumber = util::HostToDevice32(node->line_number);
-      flat_node->comment.index = util::HostToDevice32(-1);
+      flat_node->lineNumber = android::util::HostToDevice32(node->line_number);
+      flat_node->comment.index = android::util::HostToDevice32(-1);
 
       ResXMLTree_attrExt* flat_elem = start_writer.NextBlock<ResXMLTree_attrExt>();
 
@@ -126,8 +126,8 @@
                 true /* treat_empty_string_as_null */);
       AddString(node->name, kLowPriority, &flat_elem->name, true /* treat_empty_string_as_null */);
 
-      flat_elem->attributeStart = util::HostToDevice16(sizeof(*flat_elem));
-      flat_elem->attributeSize = util::HostToDevice16(sizeof(ResXMLTree_attribute));
+      flat_elem->attributeStart = android::util::HostToDevice16(sizeof(*flat_elem));
+      flat_elem->attributeSize = android::util::HostToDevice16(sizeof(ResXMLTree_attribute));
 
       WriteAttributes(node, flat_elem, &start_writer);
 
@@ -140,8 +140,8 @@
       ChunkWriter end_writer(buffer_);
       ResXMLTree_node* flat_end_node =
           end_writer.StartChunk<ResXMLTree_node>(RES_XML_END_ELEMENT_TYPE);
-      flat_end_node->lineNumber = util::HostToDevice32(node->line_number);
-      flat_end_node->comment.index = util::HostToDevice32(-1);
+      flat_end_node->lineNumber = android::util::HostToDevice32(node->line_number);
+      flat_end_node->comment.index = android::util::HostToDevice32(-1);
 
       ResXMLTree_endElementExt* flat_end_elem = end_writer.NextBlock<ResXMLTree_endElementExt>();
       AddString(node->namespace_uri, kLowPriority, &flat_end_elem->ns,
@@ -169,17 +169,17 @@
                  bool treat_empty_string_as_null = false) {
     if (str.empty() && treat_empty_string_as_null) {
       // Some parts of the runtime treat null differently than empty string.
-      dest->index = util::DeviceToHost32(-1);
+      dest->index = android::util::DeviceToHost32(-1);
     } else {
       string_refs.push_back(
-          StringFlattenDest{pool.MakeRef(str, StringPool::Context(priority)), dest});
+          StringFlattenDest{pool.MakeRef(str, android::StringPool::Context(priority)), dest});
     }
   }
 
   // We are adding strings to a StringPool whose strings will be sorted and merged with other
   // string pools. That means we can't encode the ID of a string directly. Instead, we defer the
   // writing of the ID here, until after the StringPool is merged and sorted.
-  void AddString(const StringPool::Ref& ref, android::ResStringPool_ref* dest) {
+  void AddString(const android::StringPool::Ref& ref, android::ResStringPool_ref* dest) {
     string_refs.push_back(StringFlattenDest{ref, dest});
   }
 
@@ -187,8 +187,8 @@
     ChunkWriter writer(buffer_);
 
     ResXMLTree_node* flatNode = writer.StartChunk<ResXMLTree_node>(type);
-    flatNode->lineNumber = util::HostToDevice32(decl.line_number);
-    flatNode->comment.index = util::HostToDevice32(-1);
+    flatNode->lineNumber = android::util::HostToDevice32(decl.line_number);
+    flatNode->comment.index = android::util::HostToDevice32(-1);
 
     ResXMLTree_namespaceExt* flat_ns = writer.NextBlock<ResXMLTree_namespaceExt>();
     AddString(decl.prefix, kLowPriority, &flat_ns->prefix);
@@ -217,7 +217,7 @@
 
     std::sort(filtered_attrs_.begin(), filtered_attrs_.end(), cmp_xml_attribute_by_id);
 
-    flat_elem->attributeCount = util::HostToDevice16(filtered_attrs_.size());
+    flat_elem->attributeCount = android::util::HostToDevice16(filtered_attrs_.size());
 
     ResXMLTree_attribute* flat_attr =
         writer->NextBlock<ResXMLTree_attribute>(filtered_attrs_.size());
@@ -226,12 +226,12 @@
       // Assign the indices for specific attributes.
       if (xml_attr->compiled_attribute && xml_attr->compiled_attribute.value().id &&
           xml_attr->compiled_attribute.value().id.value() == kIdAttr) {
-        flat_elem->idIndex = util::HostToDevice16(attribute_index);
+        flat_elem->idIndex = android::util::HostToDevice16(attribute_index);
       } else if (xml_attr->namespace_uri.empty()) {
         if (xml_attr->name == "class") {
-          flat_elem->classIndex = util::HostToDevice16(attribute_index);
+          flat_elem->classIndex = android::util::HostToDevice16(attribute_index);
         } else if (xml_attr->name == "style") {
-          flat_elem->styleIndex = util::HostToDevice16(attribute_index);
+          flat_elem->styleIndex = android::util::HostToDevice16(attribute_index);
         }
       }
       attribute_index++;
@@ -241,7 +241,7 @@
       AddString(xml_attr->namespace_uri, kLowPriority, &flat_attr->ns,
                 true /* treat_empty_string_as_null */);
 
-      flat_attr->rawValue.index = util::HostToDevice32(-1);
+      flat_attr->rawValue.index = android::util::HostToDevice32(-1);
 
       if (!xml_attr->compiled_attribute || !xml_attr->compiled_attribute.value().id) {
         // The attribute has no associated ResourceID, so the string order doesn't matter.
@@ -256,8 +256,9 @@
         // Lookup the StringPool for this package and make the reference there.
         const xml::AaptAttribute& aapt_attr = xml_attr->compiled_attribute.value();
 
-        StringPool::Ref name_ref = package_pools[aapt_attr.id.value().package_id()].MakeRef(
-            xml_attr->name, StringPool::Context(aapt_attr.id.value().id));
+        android::StringPool::Ref name_ref =
+            package_pools[aapt_attr.id.value().package_id()].MakeRef(
+                xml_attr->name, android::StringPool::Context(aapt_attr.id.value().id));
 
         // Add it to the list of strings to flatten.
         AddString(name_ref, &flat_attr->name);
@@ -298,7 +299,7 @@
         AddString(xml_attr->value, kLowPriority, &flat_attr->rawValue);
       }
 
-      flat_attr->typedValue.size = util::HostToDevice16(sizeof(flat_attr->typedValue));
+      flat_attr->typedValue.size = android::util::HostToDevice16(sizeof(flat_attr->typedValue));
       flat_attr++;
     }
   }
@@ -313,7 +314,7 @@
 }  // namespace
 
 bool XmlFlattener::Flatten(IAaptContext* context, const xml::Node* node) {
-  BigBuffer node_buffer(1024);
+  android::BigBuffer node_buffer(1024);
   XmlFlattenerVisitor visitor(&node_buffer, options_);
   node->Accept(&visitor);
 
@@ -323,13 +324,14 @@
   }
 
   // Sort the string pool so that attribute resource IDs show up first.
-  visitor.pool.Sort([](const StringPool::Context& a, const StringPool::Context& b) -> int {
-    return util::compare(a.priority, b.priority);
-  });
+  visitor.pool.Sort(
+      [](const android::StringPool::Context& a, const android::StringPool::Context& b) -> int {
+        return util::compare(a.priority, b.priority);
+      });
 
   // Now we flatten the string pool references into the correct places.
   for (const auto& ref_entry : visitor.string_refs) {
-    ref_entry.dest->index = util::HostToDevice32(ref_entry.ref.index());
+    ref_entry.dest->index = android::util::HostToDevice32(ref_entry.ref.index());
   }
 
   // Write the XML header.
@@ -338,9 +340,9 @@
 
   // Flatten the StringPool.
   if (options_.use_utf16) {
-    StringPool::FlattenUtf16(buffer_, visitor.pool, context->GetDiagnostics());
+    android::StringPool::FlattenUtf16(buffer_, visitor.pool, context->GetDiagnostics());
   } else {
-    StringPool::FlattenUtf8(buffer_, visitor.pool, context->GetDiagnostics());
+    android::StringPool::FlattenUtf8(buffer_, visitor.pool, context->GetDiagnostics());
   }
 
   {
@@ -353,7 +355,7 @@
         // When we see the first non-resource ID, we're done.
         break;
       }
-      *res_id_map_writer.NextBlock<uint32_t>() = util::HostToDevice32(id.id);
+      *res_id_map_writer.NextBlock<uint32_t>() = android::util::HostToDevice32(id.id);
     }
     res_id_map_writer.Finish();
   }
diff --git a/tools/aapt2/format/binary/XmlFlattener.h b/tools/aapt2/format/binary/XmlFlattener.h
index 1f9e777..e18c1e5 100644
--- a/tools/aapt2/format/binary/XmlFlattener.h
+++ b/tools/aapt2/format/binary/XmlFlattener.h
@@ -18,9 +18,8 @@
 #define AAPT_FORMAT_BINARY_XMLFLATTENER_H
 
 #include "android-base/macros.h"
-
+#include "androidfw/BigBuffer.h"
 #include "process/IResourceTableConsumer.h"
-#include "util/BigBuffer.h"
 #include "xml/XmlDom.h"
 
 namespace aapt {
@@ -36,7 +35,7 @@
 
 class XmlFlattener {
  public:
-  XmlFlattener(BigBuffer* buffer, XmlFlattenerOptions options)
+  XmlFlattener(android::BigBuffer* buffer, XmlFlattenerOptions options)
       : buffer_(buffer), options_(options) {
   }
 
@@ -47,7 +46,7 @@
 
   bool Flatten(IAaptContext* context, const xml::Node* node);
 
-  BigBuffer* buffer_;
+  android::BigBuffer* buffer_;
   XmlFlattenerOptions options_;
 };
 
diff --git a/tools/aapt2/format/binary/XmlFlattener_test.cpp b/tools/aapt2/format/binary/XmlFlattener_test.cpp
index d97e888..6d0022c 100644
--- a/tools/aapt2/format/binary/XmlFlattener_test.cpp
+++ b/tools/aapt2/format/binary/XmlFlattener_test.cpp
@@ -16,11 +16,10 @@
 
 #include "format/binary/XmlFlattener.h"
 
+#include "androidfw/BigBuffer.h"
 #include "androidfw/ResourceTypes.h"
-
 #include "link/Linkers.h"
 #include "test/Test.h"
-#include "util/BigBuffer.h"
 #include "util/Util.h"
 
 using ::aapt::test::StrEq;
@@ -59,13 +58,13 @@
                                      const XmlFlattenerOptions& options = {}) {
     using namespace android;  // For NO_ERROR on windows because it is a macro.
 
-    BigBuffer buffer(1024);
+    android::BigBuffer buffer(1024);
     XmlFlattener flattener(&buffer, options);
     if (!flattener.Consume(context_.get(), doc)) {
       return ::testing::AssertionFailure() << "failed to flatten XML Tree";
     }
 
-    std::unique_ptr<uint8_t[]> data = util::Copy(buffer);
+    std::unique_ptr<uint8_t[]> data = android::util::Copy(buffer);
     if (out_tree->setTo(data.get(), buffer.size(), true) != NO_ERROR) {
       return ::testing::AssertionFailure() << "flattened XML is corrupt";
     }
diff --git a/tools/aapt2/format/proto/ProtoDeserialize.cpp b/tools/aapt2/format/proto/ProtoDeserialize.cpp
index 82c7248..6a1e8c1 100644
--- a/tools/aapt2/format/proto/ProtoDeserialize.cpp
+++ b/tools/aapt2/format/proto/ProtoDeserialize.cpp
@@ -16,15 +16,15 @@
 
 #include "format/proto/ProtoDeserialize.h"
 
-#include "android-base/logging.h"
-#include "android-base/macros.h"
-#include "androidfw/ResourceTypes.h"
-#include "androidfw/Locale.h"
-
 #include "ResourceTable.h"
 #include "ResourceUtils.h"
 #include "ResourceValues.h"
 #include "ValueVisitor.h"
+#include "android-base/logging.h"
+#include "android-base/macros.h"
+#include "androidfw/Locale.h"
+#include "androidfw/ResourceTypes.h"
+#include "androidfw/Util.h"
 
 using ::android::ConfigDescription;
 using ::android::LocaleValue;
@@ -358,8 +358,8 @@
 }
 
 static void DeserializeSourceFromPb(const pb::Source& pb_source, const ResStringPool& src_pool,
-                                    Source* out_source) {
-  out_source->path = util::GetString(src_pool, pb_source.path_idx());
+                                    android::Source* out_source) {
+  out_source->path = android::util::GetString(src_pool, pb_source.path_idx());
   out_source->line = static_cast<size_t>(pb_source.position().line_number());
 }
 
@@ -680,7 +680,7 @@
   if (pb_ref.has_style_string()) {
     out_ref->style_string.str = pb_ref.style_string().str();
     for (const auto& span : pb_ref.style_string().spans()) {
-      out_ref->style_string.spans.emplace_back(Span{
+      out_ref->style_string.spans.emplace_back(android::Span{
           .name = span.name(), .first_char = span.start_index(), .last_char = span.end_index()});
     }
   }
@@ -705,7 +705,7 @@
 static void DeserializeItemMetaDataFromPb(const T& pb_item, const android::ResStringPool& src_pool,
                                           Value* out_value) {
   if (pb_item.has_source()) {
-    Source source;
+    android::Source source;
     DeserializeSourceFromPb(pb_item.source(), src_pool, &source);
     out_value->SetSource(std::move(source));
   }
@@ -733,8 +733,8 @@
 std::unique_ptr<Value> DeserializeValueFromPb(const pb::Value& pb_value,
                                               const android::ResStringPool& src_pool,
                                               const ConfigDescription& config,
-                                              StringPool* value_pool, io::IFileCollection* files,
-                                              std::string* out_error) {
+                                              android::StringPool* value_pool,
+                                              io::IFileCollection* files, std::string* out_error) {
   std::unique_ptr<Value> value;
   if (pb_value.has_item()) {
     value = DeserializeItemFromPb(pb_value.item(), src_pool, config, value_pool, files, out_error);
@@ -774,7 +774,7 @@
           }
 
           if (pb_style.has_parent_source()) {
-            Source parent_source;
+            android::Source parent_source;
             DeserializeSourceFromPb(pb_style.parent_source(), src_pool, &parent_source);
             style->parent.value().SetSource(std::move(parent_source));
           }
@@ -870,7 +870,8 @@
 
 std::unique_ptr<Item> DeserializeItemFromPb(const pb::Item& pb_item,
                                             const android::ResStringPool& src_pool,
-                                            const ConfigDescription& config, StringPool* value_pool,
+                                            const ConfigDescription& config,
+                                            android::StringPool* value_pool,
                                             io::IFileCollection* files, std::string* out_error) {
   switch (pb_item.value_case()) {
     case pb::Item::kRef: {
@@ -960,29 +961,32 @@
 
     case pb::Item::kStr: {
       return util::make_unique<String>(
-          value_pool->MakeRef(pb_item.str().value(), StringPool::Context(config)));
+          value_pool->MakeRef(pb_item.str().value(), android::StringPool::Context(config)));
     } break;
 
     case pb::Item::kRawStr: {
       return util::make_unique<RawString>(
-          value_pool->MakeRef(pb_item.raw_str().value(), StringPool::Context(config)));
+          value_pool->MakeRef(pb_item.raw_str().value(), android::StringPool::Context(config)));
     } break;
 
     case pb::Item::kStyledStr: {
       const pb::StyledString& pb_str = pb_item.styled_str();
-      StyleString style_str{pb_str.value()};
+      android::StyleString style_str{pb_str.value()};
       for (const pb::StyledString::Span& pb_span : pb_str.span()) {
-        style_str.spans.push_back(Span{pb_span.tag(), pb_span.first_char(), pb_span.last_char()});
+        style_str.spans.push_back(
+            android::Span{pb_span.tag(), pb_span.first_char(), pb_span.last_char()});
       }
       return util::make_unique<StyledString>(value_pool->MakeRef(
-          style_str, StringPool::Context(StringPool::Context::kNormalPriority, config)));
+          style_str,
+          android::StringPool::Context(android::StringPool::Context::kNormalPriority, config)));
     } break;
 
     case pb::Item::kFile: {
       const pb::FileReference& pb_file = pb_item.file();
       std::unique_ptr<FileReference> file_ref =
           util::make_unique<FileReference>(value_pool->MakeRef(
-              pb_file.path(), StringPool::Context(StringPool::Context::kHighPriority, config)));
+              pb_file.path(),
+              android::StringPool::Context(android::StringPool::Context::kHighPriority, config)));
       file_ref->type = DeserializeFileReferenceTypeFromPb(pb_file.type());
       if (files != nullptr) {
         file_ref->file = files->FindFile(*file_ref->path);
@@ -1011,8 +1015,8 @@
   return resource;
 }
 
-bool DeserializeXmlFromPb(const pb::XmlNode& pb_node, xml::Element* out_el, StringPool* value_pool,
-                          std::string* out_error) {
+bool DeserializeXmlFromPb(const pb::XmlNode& pb_node, xml::Element* out_el,
+                          android::StringPool* value_pool, std::string* out_error) {
   const pb::XmlElement& pb_el = pb_node.element();
   out_el->name = pb_el.name();
   out_el->namespace_uri = pb_el.namespace_uri();
@@ -1042,7 +1046,7 @@
       if (attr.compiled_value == nullptr) {
         return {};
       }
-      attr.compiled_value->SetSource(Source().WithLine(pb_attr.source().line_number()));
+      attr.compiled_value->SetSource(android::Source().WithLine(pb_attr.source().line_number()));
     }
     out_el->attributes.push_back(std::move(attr));
   }
diff --git a/tools/aapt2/format/proto/ProtoDeserialize.h b/tools/aapt2/format/proto/ProtoDeserialize.h
index 723a1c0..95de3cb 100644
--- a/tools/aapt2/format/proto/ProtoDeserialize.h
+++ b/tools/aapt2/format/proto/ProtoDeserialize.h
@@ -17,16 +17,15 @@
 #ifndef AAPT_FORMAT_PROTO_PROTODESERIALIZE_H
 #define AAPT_FORMAT_PROTO_PROTODESERIALIZE_H
 
-#include "android-base/macros.h"
-#include "androidfw/ConfigDescription.h"
-#include "androidfw/ResourceTypes.h"
-
 #include "Configuration.pb.h"
 #include "ResourceTable.h"
 #include "ResourceValues.h"
 #include "Resources.pb.h"
 #include "ResourcesInternal.pb.h"
-#include "StringPool.h"
+#include "android-base/macros.h"
+#include "androidfw/ConfigDescription.h"
+#include "androidfw/ResourceTypes.h"
+#include "androidfw/StringPool.h"
 #include "io/File.h"
 #include "xml/XmlDom.h"
 
@@ -35,20 +34,20 @@
 std::unique_ptr<Value> DeserializeValueFromPb(const pb::Value& pb_value,
                                               const android::ResStringPool& src_pool,
                                               const android::ConfigDescription& config,
-                                              StringPool* value_pool, io::IFileCollection* files,
-                                              std::string* out_error);
+                                              android::StringPool* value_pool,
+                                              io::IFileCollection* files, std::string* out_error);
 
 std::unique_ptr<Item> DeserializeItemFromPb(const pb::Item& pb_item,
                                             const android::ResStringPool& src_pool,
                                             const android::ConfigDescription& config,
-                                            StringPool* value_pool, io::IFileCollection* files,
-                                            std::string* out_error);
+                                            android::StringPool* value_pool,
+                                            io::IFileCollection* files, std::string* out_error);
 
 std::unique_ptr<xml::XmlResource> DeserializeXmlResourceFromPb(const pb::XmlNode& pb_node,
                                                                std::string* out_error);
 
-bool DeserializeXmlFromPb(const pb::XmlNode& pb_node, xml::Element* out_el, StringPool* value_pool,
-                          std::string* out_error);
+bool DeserializeXmlFromPb(const pb::XmlNode& pb_node, xml::Element* out_el,
+                          android::StringPool* value_pool, std::string* out_error);
 
 bool DeserializeConfigFromPb(const pb::Configuration& pb_config,
                              android::ConfigDescription* out_config, std::string* out_error);
diff --git a/tools/aapt2/format/proto/ProtoSerialize.cpp b/tools/aapt2/format/proto/ProtoSerialize.cpp
index bb8ea0c..163a60a 100644
--- a/tools/aapt2/format/proto/ProtoSerialize.cpp
+++ b/tools/aapt2/format/proto/ProtoSerialize.cpp
@@ -17,7 +17,7 @@
 #include "format/proto/ProtoSerialize.h"
 
 #include "ValueVisitor.h"
-#include "util/BigBuffer.h"
+#include "androidfw/BigBuffer.h"
 
 using android::ConfigDescription;
 
@@ -25,22 +25,24 @@
 
 namespace aapt {
 
-void SerializeStringPoolToPb(const StringPool& pool, pb::StringPool* out_pb_pool, IDiagnostics* diag) {
-  BigBuffer buffer(1024);
-  StringPool::FlattenUtf8(&buffer, pool, diag);
+void SerializeStringPoolToPb(const android::StringPool& pool, pb::StringPool* out_pb_pool,
+                             android::IDiagnostics* diag) {
+  android::BigBuffer buffer(1024);
+  android::StringPool::FlattenUtf8(&buffer, pool, diag);
 
   std::string* data = out_pb_pool->mutable_data();
   data->reserve(buffer.size());
 
   size_t offset = 0;
-  for (const BigBuffer::Block& block : buffer) {
+  for (const android::BigBuffer::Block& block : buffer) {
     data->insert(data->begin() + offset, block.buffer.get(), block.buffer.get() + block.size);
     offset += block.size;
   }
 }
 
-void SerializeSourceToPb(const Source& source, StringPool* src_pool, pb::Source* out_pb_source) {
-  StringPool::Ref ref = src_pool->MakeRef(source.path);
+void SerializeSourceToPb(const android::Source& source, android::StringPool* src_pool,
+                         pb::Source* out_pb_source) {
+  android::StringPool::Ref ref = src_pool->MakeRef(source.path);
   out_pb_source->set_path_idx(static_cast<uint32_t>(ref.index()));
   if (source.line) {
     out_pb_source->mutable_position()->set_line_number(static_cast<uint32_t>(source.line.value()));
@@ -276,7 +278,7 @@
 
 static void SerializeOverlayableItemToPb(const OverlayableItem& overlayable_item,
                                          std::vector<Overlayable*>& serialized_overlayables,
-                                         StringPool* source_pool, pb::Entry* pb_entry,
+                                         android::StringPool* source_pool, pb::Entry* pb_entry,
                                          pb::ResourceTable* pb_table) {
   // Retrieve the index of the overlayable in the list of groups that have already been serialized.
   size_t i;
@@ -337,8 +339,8 @@
 }
 
 void SerializeTableToPb(const ResourceTable& table, pb::ResourceTable* out_table,
-                        IDiagnostics* diag, SerializeTableOptions options) {
-  auto source_pool = (options.exclude_sources) ? nullptr : util::make_unique<StringPool>();
+                        android::IDiagnostics* diag, SerializeTableOptions options) {
+  auto source_pool = (options.exclude_sources) ? nullptr : util::make_unique<android::StringPool>();
 
   pb::ToolFingerprint* pb_fingerprint = out_table->add_tool_fingerprint();
   pb_fingerprint->set_tool(util::GetToolName());
@@ -482,7 +484,7 @@
 }
 
 template <typename T>
-static void SerializeItemMetaDataToPb(const Item& item, T* pb_item, StringPool* src_pool) {
+static void SerializeItemMetaDataToPb(const Item& item, T* pb_item, android::StringPool* src_pool) {
   if (src_pool != nullptr) {
     SerializeSourceToPb(item.GetSource(), src_pool, pb_item->mutable_source());
   }
@@ -526,7 +528,7 @@
  public:
   using ConstValueVisitor::Visit;
 
-  ValueSerializer(pb::Value* out_value, StringPool* src_pool)
+  ValueSerializer(pb::Value* out_value, android::StringPool* src_pool)
       : out_value_(out_value), src_pool_(src_pool) {
   }
 
@@ -545,7 +547,7 @@
   void Visit(const StyledString* str) override {
     pb::StyledString* pb_str = out_value_->mutable_item()->mutable_styled_str();
     pb_str->set_value(str->value->value);
-    for (const StringPool::Span& span : str->value->spans) {
+    for (const android::StringPool::Span& span : str->value->spans) {
       pb::StyledString::Span* pb_span = pb_str->add_span();
       pb_span->set_tag(*span.name);
       pb_span->set_first_char(span.first_char);
@@ -693,12 +695,12 @@
 
  private:
   pb::Value* out_value_;
-  StringPool* src_pool_;
+  android::StringPool* src_pool_;
 };
 
 }  // namespace
 
-void SerializeValueToPb(const Value& value, pb::Value* out_value, StringPool* src_pool) {
+void SerializeValueToPb(const Value& value, pb::Value* out_value, android::StringPool* src_pool) {
   ValueSerializer serializer(out_value, src_pool);
   value.Accept(&serializer);
 
diff --git a/tools/aapt2/format/proto/ProtoSerialize.h b/tools/aapt2/format/proto/ProtoSerialize.h
index b0d5630..b0a70d9 100644
--- a/tools/aapt2/format/proto/ProtoSerialize.h
+++ b/tools/aapt2/format/proto/ProtoSerialize.h
@@ -17,15 +17,14 @@
 #ifndef AAPT_FORMAT_PROTO_PROTOSERIALIZE_H
 #define AAPT_FORMAT_PROTO_PROTOSERIALIZE_H
 
-#include "android-base/macros.h"
-#include "androidfw/ConfigDescription.h"
-
 #include "Configuration.pb.h"
 #include "ResourceTable.h"
 #include "ResourceValues.h"
 #include "Resources.pb.h"
 #include "ResourcesInternal.pb.h"
-#include "StringPool.h"
+#include "android-base/macros.h"
+#include "androidfw/ConfigDescription.h"
+#include "androidfw/StringPool.h"
 #include "xml/XmlDom.h"
 
 namespace aapt {
@@ -51,7 +50,8 @@
 
 // Serializes a Value to its protobuf representation. An optional StringPool will hold the
 // source path string.
-void SerializeValueToPb(const Value& value, pb::Value* out_value, StringPool* src_pool = nullptr);
+void SerializeValueToPb(const Value& value, pb::Value* out_value,
+                        android::StringPool* src_pool = nullptr);
 
 // Serialize an Item into its protobuf representation. pb::Item does not store the source path nor
 // comments of an Item.
@@ -67,14 +67,15 @@
 
 // Serializes a StringPool into its protobuf representation, which is really just the binary
 // ResStringPool representation stuffed into a bytes field.
-void SerializeStringPoolToPb(const StringPool& pool, pb::StringPool* out_pb_pool, IDiagnostics* diag);
+void SerializeStringPoolToPb(const android::StringPool& pool, pb::StringPool* out_pb_pool,
+                             android::IDiagnostics* diag);
 
 // Serializes a ConfigDescription into its protobuf representation.
 void SerializeConfig(const android::ConfigDescription& config, pb::Configuration* out_pb_config);
 
 // Serializes a ResourceTable into its protobuf representation.
 void SerializeTableToPb(const ResourceTable& table, pb::ResourceTable* out_table,
-                        IDiagnostics* diag, SerializeTableOptions options = {});
+                        android::IDiagnostics* diag, SerializeTableOptions options = {});
 
 // Serializes a ResourceFile into its protobuf representation.
 void SerializeCompiledFileToPb(const ResourceFile& file, pb::internal::CompiledFile* out_file);
diff --git a/tools/aapt2/format/proto/ProtoSerialize_test.cpp b/tools/aapt2/format/proto/ProtoSerialize_test.cpp
index 0247021..692fa42 100644
--- a/tools/aapt2/format/proto/ProtoSerialize_test.cpp
+++ b/tools/aapt2/format/proto/ProtoSerialize_test.cpp
@@ -127,9 +127,9 @@
                                  context->GetDiagnostics()));
 
   // Make a styled string.
-  StyleString style_string;
+  android::StyleString style_string;
   style_string.str = "hello";
-  style_string.spans.push_back(Span{"b", 0u, 4u});
+  style_string.spans.push_back(android::Span{"b", 0u, 4u});
   ASSERT_TRUE(table->AddResource(
       NewResourceBuilder(test::ParseNameOrDie("com.app.a:string/styled"))
           .SetValue(util::make_unique<StyledString>(table->string_pool.MakeRef(style_string)))
@@ -164,8 +164,8 @@
 
   // Make an overlayable resource.
   OverlayableItem overlayable_item(std::make_shared<Overlayable>(
-      "OverlayableName", "overlay://theme", Source("res/values/overlayable.xml", 40)));
-  overlayable_item.source = Source("res/values/overlayable.xml", 42);
+      "OverlayableName", "overlay://theme", android::Source("res/values/overlayable.xml", 40)));
+  overlayable_item.source = android::Source("res/values/overlayable.xml", 42);
   ASSERT_TRUE(
       table->AddResource(NewResourceBuilder(test::ParseNameOrDie("com.app.a:integer/overlayable"))
                              .SetOverlayable(overlayable_item)
@@ -271,7 +271,7 @@
   attr.compiled_attribute = xml::AaptAttribute(Attribute{}, ResourceId(0x01010000));
   attr.compiled_value =
       ResourceUtils::TryParseItemForAttribute(attr.value, android::ResTable_map::TYPE_DIMENSION);
-  attr.compiled_value->SetSource(Source().WithLine(25));
+  attr.compiled_value->SetSource(android::Source().WithLine(25));
   element.attributes.push_back(std::move(attr));
 
   std::unique_ptr<xml::Text> text = util::make_unique<xml::Text>();
@@ -292,7 +292,7 @@
   pb::XmlNode pb_xml;
   SerializeXmlToPb(element, &pb_xml);
 
-  StringPool pool;
+  android::StringPool pool;
   xml::Element actual_el;
   std::string error;
   ASSERT_TRUE(DeserializeXmlFromPb(pb_xml, &actual_el, &pool, &error));
@@ -365,7 +365,7 @@
   options.remove_empty_text_nodes = true;
   SerializeXmlToPb(element, &pb_xml, options);
 
-  StringPool pool;
+  android::StringPool pool;
   xml::Element actual_el;
   std::string error;
   ASSERT_TRUE(DeserializeXmlFromPb(pb_xml, &actual_el, &pool, &error));
@@ -898,7 +898,8 @@
   auto original = std::make_unique<Macro>();
   original->raw_value = "\nThis being human is a guest house.";
   original->style_string.str = " This being human is a guest house.";
-  original->style_string.spans.emplace_back(Span{.name = "b", .first_char = 12, .last_char = 16});
+  original->style_string.spans.emplace_back(
+      android::Span{.name = "b", .first_char = 12, .last_char = 16});
   original->untranslatable_sections.emplace_back(UntranslatableSection{.start = 12, .end = 17});
   original->alias_namespaces.emplace_back(
       Macro::Namespace{.alias = "prefix", .package_name = "package.name", .is_private = true});