android_tools_rs/bundletool/
get_size_total.rs

1use crate::error::*;
2use std::path::{Path, PathBuf};
3use std::process::Command;
4
5/// To measure the estimated download sizes of APKs in an APK set as they would be served
6/// compressed over-the-wire, use the get-size total command:
7///
8/// ```xml
9/// bundletool get-size total --apks=/MyApp/my_app.apks
10/// ```
11///
12/// You can modify the behavior of the get-size total command using the following flags:
13pub struct GetSizeTotal {
14    apks: PathBuf,
15    device_spec: Option<PathBuf>,
16    dimensions: Option<String>,
17    instant: bool,
18    modules: Option<String>,
19}
20
21impl GetSizeTotal {
22    /// (Required) Specifies the path to the existing APK set file whose download size is
23    /// measured.
24    pub fn new(apks: &Path) -> Self {
25        Self {
26            apks: apks.to_owned(),
27            device_spec: None,
28            dimensions: None,
29            instant: false,
30            modules: None,
31        }
32    }
33
34    /// Specifies the path to the device spec file (from get-device-spec or constructed
35    /// manually) to use for matching. You can specify a partial path to evaluate a set of
36    /// configurations.
37    pub fn device_spec(&mut self, device_spec: &Path) -> &mut Self {
38        self.device_spec = Some(device_spec.to_owned());
39        self
40    }
41
42    /// Specifies the dimensions used when computing the size estimates.
43    /// Accepts a comma-separated list of: SDK, ABI, SCREEN_DENSITY, and LANGUAGE. To
44    /// measure across all dimensions, specify ALL.
45    pub fn dimensions(&mut self, dimensions: String) -> &mut Self {
46        self.dimensions = Some(dimensions);
47        self
48    }
49
50    /// Measures the download size of the instant-enabled APKs instead of the installable
51    /// APKs. By default, bundletool measures the installable APK download sizes
52    pub fn instant(&mut self, instant: bool) -> &mut Self {
53        self.instant = instant;
54        self
55    }
56
57    /// Specifies a comma-separated list of modules in the APK set to consider in the
58    /// measurement. The bundletool command automatically includes any dependent modules
59    /// for the specified set. By default, the command measures the download size of
60    /// all modules installed during the first download.
61    pub fn modules(&mut self, modules: String) -> &mut Self {
62        self.modules = Some(modules);
63        self
64    }
65
66    pub fn run(&self) -> Result<()> {
67        let mut get_size_total = Command::new("java");
68        get_size_total.arg("-jar");
69        if let Ok(bundletool_path) = std::env::var("BUNDLETOOL_PATH") {
70            get_size_total.arg(bundletool_path);
71        } else {
72            return Err(Error::BundletoolNotFound);
73        }
74        get_size_total.arg("get-size");
75        get_size_total.arg("total");
76        get_size_total.arg("--apks");
77        get_size_total.arg(&self.apks);
78        if let Some(device_spec) = &self.device_spec {
79            get_size_total.arg("--device-spec").arg(device_spec);
80        }
81        if let Some(dimensions) = &self.dimensions {
82            get_size_total.arg("--dimensions").arg(dimensions);
83        }
84        if self.instant {
85            get_size_total.arg("--instant");
86        }
87        if let Some(modules) = &self.modules {
88            get_size_total.arg("--modules").arg(modules);
89        }
90        get_size_total.output_err(true)?;
91        Ok(())
92    }
93}