This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
build.rs
353 lines (321 loc) · 11.9 KB
/
build.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
mod compile;
mod targets;
pub mod tempfile;
mod util;
use self::compile::SharedLibraries;
use crate::config::{AndroidConfig, AndroidTargetConfig};
use cargo::core::{Target, TargetKind, Workspace};
use cargo::util::process_builder::process;
use cargo::util::CargoResult;
use clap::ArgMatches;
use failure::format_err;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::{env, fs};
#[derive(Debug)]
pub struct BuildResult {
/// Mapping from target kind and target name to the built APK
pub target_to_apk_map: BTreeMap<(TargetKind, String), PathBuf>,
}
pub fn build(
workspace: &Workspace,
config: &AndroidConfig,
options: &ArgMatches,
) -> CargoResult<BuildResult> {
let root_build_dir = util::get_root_build_directory(workspace, config);
let shared_libraries =
compile::build_shared_libraries(workspace, config, options, &root_build_dir)?;
build_apks(config, &root_build_dir, shared_libraries)
}
fn build_apks(
config: &AndroidConfig,
root_build_dir: &PathBuf,
shared_libraries: SharedLibraries,
) -> CargoResult<BuildResult> {
// Create directory to hold final APKs which are signed using the debug key
let final_apk_dir = root_build_dir.join("apk");
fs::create_dir_all(&final_apk_dir)?;
// Paths of created APKs
let mut target_to_apk_map = BTreeMap::new();
// Build an APK for each cargo target
for (target, shared_libraries) in shared_libraries.shared_libraries.iter_all() {
let target_directory = util::get_target_directory(root_build_dir, target)?;
fs::create_dir_all(&target_directory)?;
// Determine Target Configuration
let target_config = config.resolve((target.kind().to_owned(), target.name().to_owned()))?;
//
// Run commands to produce APK
//
build_manifest(&target_directory, &config, &target_config, &target)?;
let build_tools_path = config
.sdk_path
.join("build-tools")
.join(&config.build_tools_version);
let aapt_path = build_tools_path.join("aapt");
let zipalign_path = build_tools_path.join("zipalign");
// Create unaligned APK which includes resources and assets
let unaligned_apk_name = format!("{}_unaligned.apk", target.name());
let unaligned_apk_path = target_directory.join(&unaligned_apk_name);
if unaligned_apk_path.exists() {
std::fs::remove_file(unaligned_apk_path)
.map_err(|e| format_err!("Unable to delete APK file. {}", e))?;
}
let mut aapt_package_cmd = process(&aapt_path);
aapt_package_cmd
.arg("package")
.arg("-F")
.arg(&unaligned_apk_name)
.arg("-M")
.arg("AndroidManifest.xml")
.arg("-I")
.arg(&config.android_jar_path);
if let Some(res_path) = target_config.res_path {
aapt_package_cmd.arg("-S").arg(res_path);
}
// Link assets
if let Some(assets_path) = &target_config.assets_path {
aapt_package_cmd.arg("-A").arg(assets_path);
}
aapt_package_cmd.cwd(&target_directory).exec()?;
// Add shared libraries to the APK
for shared_library in shared_libraries {
// Copy the shared library to the appropriate location in the target directory and with the appropriate name
// Note: that the type of slash used matters. This path is passed to aapt and the shared library
// will not load if backslashes are used.
let so_path = format!(
"lib/{}/{}",
&shared_library.abi.android_abi(),
shared_library.filename
);
let target_shared_object_path = target_directory.join(&so_path);
fs::create_dir_all(target_shared_object_path.parent().unwrap())?;
fs::copy(&shared_library.path, target_shared_object_path)?;
// Add to the APK
process(&aapt_path)
.arg("add")
.arg(&unaligned_apk_name)
.arg(so_path)
.cwd(&target_directory)
.exec()?;
}
// Determine the directory in which to place the aligned and signed APK
let target_apk_directory = match target.kind() {
TargetKind::Bin => final_apk_dir.clone(),
TargetKind::ExampleBin => final_apk_dir.join("examples"),
_ => unreachable!("Unexpected target kind"),
};
fs::create_dir_all(&target_apk_directory)?;
// Align apk
let final_apk_path = target_apk_directory.join(format!("{}.apk", target.name()));
process(&zipalign_path)
.arg("-f")
.arg("-v")
.arg("4")
.arg(&unaligned_apk_name)
.arg(&final_apk_path)
.cwd(&target_directory)
.exec()?;
// Find or generate a debug keystore for signing the APK
// We use the same debug keystore as used by the Android SDK. If it does not exist,
// then we create it using keytool which is part of the JRE/JDK
let android_directory = dirs::home_dir()
.ok_or_else(|| format_err!("Unable to determine home directory"))?
.join(".android");
fs::create_dir_all(&android_directory)?;
let keystore_path = android_directory.join("debug.keystore");
if !keystore_path.exists() {
// Generate key
let keytool_filename = if cfg!(target_os = "windows") {
"keytool.exe"
} else {
"keytool"
};
let keytool_path = find_java_executable(keytool_filename)?;
process(keytool_path)
.arg("-genkey")
.arg("-v")
.arg("-keystore")
.arg(&keystore_path)
.arg("-storepass")
.arg("android")
.arg("-alias")
.arg("androidebugkey")
.arg("-keypass")
.arg("android")
.arg("-dname")
.arg("CN=Android Debug,O=Android,C=US")
.arg("-keyalg")
.arg("RSA")
.arg("-keysize")
.arg("2048")
.arg("-validity")
.arg("10000")
.cwd(root_build_dir)
.exec()?;
}
// Sign the APK with the development certificate
util::script_process(
build_tools_path.join(format!("apksigner{}", util::EXECUTABLE_SUFFIX_BAT)),
)
.arg("sign")
.arg("--ks")
.arg(keystore_path)
.arg("--ks-pass")
.arg("pass:android")
.arg(&final_apk_path)
.cwd(&target_directory)
.exec()?;
target_to_apk_map.insert(
(target.kind().to_owned(), target.name().to_owned()),
final_apk_path,
);
}
Ok(BuildResult { target_to_apk_map })
}
/// Find an executable that is part of the Java SDK
fn find_java_executable(name: &str) -> CargoResult<PathBuf> {
// Look in PATH
env::var_os("PATH")
.and_then(|paths| {
env::split_paths(&paths)
.filter_map(|path| {
let filepath = path.join(name);
if fs::metadata(&filepath).is_ok() {
Some(filepath)
} else {
None
}
})
.next()
})
.or_else(||
// Look in JAVA_HOME
env::var_os("JAVA_HOME").and_then(|java_home| {
let filepath = PathBuf::from(java_home).join("bin").join(name);
if filepath.exists() {
Some(filepath)
} else {
None
}
}))
.ok_or_else(|| {
format_err!(
"Unable to find executable: '{}'. Configure PATH or JAVA_HOME with the path to the JRE or JDK.",
name
)
})
}
fn build_manifest(
path: &Path,
config: &AndroidConfig,
target_config: &AndroidTargetConfig,
target: &Target,
) -> CargoResult<()> {
let file = path.join("AndroidManifest.xml");
let mut file = File::create(&file)?;
// Building application attributes
let application_attrs = format!(
r#"
android:hasCode="false" android:label="{0}"{1}{2}{3}"#,
target_config.package_label,
target_config
.package_icon
.as_ref()
.map_or(String::new(), |a| format!(
r#"
android:icon="{}""#,
a
)),
if target_config.fullscreen {
r#"
android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen""#
} else {
""
},
target_config
.application_attributes
.as_ref()
.map_or(String::new(), |a| a.replace("\n", "\n "))
);
// Build activity attributes
let activity_attrs = format!(
r#"
android:name="android.app.NativeActivity"
android:label="{0}"
android:configChanges="orientation|keyboardHidden|screenSize" {1}"#,
target_config.package_label,
target_config
.activity_attributes
.as_ref()
.map_or(String::new(), |a| a.replace("\n", "\n "))
);
let uses_features = target_config
.features
.iter()
.map(|f| {
format!(
"\n\t<uses-feature android:name=\"{}\" android:required=\"{}\" {}/>",
f.name,
f.required,
f.version
.as_ref()
.map_or(String::new(), |v| format!(r#"android:version="{}""#, v))
)
})
.collect::<Vec<String>>()
.join(", ");
let uses_permissions = target_config
.permissions
.iter()
.map(|f| {
format!(
"\n\t<uses-permission android:name=\"{}\" {max_sdk_version}/>",
f.name,
max_sdk_version = f.max_sdk_version.map_or(String::new(), |v| format!(
r#"android:maxSdkVersion="{}""#,
v
))
)
})
.collect::<Vec<String>>()
.join(", ");
// Write final AndroidManifest
writeln!(
file,
r#"<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="{package}"
android:versionCode="{version_code}"
android:versionName="{version_name}">
<uses-sdk android:targetSdkVersion="{targetSdkVersion}" android:minSdkVersion="{minSdkVersion}" />
<uses-feature android:glEsVersion="{glEsVersion}" android:required="true"></uses-feature>{uses_features}{uses_permissions}
<application {application_attrs} >
<activity {activity_attrs} >
<meta-data android:name="android.app.lib_name" android:value="{target_name}" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>"#,
package = target_config.package_name.replace("-", "_"),
version_code = target_config.version_code,
version_name = target_config.version_name,
targetSdkVersion = config.target_sdk_version,
minSdkVersion = config.min_sdk_version,
glEsVersion = format!(
"0x{:04}{:04}",
target_config.opengles_version_major, target_config.opengles_version_minor
),
uses_features = uses_features,
uses_permissions = uses_permissions,
application_attrs = application_attrs,
activity_attrs = activity_attrs,
target_name = target.name(),
)?;
Ok(())
}