Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 17 | #include <cinttypes> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 21 | #include "androidfw/StringPiece.h" |
| 22 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 23 | #include "Debug.h" |
| 24 | #include "Diagnostics.h" |
| 25 | #include "Flags.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 26 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 27 | #include "format/binary/BinaryResourceParser.h" |
| 28 | #include "format/proto/ProtoDeserialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 29 | #include "io/FileStream.h" |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 30 | #include "io/ZipArchive.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 31 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 32 | #include "text/Printer.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 33 | #include "util/Files.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 35 | using ::aapt::text::Printer; |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 36 | using ::android::StringPiece; |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 37 | using ::android::base::StringPrintf; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 39 | namespace aapt { |
| 40 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 41 | struct DumpOptions { |
| 42 | DebugPrintTableOptions print_options; |
| 43 | |
| 44 | // The path to a file within an APK to dump. |
| 45 | Maybe<std::string> file_to_dump_path; |
| 46 | }; |
| 47 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 48 | static const char* ResourceFileTypeToString(const ResourceFile::Type& type) { |
| 49 | switch (type) { |
| 50 | case ResourceFile::Type::kPng: |
| 51 | return "PNG"; |
| 52 | case ResourceFile::Type::kBinaryXml: |
| 53 | return "BINARY_XML"; |
| 54 | case ResourceFile::Type::kProtoXml: |
| 55 | return "PROTO_XML"; |
| 56 | default: |
| 57 | break; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | } |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 59 | return "UNKNOWN"; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 62 | static void DumpCompiledFile(const ResourceFile& file, const Source& source, off64_t offset, |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 63 | size_t len, Printer* printer) { |
| 64 | printer->Print("Resource: "); |
| 65 | printer->Println(file.name.to_string()); |
| 66 | |
| 67 | printer->Print("Config: "); |
| 68 | printer->Println(file.config.to_string()); |
| 69 | |
| 70 | printer->Print("Source: "); |
| 71 | printer->Println(file.source.to_string()); |
| 72 | |
| 73 | printer->Print("Type: "); |
| 74 | printer->Println(ResourceFileTypeToString(file.type)); |
| 75 | |
| 76 | printer->Println(StringPrintf("Data: offset=%" PRIi64 " length=%zd", offset, len)); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 79 | static bool DumpXmlFile(IAaptContext* context, io::IFile* file, bool proto, |
| 80 | text::Printer* printer) { |
| 81 | std::unique_ptr<xml::XmlResource> doc; |
| 82 | if (proto) { |
| 83 | std::unique_ptr<io::InputStream> in = file->OpenInputStream(); |
| 84 | if (in == nullptr) { |
| 85 | context->GetDiagnostics()->Error(DiagMessage() << "failed to open file"); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | io::ZeroCopyInputAdaptor adaptor(in.get()); |
| 90 | pb::XmlNode pb_node; |
| 91 | if (!pb_node.ParseFromZeroCopyStream(&adaptor)) { |
| 92 | context->GetDiagnostics()->Error(DiagMessage() << "failed to parse file as proto XML"); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | std::string err; |
| 97 | doc = DeserializeXmlResourceFromPb(pb_node, &err); |
| 98 | if (doc == nullptr) { |
| 99 | context->GetDiagnostics()->Error(DiagMessage() << "failed to deserialize proto XML"); |
| 100 | return false; |
| 101 | } |
| 102 | printer->Println("Proto XML"); |
| 103 | } else { |
| 104 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 105 | if (data == nullptr) { |
| 106 | context->GetDiagnostics()->Error(DiagMessage() << "failed to open file"); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | std::string err; |
| 111 | doc = xml::Inflate(data->data(), data->size(), &err); |
| 112 | if (doc == nullptr) { |
| 113 | context->GetDiagnostics()->Error(DiagMessage() << "failed to parse file as binary XML"); |
| 114 | return false; |
| 115 | } |
| 116 | printer->Println("Binary XML"); |
| 117 | } |
| 118 | |
| 119 | Debug::DumpXml(*doc, printer); |
| 120 | return true; |
| 121 | } |
| 122 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 123 | static bool TryDumpFile(IAaptContext* context, const std::string& file_path, |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 124 | const DumpOptions& options) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 125 | // Use a smaller buffer so that there is less latency for dumping to stdout. |
| 126 | constexpr size_t kStdOutBufferSize = 1024u; |
| 127 | io::FileOutputStream fout(STDOUT_FILENO, kStdOutBufferSize); |
| 128 | Printer printer(&fout); |
| 129 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | std::string err; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 131 | std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(file_path, &err); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | if (zip) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 133 | ResourceTable table; |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 134 | bool proto = false; |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 135 | if (io::IFile* file = zip->FindFile("resources.pb")) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 136 | proto = true; |
| 137 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 139 | if (data == nullptr) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 140 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.pb"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 141 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 142 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 143 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 144 | pb::ResourceTable pb_table; |
| 145 | if (!pb_table.ParseFromArray(data->data(), data->size())) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 146 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "invalid resources.pb"); |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 147 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 148 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 149 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 150 | if (!DeserializeTableFromPb(pb_table, zip.get(), &table, &err)) { |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 151 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 152 | << "failed to parse table: " << err); |
| 153 | return false; |
| 154 | } |
| 155 | } else if (io::IFile* file = zip->FindFile("resources.arsc")) { |
| 156 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 157 | if (!data) { |
| 158 | context->GetDiagnostics()->Error(DiagMessage(file_path) << "failed to open resources.arsc"); |
| 159 | return false; |
| 160 | } |
| 161 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 162 | BinaryResourceParser parser(context->GetDiagnostics(), &table, Source(file_path), |
| 163 | data->data(), data->size()); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 164 | if (!parser.Parse()) { |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 165 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 169 | if (!options.file_to_dump_path) { |
| 170 | if (proto) { |
| 171 | printer.Println("Proto APK"); |
| 172 | } else { |
| 173 | printer.Println("Binary APK"); |
| 174 | } |
| 175 | Debug::PrintTable(table, options.print_options, &printer); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | io::IFile* file = zip->FindFile(options.file_to_dump_path.value()); |
| 180 | if (file == nullptr) { |
| 181 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 182 | << "file '" << options.file_to_dump_path.value() |
| 183 | << "' not found in APK"); |
| 184 | return false; |
| 185 | } |
| 186 | return DumpXmlFile(context, file, proto, &printer); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 187 | } |
Adam Lesinski | 5e8fa3a | 2016-06-27 16:21:42 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 189 | err.clear(); |
| 190 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 191 | io::FileInputStream input(file_path); |
| 192 | if (input.HadError()) { |
| 193 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 194 | << "failed to open file: " << input.GetError()); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 198 | // Try as a compiled file. |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 199 | ContainerReader reader(&input); |
| 200 | if (reader.HadError()) { |
| 201 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 202 | << "failed to read container: " << reader.GetError()); |
Adam Lesinski | 8cdca1b | 2017-09-28 15:50:03 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 206 | printer.Println("AAPT2 Container (APC)"); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 207 | ContainerReaderEntry* entry; |
| 208 | while ((entry = reader.Next()) != nullptr) { |
| 209 | if (entry->Type() == ContainerEntryType::kResTable) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 210 | printer.Println("kResTable"); |
| 211 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 212 | pb::ResourceTable pb_table; |
| 213 | if (!entry->GetResTable(&pb_table)) { |
| 214 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 215 | << "failed to parse proto table: " << entry->GetError()); |
| 216 | continue; |
| 217 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 218 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 219 | ResourceTable table; |
| 220 | err.clear(); |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 221 | if (!DeserializeTableFromPb(pb_table, nullptr /*files*/, &table, &err)) { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 222 | context->GetDiagnostics()->Error(DiagMessage(file_path) |
| 223 | << "failed to parse table: " << err); |
| 224 | continue; |
| 225 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 226 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 227 | printer.Indent(); |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 228 | Debug::PrintTable(table, options.print_options, &printer); |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 229 | printer.Undent(); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 230 | } else if (entry->Type() == ContainerEntryType::kResFile) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 231 | printer.Println("kResFile"); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 232 | pb::internal::CompiledFile pb_compiled_file; |
| 233 | off64_t offset; |
| 234 | size_t length; |
| 235 | if (!entry->GetResFileOffsets(&pb_compiled_file, &offset, &length)) { |
| 236 | context->GetDiagnostics()->Error( |
| 237 | DiagMessage(file_path) << "failed to parse compiled proto file: " << entry->GetError()); |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | ResourceFile file; |
| 242 | std::string error; |
| 243 | if (!DeserializeCompiledFileFromPb(pb_compiled_file, &file, &error)) { |
| 244 | context->GetDiagnostics()->Warn(DiagMessage(file_path) |
| 245 | << "failed to parse compiled file: " << error); |
| 246 | continue; |
| 247 | } |
| 248 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 249 | printer.Indent(); |
| 250 | DumpCompiledFile(file, Source(file_path), offset, length, &printer); |
| 251 | printer.Undent(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 254 | return true; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 257 | namespace { |
| 258 | |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 259 | class DumpContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 260 | public: |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 261 | PackageType GetPackageType() override { |
| 262 | // Doesn't matter. |
| 263 | return PackageType::kApp; |
| 264 | } |
| 265 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 266 | IDiagnostics* GetDiagnostics() override { |
| 267 | return &diagnostics_; |
| 268 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 269 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | NameMangler* GetNameMangler() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 271 | UNIMPLEMENTED(FATAL); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 272 | return nullptr; |
| 273 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 274 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 275 | const std::string& GetCompilationPackage() override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 276 | static std::string empty; |
| 277 | return empty; |
| 278 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 279 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 280 | uint8_t GetPackageId() override { |
| 281 | return 0; |
| 282 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 283 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 284 | SymbolTable* GetExternalSymbols() override { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 285 | UNIMPLEMENTED(FATAL); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | return nullptr; |
| 287 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 288 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 289 | bool IsVerbose() override { |
| 290 | return verbose_; |
| 291 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 292 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 293 | void SetVerbose(bool val) { |
| 294 | verbose_ = val; |
| 295 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 296 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 297 | int GetMinSdkVersion() override { |
| 298 | return 0; |
| 299 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 300 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 301 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | StdErrDiagnostics diagnostics_; |
| 303 | bool verbose_ = false; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 304 | }; |
| 305 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 306 | } // namespace |
| 307 | |
| 308 | // Entry point for dump command. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | int Dump(const std::vector<StringPiece>& args) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 310 | bool verbose = false; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 311 | bool no_values = false; |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 312 | DumpOptions options; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 313 | Flags flags = Flags() |
| 314 | .OptionalSwitch("--no-values", |
| 315 | "Suppresses output of values when displaying resource tables.", |
| 316 | &no_values) |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 317 | .OptionalFlag("--file", "Dumps the specified file from the APK passed as arg.", |
| 318 | &options.file_to_dump_path) |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 319 | .OptionalSwitch("-v", "increase verbosity of output", &verbose); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | if (!flags.Parse("aapt2 dump", args, &std::cerr)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 321 | return 1; |
| 322 | } |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 323 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 324 | DumpContext context; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | context.SetVerbose(verbose); |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 326 | |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 327 | options.print_options.show_sources = true; |
| 328 | options.print_options.show_values = !no_values; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 329 | for (const std::string& arg : flags.GetArgs()) { |
Adam Lesinski | da9eba3 | 2018-02-13 16:44:10 -0800 | [diff] [blame] | 330 | if (!TryDumpFile(&context, arg, options)) { |
Pierre Lecesne | aadf27e | 2017-05-05 14:58:21 +0100 | [diff] [blame] | 331 | return 1; |
| 332 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | } |
| 334 | return 0; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | } // namespace aapt |