android_tools/aapt2/
dump.rs

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