creator_tools/tools/aapt2/
dump.rs

1use std::path::{Path, PathBuf};
2use std::process::Command;
3
4/// # Dump
5/// Dump is used for printing information about the APK you generated using the link
6/// command. For example, the following command prints content from the resource table of
7/// the specified APK:
8///
9/// ```sh
10/// aapt2 dump resources output.apk
11/// ```
12///
13/// ## Dump syntax
14/// The general syntax for using dump is as follows:
15///
16/// ```sh
17/// aapt2 dump sub-command filename.apk [options]
18/// ```
19pub struct Aapt2Dump {
20    subcommand: SubCommand,
21    filename_apk: PathBuf,
22    /// Suppresses the output of values when displaying resource.
23    no_values: bool,
24    /// Suppresses the output of values when displaying resource.
25    file: Option<PathBuf>,
26    /// Increases verbosity of the output.
27    v: bool,
28}
29
30impl Aapt2Dump {
31    pub fn new(subcommand: SubCommand, filename_apk: &Path) -> Aapt2Dump {
32        Aapt2Dump {
33            subcommand,
34            filename_apk: filename_apk.to_owned(),
35            no_values: false,
36            file: None,
37            v: false,
38        }
39    }
40
41    pub fn no_values(&mut self, no_values: bool) -> &mut Self {
42        self.no_values = no_values;
43        self
44    }
45
46    pub fn file(&mut self, file: &Path) -> &mut Self {
47        self.file = Some(file.to_owned());
48        self
49    }
50
51    pub fn v(&mut self, v: bool) -> &mut Self {
52        self.v = v;
53        self
54    }
55
56    pub fn run(&self) {
57        let mut aapt2 = Command::new("aapt2");
58        aapt2.arg("dump");
59        aapt2.arg(self.subcommand.to_string());
60        aapt2.arg(&self.filename_apk);
61        if self.no_values {
62            aapt2.arg("--no-values");
63        }
64        if let Some(file) = &self.file {
65            aapt2.arg("--file").arg(file);
66        }
67        if self.v {
68            aapt2.arg("-v");
69        }
70    }
71}
72
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub enum SubCommand {
75    /// Print the contents of the AAPT2 Container (APC) generated during compilation.
76    Apc,
77    /// Print information extracted from the APK's manifest.
78    Badging,
79    /// Print every configuration used by a resource in the APK.
80    Configurations,
81    /// Print the APK's package name.
82    Packagename,
83    /// Print the permissions extracted from the APK's manifest.
84    Permissions,
85    /// Print the contents of the APK's resource table string pool.
86    Strings,
87    /// Print the parents of styles used in the APK.
88    Styleparents,
89    /// Print the contents of the APK's resource table.
90    Resources,
91    /// Print strings from the APK's compiled xml.
92    Xmlstrings,
93    /// Print a tree of the APK's compiled xml.
94    Xmltree,
95}
96
97impl std::fmt::Display for SubCommand {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        match *self {
100            Self::Apc => write!(f, "apc"),
101            Self::Badging => write!(f, "badging"),
102            Self::Configurations => write!(f, "configurations"),
103            Self::Packagename => write!(f, "packagename"),
104            Self::Permissions => write!(f, "permissions"),
105            Self::Strings => write!(f, "strings"),
106            Self::Styleparents => write!(f, "styleparents"),
107            Self::Resources => write!(f, "resources"),
108            Self::Xmlstrings => write!(f, "xmlstrings"),
109            Self::Xmltree => write!(f, "xmltree"),
110        }
111    }
112}