android_tools_rs/bundletool/
install_apks.rs

1use crate::error::*;
2use std::path::{Path, PathBuf};
3use std::process::Command;
4
5/// ## Deploy APKs to a connected device
6/// After you generate a set of APKs, bundletool can deploy the right combination of APKs
7/// from that set to a connected device For example, if you have a connected device
8/// running Android 5.0 (API level 21) or higher, bundletool pushes the base APK,
9/// feature module APKs, and configuration APKs required to run your app on that device.
10/// Alternatively, if your connected device is running Android 4.4 (API level 20) or
11/// lower, bundletool looks for a compatible multi-APK and deploys it to your device.
12/// To deploy your app from an APK set, use the install-apks command and specify the path
13/// of the APK set using the --apks=/path/to/apks flag, as shown below. (If you have
14/// multiple devices connected, specify a target device by adding the
15/// --device-id=serial-id flag.)
16#[derive(Debug, Default)]
17pub struct InstallApks {
18    apks: PathBuf,
19    local_testing: bool,
20    device_id: Option<String>,
21}
22
23impl InstallApks {
24    /// Specifies path to set of apks
25    pub fn new(apks: &Path) -> Self {
26        Self {
27            apks: apks.to_owned(),
28            ..Default::default()
29        }
30    }
31
32    /// If you're using the --local-testing flag with the build-apks command, for local
33    /// testing to work correctly, you need to use install-apks to install your APKs
34    pub fn local_testing(&mut self, local_testing: bool) -> &mut Self {
35        self.local_testing = local_testing;
36        self
37    }
38
39    /// If you have multiple devices connected, specify a target device by adding the
40    /// --device-id=serial-id flag
41    pub fn device_id(&mut self, device_id: String) -> &mut Self {
42        self.device_id = Some(device_id);
43        self
44    }
45
46    pub fn run(&self) -> Result<()> {
47        let mut install_apks = Command::new("java");
48        install_apks.arg("-jar");
49        if let Ok(bundletool_path) = std::env::var("BUNDLETOOL_PATH") {
50            install_apks.arg(bundletool_path);
51        } else {
52            return Err(Error::BundletoolNotFound);
53        }
54        install_apks.arg("install-apks");
55        install_apks.arg("--apks");
56        install_apks.arg(&self.apks);
57        if self.local_testing {
58            install_apks.arg("--local-testing");
59        }
60        if let Some(device_id) = &self.device_id {
61            install_apks.arg("--device-id").arg(device_id);
62        }
63        install_apks.output_err(true)?;
64        Ok(())
65    }
66}
67
68// #[cfg(test)]
69// mod tests {
70//     use super::*;
71
72//     #[test]
73//     fn new() {
74//         // TODO: Test install_apks
75// InstallApks::new(Path::new("\\target\\android\\debug\\threed.apks")).run().unwrap();
76//     }
77// }