1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use super::bundletool;
use crate::error::*;
use std::path::{Path, PathBuf};

/// ## Deploy APKs to a connected device
///
/// After you generate a set of APKs, bundletool can deploy the right combination of APKs
/// from that set to a connected device.
///
/// For example, if you have a connected device running Android 5.0 (API level 21)
/// or higher, bundletool pushes the base APK, feature module APKs, and configuration
/// APKs required to run your app on that device. Alternatively, if your connected device
/// is running Android 4.4 (API level 20) or lower, `bundletool` looks for a compatible
/// multi-APK and deploys it to your device.
///
/// To deploy your app from an APK set, use theinstall-apks command and specify the path
/// of the APK set using the `--apks=/path/to/apks` flag, as shown below. (If you have
/// multiple devices connected, specify a target device by adding the
/// `--device-id=serial-id` flag.)
#[derive(Debug, Default)]
pub struct InstallApks {
    apks: PathBuf,
    local_testing: bool,
    device_id: Option<String>,
}

impl InstallApks {
    /// Specifies path to set of apks to install it on your device or emulator
    pub fn new(apks: &Path) -> Self {
        Self {
            apks: apks.to_owned(),
            ..Default::default()
        }
    }

    /// If you're using the `--local-testing` flag with the `build-apks` command, for
    /// local testing to work correctly, you need to use `install-apks` to install
    /// your APKs
    pub fn local_testing(&mut self, local_testing: bool) -> &mut Self {
        self.local_testing = local_testing;
        self
    }

    /// If you have multiple devices connected, specify a target device by adding the
    /// `--device-id=serial-id` flag
    pub fn device_id(&mut self, device_id: String) -> &mut Self {
        self.device_id = Some(device_id);
        self
    }

    /// Runs `bundletool` commands to install apks on your device or emulator
    pub fn run(&self) -> Result<()> {
        let mut install_apks = bundletool()?;
        install_apks.arg("install-apks");
        install_apks.arg("--apks");
        install_apks.arg(&self.apks);
        if self.local_testing {
            install_apks.arg("--local-testing");
        }
        if let Some(device_id) = &self.device_id {
            install_apks.arg("--device-id").arg(device_id);
        }
        install_apks.output_err(true)?;
        Ok(())
    }
}