Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5d275512 authored by Ryan Mitchell's avatar Ryan Mitchell
Browse files

AAPT2: Reformatted dump command invocations

Use with:
  aapt2 dump apc [apc]
  aapt2 dump configurations [apk]
  aapt2 dump strings [apk]
  aapt2 dump resources [apk]
  aapt2 dump xmlstrings [apk] --file [file]
  aapt2 dump xmltree [apk] --file [file]

Will add permissions and badging in a later commit.

Bug: 73351292
Test: Manual tests of the commands
Change-Id: I97eec01222af14053a98bd70255f1bfecd16b1c4
parent 1a8a6907
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -408,6 +408,41 @@ void Debug::DumpHex(const void* data, size_t len) {
  }
}

void Debug::DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer) {
  using namespace android;
  
  if (pool->getError() == NO_INIT) {
    printer->Print("String pool is unitialized.\n");
    return;
  } else if (pool->getError() != NO_ERROR) {
    printer->Print("String pool is corrupt/invalid.\n");
    return;
  }

  SortedVector<const void*> uniqueStrings;
  const size_t N = pool->size();
  for (size_t i=0; i<N; i++) {
    size_t len;
    if (pool->isUTF8()) {
      uniqueStrings.add(pool->string8At(i, &len));
    } else {
      uniqueStrings.add(pool->stringAt(i, &len));
    }
  }

  printer->Print(StringPrintf("String pool of %zd unique %s %s strings, %zd entries and %zd styles "
                              "using %zd bytes:\n", uniqueStrings.size(),
                              pool->isUTF8() ? "UTF-8" : "UTF-16",
                              pool->isSorted() ? "sorted" : "non-sorted", N, pool->styleCount(),
                              pool->bytes()));

  const size_t NS = pool->size();
  for (size_t s=0; s<NS; s++) {
    String8 str = pool->string8ObjectAt(s);
    printer->Print(StringPrintf("String #%zd : %s\n", s, str.string()));
  }
}

namespace {

class XmlPrinter : public xml::ConstVisitor {
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ struct Debug {
  static void PrintStyleGraph(ResourceTable* table, const ResourceName& target_style);
  static void DumpHex(const void* data, size_t len);
  static void DumpXml(const xml::XmlResource& doc, text::Printer* printer);
  static void DumpResStringPool(const android::ResStringPool* pool, text::Printer* printer);
};

}  // namespace aapt
+47 −0
Original line number Diff line number Diff line
@@ -257,6 +257,53 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table
  return true;
}

std::unique_ptr<xml::XmlResource> LoadedApk::LoadXml(const std::string& file_path,
                                                     IDiagnostics* diag) {
  io::IFile* file = apk_->FindFile(file_path);
  if (file == nullptr) {
    diag->Error(DiagMessage() << "failed to find file");
    return nullptr;
  }

  std::unique_ptr<xml::XmlResource> doc;
  if (format_ == ApkFormat::kProto) {
    std::unique_ptr<io::InputStream> in = file->OpenInputStream();
    if (!in) {
      diag->Error(DiagMessage() << "failed to open file");
      return nullptr;
    }

    io::ZeroCopyInputAdaptor adaptor(in.get());
    pb::XmlNode pb_node;
    if (!pb_node.ParseFromZeroCopyStream(&adaptor)) {
      diag->Error(DiagMessage() << "failed to parse file as proto XML");
      return nullptr;
    }

    std::string err;
    doc = DeserializeXmlResourceFromPb(pb_node, &err);
    if (!doc) {
      diag->Error(DiagMessage() << "failed to deserialize proto XML: " << err);
      return nullptr;
    }
  } else if (format_ == ApkFormat::kBinary) {
    std::unique_ptr<io::IData> data = file->OpenAsData();
    if (!data) {
      diag->Error(DiagMessage() << "failed to open file");
      return nullptr;
    }

    std::string err;
    doc = xml::Inflate(data->data(), data->size(), &err);
    if (!doc) {
      diag->Error(DiagMessage() << "failed to parse file as binary XML: " << err);
      return nullptr;
    }
  }

  return doc;
}

ApkFormat LoadedApk::DetermineApkFormat(io::IFileCollection* apk) {
  if (apk->FindFile(kApkResourceTablePath) != nullptr) {
    return ApkFormat::kBinary;
+6 −0
Original line number Diff line number Diff line
@@ -70,6 +70,10 @@ class LoadedApk {
    return apk_.get();
  }

  ApkFormat GetApkFormat() {
    return format_;
  }

  const ResourceTable* GetResourceTable() const {
    return table_.get();
  }
@@ -106,6 +110,8 @@ class LoadedApk {
                              const TableFlattenerOptions& options, FilterChain* filters,
                              IArchiveWriter* writer, xml::XmlResource* manifest = nullptr);

  /** Loads the file as an xml document. */
  std::unique_ptr<xml::XmlResource> LoadXml(const std::string& file_path, IDiagnostics* diag);

 private:
  DISALLOW_COPY_AND_ASSIGN(LoadedApk);
+1 −1
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ class MainCommand : public Command {
  explicit MainCommand(IDiagnostics* diagnostics) : Command("aapt2"), diagnostics_(diagnostics) {
    AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics));
    AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics));
    AddOptionalSubcommand(util::make_unique<DumpCommand>());
    AddOptionalSubcommand(util::make_unique<DumpCommand>(diagnostics));
    AddOptionalSubcommand(util::make_unique<DiffCommand>());
    AddOptionalSubcommand(util::make_unique<OptimizeCommand>());
    AddOptionalSubcommand(util::make_unique<ConvertCommand>());
Loading