1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AAPT2_DUMP_H
#define AAPT2_DUMP_H
#include "Command.h"
#include "Debug.h"
#include "LoadedApk.h"
#include "dump/DumpManifest.h"
namespace aapt {
/**
* 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) {
SetDescription("Dump information about an APK or APC.");
}
text::Printer* GetPrinter() {
return printer_;
}
IDiagnostics* GetDiagnostics() {
return diag_;
}
Maybe<std::string> GetPackageName(LoadedApk* apk) {
xml::Element* manifest_el = apk->GetManifest()->root.get();
if (!manifest_el) {
GetDiagnostics()->Error(DiagMessage() << "No AndroidManifest.");
return Maybe<std::string>();
}
xml::Attribute* attr = manifest_el->FindAttribute({}, "package");
if (!attr) {
GetDiagnostics()->Error(DiagMessage() << "No package name.");
return Maybe<std::string>();
}
return attr->value;
}
/** 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(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_);
AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_);
}
int Action(const std::vector<std::string>& args) override;
private:
text::Printer* printer_;
IDiagnostics* diag_;
bool no_values_ = false;
bool verbose_ = false;
};
/** Easter egg command shown when users enter "badger" instead of "badging". */
class DumpBadgerCommand : public Command {
public:
explicit DumpBadgerCommand(text::Printer* printer) : Command("badger"), printer_(printer) {
}
int Action(const std::vector<std::string>& args) override;
private:
text::Printer* printer_;
const static char kBadgerData[2925];
};
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());
}
private:
DumpManifestOptions options_;
};
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;
};
class DumpPackageNameCommand : public DumpApkCommand {
public:
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;
};
/** Prints the graph of parents of a style in an APK. */
class DumpStyleParentCommand : public DumpApkCommand {
public:
explicit DumpStyleParentCommand(text::Printer* printer, IDiagnostics* diag)
: DumpApkCommand("styleparents", printer, diag) {
SetDescription("Print the parents of a style in an APK.");
AddRequiredFlag("--style", "The name of the style to print", &style_);
}
int Dump(LoadedApk* apk) override;
private:
std::string style_;
};
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 Dump(LoadedApk* apk) override;
private:
bool no_values_ = false;
bool verbose_ = false;
};
class DumpXmlStringsCommand : public DumpApkCommand {
public:
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 Dump(LoadedApk* apk) override;
private:
std::vector<std::string> files_;
};
class DumpXmlTreeCommand : public DumpApkCommand {
public:
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 Dump(LoadedApk* apk) override;
private:
std::vector<std::string> files_;
};
/** The default dump command. Performs no action because a subcommand is required. */
class DumpCommand : public Command {
public:
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<DumpStyleParentCommand>(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);
// TODO(b/120609160): Add aapt2 overlayable dump command
}
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
#endif // AAPT2_DUMP_H
|