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
use crate::{
commands::android::{get_libs_in_dir, recursively_define_needed_libs, search_dylibs},
error::*,
tools::AndroidNdk,
types::{AndroidTarget, IntoRustTriple, Profile},
};
use std::path::{Path, PathBuf};
pub fn add_libs_into_aapt2(
ndk: &AndroidNdk,
lib_path: &Path,
build_target: AndroidTarget,
profile: Profile,
min_sdk_version: u32,
build_dir: &Path,
target_dir: &Path,
) -> Result<PathBuf> {
let mut system_libs = Vec::new();
let sysroot_platform_lib_dir = ndk.sysroot_platform_lib_dir(build_target, min_sdk_version)?;
for lib in get_libs_in_dir(&sysroot_platform_lib_dir)? {
system_libs.push(lib);
}
let build_path = target_dir
.join(build_target.rust_triple())
.join(profile.as_ref());
let mut dylibs_paths = search_dylibs(&build_path.join("build"))?;
dylibs_paths.push(build_path.join("tools"));
let lib_name = lib_path.file_name().unwrap().to_str().unwrap().to_owned();
let mut needed_libs = vec![];
recursively_define_needed_libs(
(lib_name, lib_path.to_owned()),
&ndk.toolchain_bin("readelf", build_target)?,
&ndk.sysroot_lib_dir(build_target)?.join("libc++_shared.so"),
&system_libs,
&dylibs_paths,
&mut needed_libs,
)?;
let abi = build_target.android_abi();
let out_dir = build_dir.join("lib").join(abi);
for (_lib_name, lib_path) in needed_libs {
add_lib_aapt2(&lib_path, &out_dir)?;
}
Ok(out_dir)
}
pub fn add_lib_aapt2(lib_path: &Path, out_dir: &Path) -> Result<()> {
if !lib_path.exists() {
return Err(Error::PathNotFound(lib_path.to_owned()));
}
std::fs::create_dir_all(&out_dir)?;
let filename = lib_path.file_name().unwrap();
let mut options = fs_extra::file::CopyOptions::new();
options.overwrite = true;
fs_extra::file::copy(&lib_path, out_dir.join(&filename), &options)?;
Ok(())
}