cargo_mobile2/android/
apk.rs

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::path::PathBuf;

use colored::Colorize;
use heck::ToUpperCamelCase;
use thiserror::Error;

use super::{config::Config, env::Env, jnilibs, target::Target};
use crate::{
    android::jnilibs::JniLibs,
    opts::{NoiseLevel, Profile},
    util::{
        cli::{Report, Reportable},
        gradlew, last_modified, prefix_path,
    },
};

#[derive(Debug, Error)]
pub enum ApkError {
    #[error(transparent)]
    LibSymlinkCleaningFailed(jnilibs::RemoveBrokenLinksError),
    #[error("Failed to assemble APK: {0}")]
    AssembleFailed(#[from] std::io::Error),
}

impl Reportable for ApkError {
    fn report(&self) -> Report {
        match self {
            Self::LibSymlinkCleaningFailed(err) => err.report(),
            Self::AssembleFailed(err) => Report::error("Failed to assemble APK", err),
        }
    }
}

pub fn apks_paths(config: &Config, profile: Profile, flavor: &str) -> Vec<PathBuf> {
    profile
        .suffixes()
        .iter()
        .map(|suffix| {
            prefix_path(
                config.project_dir(),
                format!(
                    "app/build/outputs/apk/{}/{}/app-{}-{}.{}",
                    flavor,
                    profile.as_str(),
                    flavor,
                    suffix,
                    "apk"
                ),
            )
        })
        .collect()
}

/// Builds APK(s) and returns the built APK(s) paths
pub fn build(
    config: &Config,
    env: &Env,
    noise_level: NoiseLevel,
    profile: Profile,
    targets: Vec<&Target>,
    split_per_abi: bool,
) -> Result<Vec<PathBuf>, ApkError> {
    JniLibs::remove_broken_links(config).map_err(ApkError::LibSymlinkCleaningFailed)?;

    let build_ty = profile.as_str().to_upper_camel_case();

    let gradle_args = if split_per_abi {
        targets
            .iter()
            .map(|t| format!("assemble{}{}", t.arch_upper_camel_case(), build_ty))
            .collect()
    } else {
        let mut args = vec![format!("assembleUniversal{}", build_ty)];

        if !targets.is_empty() {
            args.extend_from_slice(&[
                format!(
                    "-PabiList={}",
                    targets.iter().map(|t| t.abi).collect::<Vec<_>>().join(",")
                ),
                format!(
                    "-ParchList={}",
                    targets.iter().map(|t| t.arch).collect::<Vec<_>>().join(",")
                ),
                format!(
                    "-PtargetList={}",
                    targets
                        .iter()
                        .map(|t| t.triple.split('-').next().unwrap())
                        .collect::<Vec<_>>()
                        .join(",")
                ),
            ])
        }

        args
    };

    gradlew(config, env)
        .before_spawn(move |cmd| {
            cmd.args(&gradle_args).arg(match noise_level {
                NoiseLevel::Polite => "--warn",
                NoiseLevel::LoudAndProud => "--info",
                NoiseLevel::FranklyQuitePedantic => "--debug",
            });
            Ok(())
        })
        .start()
        .inspect_err(|err| {
            if err.kind() == std::io::ErrorKind::NotFound {
               log::error!("`gradlew` not found. Make sure you have the Android SDK installed and added to your PATH");
            }
        })?
        .wait()?;

    let mut outputs = Vec::new();
    if split_per_abi {
        let paths = targets
            .iter()
            .map(|t| {
                apks_paths(config, profile, t.arch)
                    .into_iter()
                    .reduce(last_modified)
                    .unwrap()
            })
            .collect::<Vec<_>>();
        outputs.extend(paths);
    } else {
        let path = apks_paths(config, profile, "universal")
            .into_iter()
            .reduce(last_modified)
            .unwrap();
        outputs.push(path);
    }

    Ok(outputs)
}

pub mod cli {
    use super::*;
    pub fn build(
        config: &Config,
        env: &Env,
        noise_level: NoiseLevel,
        profile: Profile,
        targets: Vec<&Target>,
        split_per_abi: bool,
    ) -> Result<(), ApkError> {
        println!(
            "Building{} APK{} for {} ...\n",
            if split_per_abi { "" } else { " universal" },
            if split_per_abi { "(s)" } else { "" },
            targets
                .iter()
                .map(|t| t.triple.split('-').next().unwrap())
                .collect::<Vec<_>>()
                .join(", ")
        );

        let outputs = super::build(config, env, noise_level, profile, targets, split_per_abi)?;

        println!("\nFinished building APK(s):");
        for p in &outputs {
            println!("    {}", p.to_string_lossy().green(),);
        }
        Ok(())
    }
}