use super::aapt2_tool;
use crate::error::*;
use std::path::{Path, PathBuf};
#[derive(Clone, Default)]
pub struct Aapt2Optimize {
output_apk: PathBuf,
output_dir: PathBuf,
config_xml_file: Option<PathBuf>,
apk_artifacts: bool,
target_densities: Option<String>,
resources_config_path: Option<PathBuf>,
configs_to_include: Option<String>,
split: Option<PathBuf>,
keep_artifacts: Option<String>,
enable_sparse_encoding: bool,
collapse_resource_name: bool,
shorten_resource_paths: bool,
resource_path_shortening_map: Option<PathBuf>,
verbose: bool,
help: bool,
}
impl Aapt2Optimize {
pub fn new(output_apk: &Path, output_dir: &Path) -> Self {
Self {
output_apk: output_apk.to_owned(),
output_dir: output_dir.to_owned(),
..Default::default()
}
}
pub fn config_xml_file(&mut self, config_xml_file: &Path) -> &mut Self {
self.config_xml_file = Some(config_xml_file.to_owned());
self
}
pub fn apk_artifacts(&mut self, apk_artifacts: bool) -> &mut Self {
self.apk_artifacts = apk_artifacts;
self
}
pub fn target_densities(&mut self, target_densities: &str) -> &mut Self {
self.target_densities = Some(target_densities.to_owned());
self
}
pub fn resources_config_path(&mut self, resources_config_path: &Path) -> &mut Self {
self.resources_config_path = Some(resources_config_path.to_owned());
self
}
pub fn configs_to_include(&mut self, configs_to_include: &str) -> &mut Self {
self.configs_to_include = Some(configs_to_include.to_owned());
self
}
pub fn split(&mut self, split: &Path) -> &mut Self {
self.split = Some(split.to_owned());
self
}
pub fn keep_artifacts(&mut self, keep_artifacts: &str) -> &mut Self {
self.keep_artifacts = Some(keep_artifacts.to_owned());
self
}
pub fn enable_sparse_encoding(&mut self, enable_sparse_encoding: bool) -> &mut Self {
self.enable_sparse_encoding = enable_sparse_encoding;
self
}
pub fn collapse_resource_name(&mut self, collapse_resource_name: bool) -> &mut Self {
self.collapse_resource_name = collapse_resource_name;
self
}
pub fn shorten_resource_paths(&mut self, shorten_resource_paths: bool) -> &mut Self {
self.shorten_resource_paths = shorten_resource_paths;
self
}
pub fn resource_path_shortening_map(
&mut self,
resource_path_shortening_map: &Path,
) -> &mut Self {
self.resource_path_shortening_map = Some(resource_path_shortening_map.to_owned());
self
}
pub fn verbose(&mut self, verbose: bool) -> &mut Self {
self.verbose = verbose;
self
}
pub fn help(&mut self, help: bool) -> &mut Self {
self.help = help;
self
}
pub fn run(self) -> Result<()> {
let mut aapt2 = aapt2_tool()?;
aapt2.arg("optimize");
aapt2.arg("-o").arg(&self.output_apk);
aapt2.arg("-d").arg(&self.output_dir);
if let Some(config_xml_file) = self.config_xml_file {
aapt2.arg("-x").arg(config_xml_file);
}
if self.apk_artifacts {
aapt2.arg("-p");
}
if let Some(target_densities) = self.target_densities {
aapt2.arg("--target_densities").arg(target_densities);
}
if let Some(resources_config_path) = self.resources_config_path {
aapt2
.arg("--resources_config_path")
.arg(resources_config_path);
}
if let Some(configs_to_include) = self.configs_to_include {
aapt2.arg("-c").arg(configs_to_include);
}
if let Some(split) = self.split {
aapt2.arg("--split").arg(split);
}
if let Some(keep_artifacts) = self.keep_artifacts {
aapt2.arg("--keep-artifacts").arg(keep_artifacts);
}
if self.enable_sparse_encoding {
aapt2.arg("--enable-sparse-encoding");
}
if self.collapse_resource_name {
aapt2.arg("--collapse-resource-name");
}
if self.shorten_resource_paths {
aapt2.arg("--shorten-resource-paths");
}
if let Some(resource_path_shortening_map) = self.resource_path_shortening_map {
aapt2
.arg("--resource-path-shortening-map")
.arg(resource_path_shortening_map);
}
if self.verbose {
aapt2.arg("-v");
}
if self.help {
aapt2.arg("-h");
}
aapt2.output_err(true)?;
Ok(())
}
}