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