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
use super::bundletool;
use crate::error::*;
use std::path::{Path, PathBuf};

/// ## Extract device-specific APKs from an existing APK set
///
/// If you have an existing APK set and you want to extract from it a subset of APKs
/// that target a specific device configuration, you can use the `extract-apk` command
/// and specify a device specification JSON, as follows:
///
/// ```sh
/// `bundletool extract-apks
/// --apks=/MyApp/my_existing_APK_set.apks
/// --output-dir=/MyApp/my_pixel2_APK_set.apks
/// --device-spec=/MyApp/bundletool/pixel2.json`
/// ```
pub struct ExtractApks {
    apks: PathBuf,
    device_spec: PathBuf,
    output_dir: PathBuf,
}

impl ExtractApks {
    /// Specifies the path to the device spec file (from get-device-spec or constructed
    /// manually) to use for matching
    pub fn new(apks: &Path, device_spec: &Path, output_dir: &Path) -> Self {
        Self {
            apks: apks.to_owned(),
            device_spec: device_spec.to_owned(),
            output_dir: output_dir.to_owned(),
        }
    }

    /// Runs `bundletool` commands to extract apks on your device or emulator
    pub fn run(&self) -> Result<()> {
        let mut extract_apks = bundletool()?;
        extract_apks.arg("--apks");
        extract_apks.arg(&self.apks);
        extract_apks.arg("--device-spec");
        extract_apks.arg(&self.device_spec);
        extract_apks.arg("--output-dir");
        extract_apks.arg(&self.output_dir);
        extract_apks.output_err(true)?;
        Ok(())
    }
}