diff options
author | 2018-09-19 16:57:01 -0700 | |
---|---|---|
committer | 2018-10-09 10:51:25 -0700 | |
commit | 214846df59118ebe6b75706ea3505a8bb3b2f93c (patch) | |
tree | 993d34675e462c8e535b8b8ee543c7b22328e4fe | |
parent | 99ecc121bca092afb2ace650f879f437c9d3ff9b (diff) |
Created resuable DumpApkCommand and added "badger"
This change refactors the dump commands to inherit from a base
DumpApkCommand and adds a command that prints out an ASCII
image of a badger if the user wrote "badger" instead of
"badging". The command is hidden from the help menu.
Bug: 73535002
Test: manual
Change-Id: I9bdd8a7bbf6a4282c4933e5c478f6d1d8e32d99e
-rw-r--r-- | tools/aapt2/Main.cpp | 28 | ||||
-rw-r--r-- | tools/aapt2/cmd/Command.cpp | 16 | ||||
-rw-r--r-- | tools/aapt2/cmd/Command.h | 3 | ||||
-rw-r--r-- | tools/aapt2/cmd/Dump.cpp | 440 | ||||
-rw-r--r-- | tools/aapt2/cmd/Dump.h | 218 | ||||
-rw-r--r-- | tools/aapt2/dump/DumpManifest.cpp | 353 | ||||
-rw-r--r-- | tools/aapt2/dump/DumpManifest.h | 46 |
7 files changed, 606 insertions, 498 deletions
diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp index 37013c07ffef..adf85b0ea8e8 100644 --- a/tools/aapt2/Main.cpp +++ b/tools/aapt2/Main.cpp @@ -36,6 +36,7 @@ #include "cmd/Dump.h" #include "cmd/Link.h" #include "cmd/Optimize.h" +#include "io/FileStream.h" #include "util/Files.h" #include "util/Util.h" @@ -68,10 +69,11 @@ class VersionCommand : public Command { /** The main entry point of AAPT. */ class MainCommand : public Command { public: - explicit MainCommand(IDiagnostics* diagnostics) : Command("aapt2"), diagnostics_(diagnostics) { + explicit MainCommand(text::Printer* printer, IDiagnostics* diagnostics) + : Command("aapt2"), diagnostics_(diagnostics) { AddOptionalSubcommand(util::make_unique<CompileCommand>(diagnostics)); AddOptionalSubcommand(util::make_unique<LinkCommand>(diagnostics)); - AddOptionalSubcommand(util::make_unique<DumpCommand>(diagnostics)); + AddOptionalSubcommand(util::make_unique<DumpCommand>(printer, diagnostics)); AddOptionalSubcommand(util::make_unique<DiffCommand>()); AddOptionalSubcommand(util::make_unique<OptimizeCommand>()); AddOptionalSubcommand(util::make_unique<ConvertCommand>()); @@ -101,13 +103,14 @@ class MainCommand : public Command { */ class DaemonCommand : public Command { public: - explicit DaemonCommand(IDiagnostics* diagnostics) : Command("daemon", "m"), - diagnostics_(diagnostics) { + explicit DaemonCommand(io::FileOutputStream* out, IDiagnostics* diagnostics) + : Command("daemon", "m"), out_(out), diagnostics_(diagnostics) { SetDescription("Runs aapt in daemon mode. Each subsequent line is a single parameter to the\n" "command. The end of an invocation is signaled by providing an empty line."); } int Action(const std::vector<std::string>& /* args */) override { + text::Printer printer(out_); std::cout << "Ready" << std::endl; while (true) { @@ -132,7 +135,9 @@ class DaemonCommand : public Command { std::vector<StringPiece> args; args.insert(args.end(), raw_args.begin(), raw_args.end()); - if (MainCommand(diagnostics_).Execute(args, &std::cerr) != 0) { + int result = MainCommand(&printer, diagnostics_).Execute(args, &std::cerr); + out_->Flush(); + if (result != 0) { std::cerr << "Error" << std::endl; } std::cerr << "Done" << std::endl; @@ -143,6 +148,7 @@ class DaemonCommand : public Command { } private: + io::FileOutputStream* out_; IDiagnostics* diagnostics_; }; @@ -159,11 +165,17 @@ int MainImpl(int argc, char** argv) { args.push_back(argv[i]); } - // Add the daemon subcommand here so it cannot be called while executing the daemon + // Use a smaller buffer so that there is less latency for printing to stdout. + constexpr size_t kStdOutBufferSize = 1024u; + aapt::io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); + aapt::text::Printer printer(&fout); + aapt::StdErrDiagnostics diagnostics; - auto main_command = new aapt::MainCommand(&diagnostics); - main_command->AddOptionalSubcommand(aapt::util::make_unique<aapt::DaemonCommand>(&diagnostics)); + auto main_command = new aapt::MainCommand(&printer, &diagnostics); + // Add the daemon subcommand here so it cannot be called while executing the daemon + main_command->AddOptionalSubcommand( + aapt::util::make_unique<aapt::DaemonCommand>(&fout, &diagnostics)); return main_command->Execute(args, &std::cerr); } diff --git a/tools/aapt2/cmd/Command.cpp b/tools/aapt2/cmd/Command.cpp index 09411b95bcfe..bdee5c9d4909 100644 --- a/tools/aapt2/cmd/Command.cpp +++ b/tools/aapt2/cmd/Command.cpp @@ -93,9 +93,13 @@ void Command::AddOptionalSwitch(const StringPiece& name, flags_.push_back(Flag{name.to_string(), description.to_string(), func, false, 0, false}); } -void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand) { +void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental) { subcommand->fullname_ = name_ + " " + subcommand->name_; - subcommands_.push_back(std::move(subcommand)); + if (experimental) { + experimental_subcommands_.push_back(std::move(subcommand)); + } else { + subcommands_.push_back(std::move(subcommand)); + } } void Command::SetDescription(const android::StringPiece& description) { @@ -162,7 +166,7 @@ int Command::Execute(const std::vector<android::StringPiece>& args, std::ostream for (size_t i = 0; i < args.size(); i++) { StringPiece arg = args[i]; if (*(arg.data()) != '-') { - // Continue parsing as the sub command if the first argument matches one of the subcommands + // Continue parsing as the subcommand if the first argument matches one of the subcommands if (i == 0) { for (auto& subcommand : subcommands_) { if (arg == subcommand->name_ || arg==subcommand->short_name_) { @@ -170,6 +174,12 @@ int Command::Execute(const std::vector<android::StringPiece>& args, std::ostream std::vector<android::StringPiece>(args.begin() + 1, args.end()), out_error); } } + for (auto& subcommand : experimental_subcommands_) { + if (arg == subcommand->name_ || arg==subcommand->short_name_) { + return subcommand->Execute( + std::vector<android::StringPiece>(args.begin() + 1, args.end()), out_error); + } + } } file_args.push_back(arg.to_string()); diff --git a/tools/aapt2/cmd/Command.h b/tools/aapt2/cmd/Command.h index 71dc6fe48da1..16949883d123 100644 --- a/tools/aapt2/cmd/Command.h +++ b/tools/aapt2/cmd/Command.h @@ -49,7 +49,7 @@ class Command { const android::StringPiece& description, std::unordered_set<std::string>* value); void AddOptionalSwitch(const android::StringPiece& name, const android::StringPiece& description, bool* value); - void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand); + void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental = false); void SetDescription(const android::StringPiece& name); @@ -83,6 +83,7 @@ class Command { std::string fullname_; std::vector<Flag> flags_; std::vector<std::unique_ptr<Command>> subcommands_; + std::vector<std::unique_ptr<Command>> experimental_subcommands_; }; } // namespace aapt diff --git a/tools/aapt2/cmd/Dump.cpp b/tools/aapt2/cmd/Dump.cpp index d80b5ea38bbd..06e4622d949a 100644 --- a/tools/aapt2/cmd/Dump.cpp +++ b/tools/aapt2/cmd/Dump.cpp @@ -125,9 +125,6 @@ class DumpContext : public IAaptContext { } // namespace -// Use a smaller buffer so that there is less latency for dumping to stdout. -constexpr size_t kStdOutBufferSize = 1024u; - int DumpAPCCommand::Action(const std::vector<std::string>& args) { DumpContext context; DebugPrintTableOptions print_options; @@ -135,41 +132,41 @@ int DumpAPCCommand::Action(const std::vector<std::string>& args) { print_options.show_values = !no_values_; if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump container specified."); + diag_->Error(DiagMessage() << "No dump container specified"); return 1; } - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - + bool error = false; for (auto container : args) { io::FileInputStream input(container); if (input.HadError()) { context.GetDiagnostics()->Error(DiagMessage(container) - << "failed to open file: " << input.GetError()); - return false; + << "failed to open file: " << input.GetError()); + error = true; + continue; } // Try as a compiled file. ContainerReader reader(&input); if (reader.HadError()) { context.GetDiagnostics()->Error(DiagMessage(container) - << "failed to read container: " << reader.GetError()); - return false; + << "failed to read container: " << reader.GetError()); + error = true; + continue; } - printer.Println("AAPT2 Container (APC)"); + printer_->Println("AAPT2 Container (APC)"); ContainerReaderEntry* entry; std::string error; while ((entry = reader.Next()) != nullptr) { if (entry->Type() == ContainerEntryType::kResTable) { - printer.Println("kResTable"); + printer_->Println("kResTable"); pb::ResourceTable pb_table; if (!entry->GetResTable(&pb_table)) { context.GetDiagnostics()->Error(DiagMessage(container) - << "failed to parse proto table: " - << entry->GetError()); + << "failed to parse proto table: " << entry->GetError()); + error = true; continue; } @@ -177,65 +174,61 @@ int DumpAPCCommand::Action(const std::vector<std::string>& args) { error.clear(); if (!DeserializeTableFromPb(pb_table, nullptr /*files*/, &table, &error)) { context.GetDiagnostics()->Error(DiagMessage(container) - << "failed to parse table: " << error); + << "failed to parse table: " << error); + error = true; continue; } - printer.Indent(); - Debug::PrintTable(table, print_options, &printer); - printer.Undent(); + printer_->Indent(); + Debug::PrintTable(table, print_options, printer_); + printer_->Undent(); } else if (entry->Type() == ContainerEntryType::kResFile) { - printer.Println("kResFile"); + printer_->Println("kResFile"); pb::internal::CompiledFile pb_compiled_file; off64_t offset; size_t length; if (!entry->GetResFileOffsets(&pb_compiled_file, &offset, &length)) { - context.GetDiagnostics()->Error( - DiagMessage(container) << "failed to parse compiled proto file: " - << entry->GetError()); + context.GetDiagnostics()->Error(DiagMessage(container) + << "failed to parse compiled proto file: " + << entry->GetError()); + error = true; continue; } ResourceFile file; if (!DeserializeCompiledFileFromPb(pb_compiled_file, &file, &error)) { context.GetDiagnostics()->Warn(DiagMessage(container) - << "failed to parse compiled file: " << error); + << "failed to parse compiled file: " << error); + error = true; continue; } - printer.Indent(); - DumpCompiledFile(file, Source(container), offset, length, &printer); - printer.Undent(); + printer_->Indent(); + DumpCompiledFile(file, Source(container), offset, length, printer_); + printer_->Undent(); } } } - return 0; + return (error) ? 1 : 0; } -int DumpConfigsCommand::Action(const std::vector<std::string>& args) { - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); - return 1; - } - - auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); - if (!loaded_apk) { - return 1; - } +int DumpBadgerCommand::Action(const std::vector<std::string>& args) { + printer_->Print(StringPrintf("%s", kBadgerData)); + printer_->Print("Did you mean \"aapt2 dump badging\"?\n"); + return 1; +} - ResourceTable* table = loaded_apk->GetResourceTable(); +int DumpConfigsCommand::Dump(LoadedApk* apk) { + ResourceTable* table = apk->GetResourceTable(); if (!table) { - diag_->Error(DiagMessage() << "Failed to retrieve resource table."); + GetDiagnostics()->Error(DiagMessage() << "Failed to retrieve resource table"); return 1; } - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - // Comparison function used to order configurations auto compare = [](android::ConfigDescription c1, android::ConfigDescription c2) -> bool { - return c1.compare(c2) < 0; + return c1.compare(c2) < 0; }; // Insert the configurations into a set in order to keep every configuarion seen @@ -252,132 +245,75 @@ int DumpConfigsCommand::Action(const std::vector<std::string>& args) { // Print the configurations in order for (auto& config : configs) { - printer.Print(StringPrintf("%s\n", config.to_string().data())); + GetPrinter()->Print(StringPrintf("%s\n", config.to_string().data())); } - return 0; } -int DumpStringsCommand::Action(const std::vector<std::string>& args) { - DumpContext context; - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); +int DumpPackageNameCommand::Dump(LoadedApk* apk) { + xml::Element* manifest_el = apk->GetManifest()->root.get(); + if (!manifest_el) { + GetDiagnostics()->Error(DiagMessage() << "No AndroidManifest"); return 1; } - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - - for (auto apk : args) { - auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); - if (!loaded_apk) { - return 1; - } - - ResourceTable* table = loaded_apk->GetResourceTable(); - if (!table) { - diag_->Error(DiagMessage() << "Failed to retrieve resource table."); - return 1; - } - - // Load the run-time xml string pool using the flattened data - BigBuffer buffer(4096); - StringPool::FlattenUtf8(&buffer, table->string_pool, context.GetDiagnostics()); - auto data = buffer.to_string(); - android::ResStringPool pool(data.data(), data.size(), false); - Debug::DumpResStringPool(&pool, &printer); + xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); + if (!attr) { + GetDiagnostics()->Error(DiagMessage() << "No package name"); + return 1; } + GetPrinter()->Println(StringPrintf("%s", attr->value.c_str())); return 0; } -int DumpTableCommand::Action(const std::vector<std::string>& args) { - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); +int DumpStringsCommand::Dump(LoadedApk* apk) { + ResourceTable* table = apk->GetResourceTable(); + if (!table) { + GetDiagnostics()->Error(DiagMessage() << "Failed to retrieve resource table"); return 1; } - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - - DebugPrintTableOptions print_options; - print_options.show_sources = true; - print_options.show_values = !no_values_; - - for (auto apk : args) { - auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); - if (!loaded_apk) { - return 1; - } - - if (loaded_apk->GetApkFormat() == ApkFormat::kProto) { - printer.Println("Proto APK"); - } else { - printer.Println("Binary APK"); - } - - ResourceTable* table = loaded_apk->GetResourceTable(); - if (!table) { - diag_->Error(DiagMessage() << "Failed to retrieve resource table."); - return 1; - } - - Debug::PrintTable(*table, print_options, &printer); - } - + // Load the run-time xml string pool using the flattened data + BigBuffer buffer(4096); + StringPool::FlattenUtf8(&buffer, table->string_pool, GetDiagnostics()); + auto data = buffer.to_string(); + android::ResStringPool pool(data.data(), data.size(), false); + Debug::DumpResStringPool(&pool, GetPrinter()); return 0; } -int DumpXmlTreeCommand::Action(const std::vector<std::string>& args) { - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified"); - return 1; +int DumpTableCommand::Dump(LoadedApk* apk) { + if (apk->GetApkFormat() == ApkFormat::kProto) { + GetPrinter()->Println("Proto APK"); + } else { + GetPrinter()->Println("Binary APK"); } - auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); - if (!loaded_apk) { + ResourceTable* table = apk->GetResourceTable(); + if (!table) { + GetDiagnostics()->Error(DiagMessage() << "Failed to retrieve resource table"); return 1; } - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - - // Dump the xml tree of every passed in file - for (auto file : files_) { - auto xml = loaded_apk->LoadXml(file, diag_); - if (!xml) { - return 1; - } - - Debug::DumpXml(*xml, &printer); - } - + DebugPrintTableOptions print_options; + print_options.show_sources = true; + print_options.show_values = !no_values_; + Debug::PrintTable(*table, print_options, GetPrinter()); return 0; } -int DumpXmlStringsCommand::Action(const std::vector<std::string>& args) { +int DumpXmlStringsCommand::Dump(LoadedApk* apk) { DumpContext context; - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); - return 1; - } - - auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); - if (!loaded_apk) { - return 1; - } - - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - - // Dump the xml strings of every passed in file + bool error = false; for (auto xml_file : files_) { android::ResXMLTree tree; - if (loaded_apk->GetApkFormat() == ApkFormat::kProto) { - auto xml = loaded_apk->LoadXml(xml_file, diag_); + if (apk->GetApkFormat() == ApkFormat::kProto) { + auto xml = apk->LoadXml(xml_file, GetDiagnostics()); if (!xml) { - return 1; + error = true; + continue; } // Flatten the xml document to get a binary representation of the proto xml file @@ -386,76 +322,208 @@ int DumpXmlStringsCommand::Action(const std::vector<std::string>& args) { options.keep_raw_values = true; XmlFlattener flattener(&buffer, options); if (!flattener.Consume(&context, xml.get())) { - return 1; + error = true; + continue; } // Load the run-time xml tree using the flattened data std::string data = buffer.to_string(); tree.setTo(data.data(), data.size(), /** copyData */ true); - } else if (loaded_apk->GetApkFormat() == ApkFormat::kBinary) { - io::IFile* file = loaded_apk->GetFileCollection()->FindFile(xml_file); + } else if (apk->GetApkFormat() == ApkFormat::kBinary) { + io::IFile* file = apk->GetFileCollection()->FindFile(xml_file); if (!file) { - diag_->Error(DiagMessage(xml_file) << "file '" << xml_file << "' not found in APK"); - return 1; + GetDiagnostics()->Error(DiagMessage(xml_file) + << "File '" << xml_file << "' not found in APK"); + error = true; + continue; } std::unique_ptr<io::IData> data = file->OpenAsData(); if (!data) { - diag_->Error(DiagMessage() << "failed to open file"); - return 1; + GetDiagnostics()->Error(DiagMessage() << "Failed to open " << xml_file); + error = true; + continue; } // Load the run-time xml tree from the file data tree.setTo(data->data(), data->size(), /** copyData */ true); + } else { + GetDiagnostics()->Error(DiagMessage(apk->GetSource()) << "Unknown APK format"); + error = true; + continue; } - Debug::DumpResStringPool(&tree.getStrings(), &printer); + Debug::DumpResStringPool(&tree.getStrings(), GetPrinter()); } - - return 0; + return (error) ? 1 : 0; } -int DumpPackageNameCommand::Action(const std::vector<std::string>& args) { - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); - return 1; - } - - auto loaded_apk = LoadedApk::LoadApkFromPath(args[0], diag_); - if (!loaded_apk) { - return 1; - } - - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - Printer printer(&fout); - - xml::Element* manifest_el = loaded_apk->GetManifest()->root.get(); - if (!manifest_el) { - diag_->Error(DiagMessage() << "No AndroidManifest."); - return 1; - } - - xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); - if (!attr) { - diag_->Error(DiagMessage() << "No package name."); - return 1; +int DumpXmlTreeCommand::Dump(LoadedApk* apk) { + for (auto file : files_) { + auto xml = apk->LoadXml(file, GetDiagnostics()); + if (!xml) { + return 1; + } + Debug::DumpXml(*xml, GetPrinter()); } - printer.Println(StringPrintf("%s", attr->value.c_str())); - return 0; } -/** Preform no action because a subcommand is required. */ -int DumpCommand::Action(const std::vector<std::string>& args) { - if (args.size() == 0) { - diag_->Error(DiagMessage() << "no subcommand specified"); - } else { - diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'"); - } - - Usage(&std::cerr); - return 1; -} +const char DumpBadgerCommand::kBadgerData[2925] = { + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 95, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 61, 63, 86, 35, 40, 46, 46, + 95, 95, 95, 95, 97, 97, 44, 32, 46, 124, 42, 33, 83, 62, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 58, 46, 58, 59, 61, 59, 61, 81, 81, 81, 81, 66, 96, 61, 61, 58, 46, + 46, 46, 58, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 61, 59, 59, 59, 58, 106, 81, 81, + 81, 81, 102, 59, 61, 59, 59, 61, 61, 61, 58, 46, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 61, 59, 59, 59, 58, 109, 81, 81, 81, 81, 61, 59, 59, 59, 59, 59, 58, 59, 59, + 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 46, 61, 59, 59, 59, 60, 81, 81, 81, 81, 87, 58, + 59, 59, 59, 59, 59, 59, 61, 119, 44, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 47, 61, 59, 59, + 58, 100, 81, 81, 81, 81, 35, 58, 59, 59, 59, 59, 59, 58, 121, 81, 91, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 46, 109, 58, 59, 59, 61, 81, 81, 81, 81, 81, 109, 58, 59, 59, 59, + 59, 61, 109, 81, 81, 76, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 41, 87, 59, 61, 59, 41, 81, 81, + 81, 81, 81, 81, 59, 61, 59, 59, 58, 109, 81, 81, 87, 39, 46, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 60, 81, 91, 59, 59, 61, 81, 81, 81, 81, 81, 87, 43, 59, 58, 59, 60, 81, 81, + 81, 76, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 52, 91, 58, 45, 59, 87, 81, 81, 81, 81, + 70, 58, 58, 58, 59, 106, 81, 81, 81, 91, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 93, 40, + 32, 46, 59, 100, 81, 81, 81, 81, 40, 58, 46, 46, 58, 100, 81, 81, 68, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 46, 46, 32, 46, + 46, 46, 32, 46, 32, 46, 45, 91, 59, 61, 58, 109, 81, 81, 81, 87, 46, 58, 61, + 59, 60, 81, 81, 80, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 46, 46, + 61, 59, 61, 61, 61, 59, 61, 61, 59, 59, 59, 58, 58, 46, 46, 41, 58, 59, 58, + 81, 81, 81, 81, 69, 58, 59, 59, 60, 81, 81, 68, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, + 32, 32, 32, 32, 58, 59, 61, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 61, 61, 46, 61, 59, 93, 81, 81, 81, 81, 107, 58, 59, 58, 109, 87, 68, + 96, 32, 32, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 10, 32, 32, 32, 46, 60, 61, 61, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 58, 58, 58, 115, 109, 68, 41, 36, + 81, 109, 46, 61, 61, 81, 69, 96, 46, 58, 58, 46, 58, 46, 46, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 46, 32, 95, 81, 67, + 61, 61, 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 58, 68, 39, 61, 105, 61, 63, 81, 119, 58, 106, 80, 32, 58, 61, 59, 59, + 61, 59, 61, 59, 61, 46, 95, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 10, 32, 32, 36, 81, 109, 105, 59, 61, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 46, 58, 37, 73, 108, 108, 62, 52, 81, + 109, 34, 32, 61, 59, 59, 59, 59, 59, 59, 59, 59, 59, 61, 59, 61, 61, 46, 46, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 46, 45, 57, 101, 43, 43, 61, + 61, 59, 59, 59, 59, 59, 59, 61, 59, 59, 59, 59, 59, 59, 59, 59, 59, 58, 97, + 46, 61, 108, 62, 126, 58, 106, 80, 96, 46, 61, 61, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 59, 59, 61, 61, 97, 103, 97, 32, 32, 32, 32, 32, 32, 32, 10, + 32, 32, 32, 32, 45, 46, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 45, 45, 45, + 58, 59, 59, 59, 59, 61, 119, 81, 97, 124, 105, 124, 124, 39, 126, 95, 119, 58, 61, + 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 61, 119, 81, 81, + 99, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 58, 59, 59, 58, 106, 81, 81, 81, 109, 119, + 119, 119, 109, 109, 81, 81, 122, 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 58, 115, 81, 87, 81, 102, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 61, 58, + 59, 61, 81, 81, 81, 81, 81, 81, 87, 87, 81, 81, 81, 81, 81, 58, 59, 59, 59, + 59, 59, 59, 59, 59, 58, 45, 45, 45, 59, 59, 59, 41, 87, 66, 33, 32, 32, 32, + 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 58, 59, 59, 93, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 40, 58, 59, 59, 59, 58, 45, 32, 46, 32, 32, 32, 32, 32, 46, + 32, 126, 96, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 58, 61, 59, 58, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 40, 58, 59, 59, 59, 58, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 58, 59, 59, 58, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 40, 58, 59, 59, 59, 46, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 58, 61, 59, 60, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 59, 61, 59, 59, 61, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 58, 59, 59, 93, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 40, 59, + 59, 59, 59, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 58, 61, 58, 106, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 81, 81, 81, 81, 76, 58, 59, 59, 59, 32, 46, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 61, 58, 58, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 87, 58, 59, 59, 59, 59, + 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 58, 59, 61, 41, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, + 81, 81, 87, 59, 61, 58, 59, 59, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 58, 61, 58, 61, 81, 81, + 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 107, 58, 59, 59, 59, 59, 58, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 58, 59, 59, 58, 51, 81, 81, 81, 81, 81, 81, 81, 81, 81, 81, 102, 94, + 59, 59, 59, 59, 59, 61, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 58, 61, 59, 59, 59, 43, 63, 36, 81, + 81, 81, 87, 64, 86, 102, 58, 59, 59, 59, 59, 59, 59, 59, 46, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 61, + 59, 59, 59, 59, 59, 59, 59, 43, 33, 58, 126, 126, 58, 59, 59, 59, 59, 59, 59, + 59, 59, 59, 59, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 46, 61, 59, 59, 59, 58, 45, 58, 61, 59, 58, 58, 58, 61, + 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 58, 32, 46, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 61, 59, 59, 59, 59, 59, + 58, 95, 32, 45, 61, 59, 61, 59, 59, 59, 59, 59, 59, 59, 45, 58, 59, 59, 59, + 59, 61, 58, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 58, 61, 59, 59, 59, 59, 59, 61, 59, 61, 46, 46, 32, 45, 45, 45, 59, 58, + 45, 45, 46, 58, 59, 59, 59, 59, 59, 59, 61, 46, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 46, 58, 59, 59, 59, 59, 59, 59, 59, 59, 59, + 61, 59, 46, 32, 32, 46, 32, 46, 32, 58, 61, 59, 59, 59, 59, 59, 59, 59, 59, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 45, + 59, 59, 59, 59, 59, 59, 59, 59, 58, 32, 32, 32, 32, 32, 32, 32, 32, 32, 61, + 59, 59, 59, 59, 59, 59, 59, 58, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 46, 61, 59, 59, 59, 59, 59, 59, 59, 32, 46, 32, + 32, 32, 32, 32, 32, 61, 46, 61, 59, 59, 59, 59, 59, 59, 58, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 61, 59, 59, 59, + 59, 59, 59, 59, 59, 32, 46, 32, 32, 32, 32, 32, 32, 32, 46, 61, 58, 59, 59, + 59, 59, 59, 58, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 58, 59, 59, 59, 59, 59, 59, 59, 59, 46, 46, 32, 32, 32, 32, + 32, 32, 32, 61, 59, 59, 59, 59, 59, 59, 59, 45, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 32, 45, 61, 59, 59, 59, 59, + 59, 58, 32, 46, 32, 32, 32, 32, 32, 32, 32, 58, 59, 59, 59, 59, 59, 58, 45, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 45, 45, 45, 45, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 45, 61, 59, 58, 45, 45, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 32, 32, 46, 32, 32, 32, 32, 32, 32, + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 10}; } // namespace aapt diff --git a/tools/aapt2/cmd/Dump.h b/tools/aapt2/cmd/Dump.h index 9ec820d89596..cd215b00db22 100644 --- a/tools/aapt2/cmd/Dump.h +++ b/tools/aapt2/cmd/Dump.h @@ -19,14 +19,62 @@ #include "Command.h" #include "Debug.h" +#include "LoadedApk.h" #include "dump/DumpManifest.h" namespace aapt { -/** Command the contents of files generated from the compilation stage. */ +/** + * The base command for dumping information about apks. When the command is executed, the command + * performs the DumpApkCommand::Dump() operation on each apk provided as a file argument. + **/ +class DumpApkCommand : public Command { + public: + explicit DumpApkCommand(const std::string&& name, text::Printer* printer, IDiagnostics* diag) + : Command(name), printer_(printer), diag_(diag) { + } + + text::Printer* GetPrinter() { + return printer_; + } + + IDiagnostics* GetDiagnostics() { + return diag_; + } + + /** Perform the dump operation on the apk. */ + virtual int Dump(LoadedApk* apk) = 0; + + int Action(const std::vector<std::string>& args) final { + if (args.size() < 1) { + diag_->Error(DiagMessage() << "No dump apk specified."); + return 1; + } + + bool error = false; + for (auto apk : args) { + auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); + if (!loaded_apk) { + error = true; + continue; + } + + error |= Dump(loaded_apk.get()); + } + + return error; + } + + private: + text::Printer* printer_; + IDiagnostics* diag_; +}; + +/** Command that prints contents of files generated from the compilation stage. */ class DumpAPCCommand : public Command { public: - explicit DumpAPCCommand(IDiagnostics* diag) : Command("apc"), diag_(diag) { + explicit DumpAPCCommand(text::Printer* printer, IDiagnostics* diag) + : Command("apc"), printer_(printer), diag_(diag) { SetDescription("Print the contents of the AAPT2 Container (APC) generated fom compilation."); AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.", &no_values_); @@ -36,120 +84,162 @@ class DumpAPCCommand : public Command { int Action(const std::vector<std::string>& args) override; private: + text::Printer* printer_; IDiagnostics* diag_; - bool verbose_ = false; bool no_values_ = false; + bool verbose_ = false; }; -/** Prints every configuration used by a resource in an APK. */ -class DumpConfigsCommand : public Command { +/** Easter egg command shown when users enter "badger" instead of "badging". */ +class DumpBadgerCommand : public Command { public: - explicit DumpConfigsCommand(IDiagnostics* diag) : Command("configurations"), diag_(diag) { - SetDescription("Print every configuration used by a resource in the APK."); + explicit DumpBadgerCommand(text::Printer* printer) : Command("badger"), printer_(printer) { } int Action(const std::vector<std::string>& args) override; private: - IDiagnostics* diag_; + text::Printer* printer_; + const static char kBadgerData[2925]; }; -/** Prints the contents of the resource table string pool in the APK. */ -class DumpStringsCommand : public Command { - public: - explicit DumpStringsCommand(IDiagnostics* diag) : Command("strings"), diag_(diag) { - SetDescription("Print the contents of the resource table string pool in the APK."); - } +class DumpBadgingCommand : public DumpApkCommand { + public: + explicit DumpBadgingCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("badging", printer, diag) { + SetDescription("Print information extracted from the manifest of the APK."); + AddOptionalSwitch("--include-meta-data", "Include meta-data information.", + &options_.include_meta_data); + } + + int Dump(LoadedApk* apk) override { + return DumpManifest(apk, options_, GetPrinter(), GetDiagnostics()); + } - int Action(const std::vector<std::string>& args) override; + private: + DumpManifestOptions options_; +}; - private: - IDiagnostics* diag_; +class DumpConfigsCommand : public DumpApkCommand { + public: + explicit DumpConfigsCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("configurations", printer, diag) { + SetDescription("Print every configuration used by a resource in the APK."); + } + + int Dump(LoadedApk* apk) override; }; -/** Prints the contents of the resource table from the APK. */ -class DumpTableCommand : public Command { +class DumpPackageNameCommand : public DumpApkCommand { public: - explicit DumpTableCommand(IDiagnostics* diag) : Command("resources"), diag_(diag) { + explicit DumpPackageNameCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("packagename", printer, diag) { + SetDescription("Print the package name of the APK."); + } + + int Dump(LoadedApk* apk) override; +}; + +class DumpPermissionsCommand : public DumpApkCommand { + public: + explicit DumpPermissionsCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("permissions", printer, diag) { + SetDescription("Print the permissions extracted from the manifest of the APK."); + } + + int Dump(LoadedApk* apk) override { + DumpManifestOptions options; + options.only_permissions = true; + return DumpManifest(apk, options, GetPrinter(), GetDiagnostics()); + } +}; + +class DumpStringsCommand : public DumpApkCommand { + public: + explicit DumpStringsCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("strings", printer, diag) { + SetDescription("Print the contents of the resource table string pool in the APK."); + } + + int Dump(LoadedApk* apk) override; +}; + +class DumpTableCommand : public DumpApkCommand { + public: + explicit DumpTableCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("resources", printer, diag) { SetDescription("Print the contents of the resource table from the APK."); AddOptionalSwitch("--no-values", "Suppresses output of values when displaying resource tables.", &no_values_); AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_); } - int Action(const std::vector<std::string>& args) override; + int Dump(LoadedApk* apk) override; private: - IDiagnostics* diag_; - bool verbose_ = false; bool no_values_ = false; + bool verbose_ = false; }; -/** Prints the string pool of a compiled xml in an APK. */ -class DumpXmlStringsCommand : public Command { -public: - explicit DumpXmlStringsCommand(IDiagnostics* diag) : Command("xmlstrings"), diag_(diag) { - SetDescription("Print the string pool of a compiled xml in an APK."); - AddRequiredFlagList("--file", "A compiled xml file to print", &files_); - } - - int Action(const std::vector<std::string>& args) override; - -private: - IDiagnostics* diag_; - std::vector<std::string> files_; -}; - - -/** Prints the tree of a compiled xml in an APK. */ -class DumpXmlTreeCommand : public Command { +class DumpXmlStringsCommand : public DumpApkCommand { public: - explicit DumpXmlTreeCommand(IDiagnostics* diag) : Command("xmltree"), diag_(diag) { - SetDescription("Print the tree of a compiled xml in an APK."); + explicit DumpXmlStringsCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("xmlstrings", printer, diag) { + SetDescription("Print the string pool of a compiled xml in an APK."); AddRequiredFlagList("--file", "A compiled xml file to print", &files_); } - int Action(const std::vector<std::string>& args) override; + int Dump(LoadedApk* apk) override; private: - IDiagnostics* diag_; std::vector<std::string> files_; }; -/** Prints the contents of the resource table from the APK. */ -class DumpPackageNameCommand : public Command { +class DumpXmlTreeCommand : public DumpApkCommand { public: - explicit DumpPackageNameCommand(IDiagnostics* diag) : Command("packagename"), diag_(diag) { - SetDescription("Print the package name of the APK."); + explicit DumpXmlTreeCommand(text::Printer* printer, IDiagnostics* diag) + : DumpApkCommand("xmltree", printer, diag) { + SetDescription("Print the tree of a compiled xml in an APK."); + AddRequiredFlagList("--file", "A compiled xml file to print", &files_); } - int Action(const std::vector<std::string>& args) override; + int Dump(LoadedApk* apk) override; private: - IDiagnostics* diag_; + std::vector<std::string> files_; }; /** The default dump command. Performs no action because a subcommand is required. */ class DumpCommand : public Command { public: - explicit DumpCommand(IDiagnostics* diag) : Command("dump", "d"), diag_(diag) { - AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpTableCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(diag_)); - AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(diag_)); + explicit DumpCommand(text::Printer* printer, IDiagnostics* diag) + : Command("dump", "d"), diag_(diag) { + AddOptionalSubcommand(util::make_unique<DumpAPCCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpBadgingCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpConfigsCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpPackageNameCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpPermissionsCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpStringsCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpTableCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpXmlStringsCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpXmlTreeCommand>(printer, diag_)); + AddOptionalSubcommand(util::make_unique<DumpBadgerCommand>(printer), /* hidden */ true); } - int Action(const std::vector<std::string>& args) override; + int Action(const std::vector<std::string>& args) override { + if (args.size() == 0) { + diag_->Error(DiagMessage() << "no subcommand specified"); + } else { + diag_->Error(DiagMessage() << "unknown subcommand '" << args[0] << "'"); + } + Usage(&std::cerr); + return 1; + } private: IDiagnostics* diag_; }; -}// namespace aapt +} // namespace aapt -#endif //AAPT2_DUMP_H +#endif // AAPT2_DUMP_H diff --git a/tools/aapt2/dump/DumpManifest.cpp b/tools/aapt2/dump/DumpManifest.cpp index c0e1dc682845..11a4074cd3cd 100644 --- a/tools/aapt2/dump/DumpManifest.cpp +++ b/tools/aapt2/dump/DumpManifest.cpp @@ -109,15 +109,8 @@ class CommonFeatureGroup; class ManifestExtractor { public: - struct Options { - /** Include meta information from <meta-data> elements in the output. */ - bool include_meta_data = false; - /** Only output permission information. */ - bool only_permissions = false; - }; - - explicit ManifestExtractor(LoadedApk* apk, ManifestExtractor::Options options) + explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options) : apk_(apk), options_(options) { } class Element { @@ -128,7 +121,7 @@ class ManifestExtractor { static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el); /** Writes out the extracted contents of the element. */ - virtual void Print(text::Printer& printer) { } + virtual void Print(text::Printer* printer) { } /** Adds an element to the list of children of the element. */ void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); } @@ -332,7 +325,7 @@ class ManifestExtractor { return config; } - bool Dump(text::Printer& printer, IDiagnostics* diag); + bool Dump(text::Printer* printer, IDiagnostics* diag); /** Recursively visit the xml element tree and return a processed badging element tree. */ std::unique_ptr<Element> Visit(xml::Element* element); @@ -378,7 +371,7 @@ class ManifestExtractor { } LoadedApk* const apk_; - const Options options_; + DumpManifestOptions& options_; private: std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>(); @@ -450,40 +443,40 @@ class Manifest : public ManifestExtractor::Element { installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR)); } - void Print(text::Printer& printer) override { - printer.Print(StringPrintf("package: name='%s' ", package.data())); - printer.Print(StringPrintf("versionCode='%s' ", + void Print(text::Printer* printer) override { + printer->Print(StringPrintf("package: name='%s' ", package.data())); + printer->Print(StringPrintf("versionCode='%s' ", (versionCode > 0) ? std::to_string(versionCode).data() : "")); - printer.Print(StringPrintf("versionName='%s'", versionName.data())); + printer->Print(StringPrintf("versionName='%s'", versionName.data())); if (split) { - printer.Print(StringPrintf(" split='%s'", split->data())); + printer->Print(StringPrintf(" split='%s'", split->data())); } if (platformVersionName) { - printer.Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data())); + printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data())); } if (platformVersionCode) { - printer.Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data())); + printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data())); } if (compilesdkVersion) { - printer.Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion)); + printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion)); } if (compilesdkVersionCodename) { - printer.Print(StringPrintf(" compileSdkVersionCodename='%s'", + printer->Print(StringPrintf(" compileSdkVersionCodename='%s'", compilesdkVersionCodename->data())); } - printer.Print("\n"); + printer->Print("\n"); if (installLocation) { switch (*installLocation) { case 0: - printer.Print("install-location:'auto'\n"); + printer->Print("install-location:'auto'\n"); break; case 1: - printer.Print("install-location:'internalOnly'\n"); + printer->Print("install-location:'internalOnly'\n"); break; case 2: - printer.Print("install-location:'preferExternal'\n"); + printer->Print("install-location:'preferExternal'\n"); break; default: break; @@ -544,15 +537,15 @@ class Application : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { // Print the labels for every locale for (auto p : locale_labels) { if (p.first.empty()) { - printer.Print(StringPrintf("application-label:'%s'\n", + printer->Print(StringPrintf("application-label:'%s'\n", android::ResTable::normalizeForOutput(p.second.data()) .c_str())); } else { - printer.Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(), + printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(), android::ResTable::normalizeForOutput(p.second.data()) .c_str())); } @@ -560,26 +553,26 @@ class Application : public ManifestExtractor::Element { // Print the icon paths for every density for (auto p : density_icons) { - printer.Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data())); + printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data())); } // Print the application info - printer.Print(StringPrintf("application: label='%s' ", + printer->Print(StringPrintf("application: label='%s' ", android::ResTable::normalizeForOutput(label.data()).c_str())); - printer.Print(StringPrintf("icon='%s'", icon.data())); + printer->Print(StringPrintf("icon='%s'", icon.data())); if (!banner.empty()) { - printer.Print(StringPrintf(" banner='%s'", banner.data())); + printer->Print(StringPrintf(" banner='%s'", banner.data())); } - printer.Print("\n"); + printer->Print("\n"); if (test_only != 0) { - printer.Print(StringPrintf("testOnly='%d'\n", test_only)); + printer->Print(StringPrintf("testOnly='%d'\n", test_only)); } if (is_game != 0) { - printer.Print("application-isGame\n"); + printer->Print("application-isGame\n"); } if (debuggable != 0) { - printer.Print("application-debuggable\n"); + printer->Print("application-debuggable\n"); } } }; @@ -614,19 +607,19 @@ class UsesSdkBadging : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (min_sdk) { - printer.Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk)); + printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk)); } else if (min_sdk_name) { - printer.Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data())); + printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data())); } if (max_sdk) { - printer.Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk)); + printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk)); } if (target_sdk) { - printer.Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk)); + printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk)); } else if (target_sdk_name) { - printer.Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data())); + printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data())); } } }; @@ -654,24 +647,24 @@ class UsesConfiguarion : public ManifestExtractor::Element { FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0); } - void Print(text::Printer& printer) override { - printer.Print("uses-configuration:"); + void Print(text::Printer* printer) override { + printer->Print("uses-configuration:"); if (req_touch_screen != 0) { - printer.Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen)); + printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen)); } if (req_keyboard_type != 0) { - printer.Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type)); + printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type)); } if (req_hard_keyboard != 0) { - printer.Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard)); + printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard)); } if (req_navigation != 0) { - printer.Print(StringPrintf(" reqNavigation='%d'", req_navigation)); + printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation)); } if (req_five_way_nav != 0) { - printer.Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav)); + printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav)); } - printer.Print("\n"); + printer->Print("\n"); } }; @@ -715,7 +708,7 @@ class SupportsScreen : public ManifestExtractor::Element { } } - void PrintScreens(text::Printer& printer, int32_t target_sdk) { + void PrintScreens(text::Printer* printer, int32_t target_sdk) { int32_t small_screen_temp = small_screen; int32_t normal_screen_temp = normal_screen; int32_t large_screen_temp = large_screen; @@ -745,30 +738,30 @@ class SupportsScreen : public ManifestExtractor::Element { } // Print the formatted screen info - printer.Print("supports-screens:"); + printer->Print("supports-screens:"); if (small_screen_temp != 0) { - printer.Print(" 'small'"); + printer->Print(" 'small'"); } if (normal_screen_temp != 0) { - printer.Print(" 'normal'"); + printer->Print(" 'normal'"); } if (large_screen_temp != 0) { - printer.Print(" 'large'"); + printer->Print(" 'large'"); } if (xlarge_screen_temp != 0) { - printer.Print(" 'xlarge'"); + printer->Print(" 'xlarge'"); } - printer.Print("\n"); - printer.Print(StringPrintf("supports-any-density: '%s'\n", + printer->Print("\n"); + printer->Print(StringPrintf("supports-any-density: '%s'\n", (any_density_temp ) ? "true" : "false")); if (requires_smallest_width_dp > 0) { - printer.Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp)); + printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp)); } if (compatible_width_limit_dp > 0) { - printer.Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp)); + printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp)); } if (largest_width_limit_dp > 0) { - printer.Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp)); + printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp)); } } }; @@ -784,20 +777,20 @@ class FeatureGroup : public ManifestExtractor::Element { label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), ""); } - virtual void PrintGroup(text::Printer& printer) { - printer.Print(StringPrintf("feature-group: label='%s'\n", label.data())); + virtual void PrintGroup(text::Printer* printer) { + printer->Print(StringPrintf("feature-group: label='%s'\n", label.data())); if (open_gles_version > 0) { - printer.Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version)); + printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version)); } for (auto feature : features_) { - printer.Print(StringPrintf(" uses-feature%s: name='%s'", + printer->Print(StringPrintf(" uses-feature%s: name='%s'", (feature.second.required ? "" : "-not-required"), feature.first.data())); if (feature.second.version > 0) { - printer.Print(StringPrintf(" version='%d'", feature.second.version)); + printer->Print(StringPrintf(" version='%d'", feature.second.version)); } - printer.Print("\n"); + printer->Print("\n"); } } @@ -863,29 +856,29 @@ class FeatureGroup : public ManifestExtractor::Element { class CommonFeatureGroup : public FeatureGroup { public: CommonFeatureGroup() = default; - void PrintGroup(text::Printer& printer) override { + void PrintGroup(text::Printer* printer) override { FeatureGroup::PrintGroup(printer); // Also print the implied features for (auto feature : implied_features_) { if (features_.find(feature.first) == features_.end()) { const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : ""; - printer.Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data())); - printer.Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23, + printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data())); + printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23, feature.first.data())); // Print the reasons as a sentence size_t count = 0; for (auto reason : feature.second.reasons) { - printer.Print(reason); + printer->Print(reason); if (count + 2 < feature.second.reasons.size()) { - printer.Print(", "); + printer->Print(", "); } else if (count + 1 < feature.second.reasons.size()) { - printer.Print(", and "); + printer->Print(", and "); } count++; } - printer.Print("'\n"); + printer->Print("'\n"); } } } @@ -1070,35 +1063,35 @@ class UsesPermission : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (!name.empty()) { - printer.Print(StringPrintf("uses-permission: name='%s'", name.data())); + printer->Print(StringPrintf("uses-permission: name='%s'", name.data())); if (maxSdkVersion >= 0) { - printer.Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); + printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); } if (!requiredFeature.empty()) { - printer.Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data())); + printer->Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data())); } if (!requiredNotFeature.empty()) { - printer.Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data())); + printer->Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data())); } - printer.Print("\n"); + printer->Print("\n"); if (required == 0) { - printer.Print(StringPrintf("optional-permission: name='%s'", name.data())); + printer->Print(StringPrintf("optional-permission: name='%s'", name.data())); if (maxSdkVersion >= 0) { - printer.Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); + printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); } - printer.Print("\n"); + printer->Print("\n"); } } } - void PrintImplied(text::Printer& printer, const std::string& reason) { - printer.Print(StringPrintf("uses-implied-permission: name='%s'", name.data())); + void PrintImplied(text::Printer* printer, const std::string& reason) { + printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data())); if (maxSdkVersion >= 0) { - printer.Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); + printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion)); } - printer.Print(StringPrintf(" reason='%s'\n", reason.data())); + printer->Print(StringPrintf(" reason='%s'\n", reason.data())); } }; @@ -1119,13 +1112,13 @@ class UsesPermissionSdk23 : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (name) { - printer.Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data())); + printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data())); if (maxSdkVersion) { - printer.Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion)); + printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion)); } - printer.Print("\n"); + printer->Print("\n"); } } }; @@ -1140,9 +1133,9 @@ class Permission : public ManifestExtractor::Element { name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), ""); } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (extractor()->options_.only_permissions && !name.empty()) { - printer.Print(StringPrintf("permission: %s\n", name.data())); + printer->Print(StringPrintf("permission: %s\n", name.data())); } } }; @@ -1202,25 +1195,25 @@ class Activity : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { // Print whether the activity has the HOME category and a the MAIN action if (has_main_action && has_launcher_category) { - printer.Print("launchable-activity:"); + printer->Print("launchable-activity:"); if (!name.empty()) { - printer.Print(StringPrintf(" name='%s' ", name.data())); + printer->Print(StringPrintf(" name='%s' ", name.data())); } - printer.Print(StringPrintf(" label='%s' icon='%s'\n", + printer->Print(StringPrintf(" label='%s' icon='%s'\n", android::ResTable::normalizeForOutput(label.data()).c_str(), icon.data())); } // Print wether the activity has the HOME category and a the MAIN action if (has_leanback_launcher_category) { - printer.Print("leanback-launchable-activity:"); + printer->Print("leanback-launchable-activity:"); if (!name.empty()) { - printer.Print(StringPrintf(" name='%s' ", name.data())); + printer->Print(StringPrintf(" name='%s' ", name.data())); } - printer.Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n", + printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n", android::ResTable::normalizeForOutput(label.data()).c_str(), icon.data(), banner.data())); } @@ -1319,9 +1312,9 @@ class UsesLibrary : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (!name.empty()) { - printer.Print(StringPrintf("uses-library%s:'%s'\n", + printer->Print(StringPrintf("uses-library%s:'%s'\n", (required == 0) ? "-not-required" : "", name.data())); } } @@ -1344,8 +1337,8 @@ class StaticLibrary : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { - printer.Print(StringPrintf( + void Print(text::Printer* printer) override { + printer->Print(StringPrintf( "static-library: name='%s' version='%d' versionMajor='%d'\n", name.data(), version, versionMajor)); } @@ -1380,14 +1373,14 @@ class UsesStaticLibrary : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { - printer.Print(StringPrintf( + void Print(text::Printer* printer) override { + printer->Print(StringPrintf( "uses-static-library: name='%s' version='%d' versionMajor='%d'", name.data(), version, versionMajor)); for (size_t i = 0; i < certDigests.size(); i++) { - printer.Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); + printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); } - printer.Print("\n"); + printer->Print("\n"); } }; @@ -1412,21 +1405,21 @@ class MetaData : public ManifestExtractor::Element { resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR)); } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (extractor()->options_.include_meta_data && !name.empty()) { - printer.Print(StringPrintf("meta-data: name='%s' ", name.data())); + printer->Print(StringPrintf("meta-data: name='%s' ", name.data())); if (!value.empty()) { - printer.Print(StringPrintf("value='%s' ", value.data())); + printer->Print(StringPrintf("value='%s' ", value.data())); } else if (value_int) { - printer.Print(StringPrintf("value='%d' ", *value_int)); + printer->Print(StringPrintf("value='%d' ", *value_int)); } else { if (!resource.empty()) { - printer.Print(StringPrintf("resource='%s' ", resource.data())); + printer->Print(StringPrintf("resource='%s' ", resource.data())); } else if (resource_int) { - printer.Print(StringPrintf("resource='%d' ", *resource_int)); + printer->Print(StringPrintf("resource='%d' ", *resource_int)); } } - printer.Print("\n"); + printer->Print("\n"); } } }; @@ -1548,14 +1541,14 @@ class SupportsInput : public ManifestExtractor::Element { SupportsInput() = default; std::vector<std::string> inputs; - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { const size_t size = inputs.size(); if (size > 0) { - printer.Print("supports-input: '"); + printer->Print("supports-input: '"); for (size_t i = 0; i < size; i++) { - printer.Print(StringPrintf("value='%s' ", inputs[i].data())); + printer->Print(StringPrintf("value='%s' ", inputs[i].data())); } - printer.Print("\n"); + printer->Print("\n"); } } }; @@ -1586,9 +1579,9 @@ class OriginalPackage : public ManifestExtractor::Element { name = GetAttributeString(FindAttribute(element, NAME_ATTR)); } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (name) { - printer.Print(StringPrintf("original-package:'%s'\n", name->data())); + printer->Print(StringPrintf("original-package:'%s'\n", name->data())); } } }; @@ -1605,9 +1598,9 @@ class PackageVerifier : public ManifestExtractor::Element { public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR)); } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (name && public_key) { - printer.Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n", + printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n", name->data(), public_key->data())); } } @@ -1644,18 +1637,18 @@ class UsesPackage : public ManifestExtractor::Element { } } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (name) { if (packageType) { - printer.Print(StringPrintf( + printer->Print(StringPrintf( "uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'", packageType->data(), name->data(), version, versionMajor)); for (size_t i = 0; i < certDigests.size(); i++) { - printer.Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); + printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data())); } - printer.Print("\n"); + printer->Print("\n"); } else { - printer.Print(StringPrintf("uses-package:'%s'\n", name->data())); + printer->Print(StringPrintf("uses-package:'%s'\n", name->data())); } } } @@ -1700,8 +1693,8 @@ class Screen : public ManifestExtractor::Element { class CompatibleScreens : public ManifestExtractor::Element { public: CompatibleScreens() = default; - void Print(text::Printer& printer) override { - printer.Print("compatible-screens:"); + void Print(text::Printer* printer) override { + printer->Print("compatible-screens:"); bool first = true; ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){ @@ -1709,15 +1702,15 @@ class CompatibleScreens : public ManifestExtractor::Element { if (first) { first = false; } else { - printer.Print(","); + printer->Print(","); } if (screen->size && screen->density) { - printer.Print(StringPrintf("'%d/%d'", *screen->size, *screen->density)); + printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density)); } } }); - printer.Print("\n"); + printer->Print("\n"); } }; @@ -1731,22 +1724,22 @@ class SupportsGlTexture : public ManifestExtractor::Element { name = GetAttributeString(FindAttribute(element, NAME_ATTR)); } - void Print(text::Printer& printer) override { + void Print(text::Printer* printer) override { if (name) { - printer.Print(StringPrintf("supports-gl-texture:'%s'\n", name->data())); + printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data())); } } }; /** Recursively prints the extracted badging element. */ -static void Print(ManifestExtractor::Element* el, text::Printer& printer) { +static void Print(ManifestExtractor::Element* el, text::Printer* printer) { el->Print(printer); for (auto &child : el->children()) { Print(child.get(), printer); } } -bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { +bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) { // Load the manifest std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag); if (doc == nullptr) { @@ -1775,7 +1768,7 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { } } - printer.Print(StringPrintf("package: %s\n", manifest->package.data())); + printer->Print(StringPrintf("package: %s\n", manifest->package.data())); ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void { el->Print(printer); }); @@ -1992,7 +1985,7 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { // Print the components types if they are present auto PrintComponent = [&components, &printer](const std::string& component) -> void { if (components.find(component) != components.end()) { - printer.Print(StringPrintf("provides-component:'%s'\n", component.data())); + printer->Print(StringPrintf("provides-component:'%s'\n", component.data())); } }; @@ -2013,14 +2006,14 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { // Print presence of main activity if (components.find("main") != components.end()) { - printer.Print("main\n"); + printer->Print("main\n"); } // Print presence of activities, recivers, and services with no special components FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool { if (auto activity = ElementCast<Activity>(el)) { if (!activity->has_component_) { - printer.Print("other-activities\n"); + printer->Print("other-activities\n"); return true; } } @@ -2030,7 +2023,7 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool { if (auto receiver = ElementCast<Receiver>(el)) { if (!receiver->has_component) { - printer.Print("other-receivers\n"); + printer->Print("other-receivers\n"); return true; } } @@ -2040,7 +2033,7 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool { if (auto service = ElementCast<Service>(el)) { if (!service->has_component) { - printer.Print("other-services\n"); + printer->Print("other-services\n"); return true; } } @@ -2062,22 +2055,22 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { } // Print all the unique locales of the apk - printer.Print("locales:"); + printer->Print("locales:"); for (auto& config : locales_) { if (config.first.empty()) { - printer.Print(" '--_--'"); + printer->Print(" '--_--'"); } else { - printer.Print(StringPrintf(" '%s'", config.first.data())); + printer->Print(StringPrintf(" '%s'", config.first.data())); } } - printer.Print("\n"); + printer->Print("\n"); // Print all the densities locales of the apk - printer.Print("densities:"); + printer->Print("densities:"); for (auto& config : densities_) { - printer.Print(StringPrintf(" '%d'", config.first)); + printer->Print(StringPrintf(" '%d'", config.first)); } - printer.Print("\n"); + printer->Print("\n"); // Print the supported architectures of the app std::set<std::string> architectures; @@ -2131,7 +2124,7 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { } if (arch != architectures.end()) { - printer.Print(StringPrintf("native-code: '%s'\n", arch->data())); + printer->Print(StringPrintf("native-code: '%s'\n", arch->data())); architectures.erase(arch); output_alt_native_code = true; } @@ -2139,13 +2132,13 @@ bool ManifestExtractor::Dump(text::Printer& printer, IDiagnostics* diag) { if (architectures.size() > 0) { if (output_alt_native_code) { - printer.Print("alt-"); + printer->Print("alt-"); } - printer.Print("native-code:"); + printer->Print("native-code:"); for (auto& arch : architectures) { - printer.Print(StringPrintf(" '%s'", arch.data())); + printer->Print(StringPrintf(" '%s'", arch.data())); } - printer.Print("\n"); + printer->Print("\n"); } return true; @@ -2272,55 +2265,11 @@ std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Elemen return element; } -// Use a smaller buffer so that there is less latency for dumping to stdout. -constexpr size_t kStdOutBufferSize = 1024u; - -int DumpBadgingCommand::Action(const std::vector<std::string>& args) { - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); - return 1; - } - - ManifestExtractor::Options options; - options.include_meta_data = include_metadata_; - - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - text::Printer printer(&fout); - for (auto apk : args) { - auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); - if (!loaded_apk) { - return 1; - } - - ManifestExtractor extractor(loaded_apk.get(), options); - extractor.Dump(printer, diag_); - } - - return 0; -} - -int DumpPermissionsCommand::Action(const std::vector<std::string>& args) { - if (args.size() < 1) { - diag_->Error(DiagMessage() << "No dump apk specified."); - return 1; - } - - ManifestExtractor::Options options; - options.only_permissions = true; - - io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); - text::Printer printer(&fout); - for (auto apk : args) { - auto loaded_apk = LoadedApk::LoadApkFromPath(apk, diag_); - if (!loaded_apk) { - return 1; - } - - ManifestExtractor extractor(loaded_apk.get(), options); - extractor.Dump(printer, diag_); - } - return 0; +int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer, + IDiagnostics* diag) { + ManifestExtractor extractor(apk, options); + return extractor.Dump(printer, diag); } } // namespace aapt diff --git a/tools/aapt2/dump/DumpManifest.h b/tools/aapt2/dump/DumpManifest.h index a70be53fa070..daf22ed57a84 100644 --- a/tools/aapt2/dump/DumpManifest.h +++ b/tools/aapt2/dump/DumpManifest.h @@ -17,45 +17,23 @@ #ifndef AAPT2_DUMP_MANIFEST_H #define AAPT2_DUMP_MANIFEST_H -#include <Diagnostics.h> -#include <ValueVisitor.h> -#include <io/ZipArchive.h> - - -#include "cmd/Command.h" -#include "process/IResourceTableConsumer.h" +#include "Diagnostics.h" +#include "LoadedApk.h" #include "text/Printer.h" -#include "xml/XmlDom.h" namespace aapt { -class DumpBadgingCommand : public Command { - public: - explicit DumpBadgingCommand(IDiagnostics* diag) : Command("badging"), diag_(diag) { - SetDescription("Print information extracted from the manifest of the APK."); - AddOptionalSwitch("--include-meta-data", "Include meta-data information.", - &include_metadata_); - } - - int Action(const std::vector<std::string>& args) override; - - private: - IDiagnostics* diag_; - bool include_metadata_ = false; +struct DumpManifestOptions { + /** Include meta information from <meta-data> elements in the output. */ + bool include_meta_data = false; + /** Only output permission information. */ + bool only_permissions = false; }; -class DumpPermissionsCommand : public Command { - public: - explicit DumpPermissionsCommand(IDiagnostics* diag) : Command("permissions"), diag_(diag) { - SetDescription("Print the permissions extracted from the manifest of the APK."); - } - - int Action(const std::vector<std::string>& args) override; - - private: - IDiagnostics* diag_; -}; +/** Print information extracted from the manifest of the APK. */ +int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer, + IDiagnostics* diag); -}// namespace aapt +} // namespace aapt -#endif //AAPT2_DUMP_MANIFEST_H
\ No newline at end of file +#endif // AAPT2_DUMP_MANIFEST_H
\ No newline at end of file |