android_tools_rs/aapt2/
dump.rs

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