Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apk: Include recursive libraries via gradle as well #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions apk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,10 @@ impl Apk {
}

pub fn add_lib(&mut self, target: Target, path: &Path) -> Result<()> {
let name = path
.file_name()
.context("invalid path")?
.to_str()
.context("invalid path")?;
let name = path.file_name().context("invalid path")?;
self.zip.add_file(
path,
&Path::new("lib").join(target.android_abi()).join(name),
&Path::new("lib").join(target.as_str()).join(name),
ZipFileOptions::Compressed,
)
}
Expand Down
2 changes: 1 addition & 1 deletion apk/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum Target {

impl Target {
/// Identifier used in the NDK to refer to the ABI
pub fn android_abi(self) -> &'static str {
pub fn as_str(self) -> &'static str {
match self {
Self::Arm64V8a => "arm64-v8a",
Self::ArmV7a => "armeabi-v7a",
Expand Down
62 changes: 33 additions & 29 deletions xbuild/src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cargo::CrateType;
use crate::download::DownloadManager;
use crate::task::TaskRunner;
use crate::{BuildEnv, Format, Opt, Platform};
use anyhow::{Context, Result};
use anyhow::{ensure, Context, Result};
use apk::Apk;
use appbundle::AppBundle;
use appimage::AppImage;
Expand Down Expand Up @@ -71,27 +71,10 @@ pub fn build(env: &BuildEnv) -> Result<()> {
}
Platform::Android => {
let out = platform_dir.join(format!("{}.{}", env.name(), env.target().format()));
if env.config().android().gradle {
crate::gradle::build(env, &out)?;
runner.end_verbose_task();
return Ok(());
} else {
let mut apk = Apk::new(
out,
env.config().android().manifest.clone(),
env.target().opt() != Opt::Debug,
)?;
apk.add_res(env.icon(), &env.android_jar())?;
ensure!(has_lib, "Android APKs/AABs require a library");

for asset in &env.config().android().assets {
let path = env.cargo().package_root().join(asset.path());
let mut libraries = vec![];

if !asset.optional() || path.exists() {
apk.add_asset(&path, asset.alignment().to_zip_file_options())?
}
}

if has_lib {
for target in env.target().compile_targets() {
let arch_dir = platform_dir.join(target.arch().to_string());
let cargo_dir = arch_dir.join("cargo");
Expand Down Expand Up @@ -120,8 +103,7 @@ pub fn build(env: &BuildEnv) -> Result<()> {
)
})?;
search_paths.push(deps_dir);
let search_paths =
search_paths.iter().map(AsRef::as_ref).collect::<Vec<_>>();
let search_paths = search_paths.iter().map(AsRef::as_ref).collect::<Vec<_>>();

let ndk_sysroot_libs = ndk.join("usr/lib").join(target.ndk_triple());
let provided_libs_paths = [
Expand All @@ -148,7 +130,7 @@ pub fn build(env: &BuildEnv) -> Result<()> {
.cargo()
.package_root()
.join(runtime_lib_path)
.join(target.android_abi().android_abi());
.join(target.android_abi().as_str());
let entries = std::fs::read_dir(abi_dir)?;
for entry in entries {
let entry = entry?;
Expand All @@ -168,8 +150,7 @@ pub fn build(env: &BuildEnv) -> Result<()> {

// Collect the names of all libraries that are available on Android
for provided_libs_path in provided_libs_paths {
included_libs
.extend(xcommon::llvm::find_libs_in_dir(provided_libs_path)?);
included_libs.extend(xcommon::llvm::find_libs_in_dir(provided_libs_path)?);
}

// libc++_shared is bundled with the NDK but not available on-device
Expand All @@ -178,7 +159,7 @@ pub fn build(env: &BuildEnv) -> Result<()> {
let mut needs_cpp_shared = false;

for lib in explicit_libs {
apk.add_lib(target.android_abi(), &lib)?;
libraries.push((target.android_abi(), lib.clone()));

let (extra_libs, cpp_shared) = xcommon::llvm::list_needed_libs_recursively(
&lib,
Expand All @@ -194,17 +175,40 @@ pub fn build(env: &BuildEnv) -> Result<()> {
)
})?;
needs_cpp_shared |= cpp_shared;
for lib in &extra_libs {
apk.add_lib(target.android_abi(), lib)?;
for lib in extra_libs {
libraries.push((target.android_abi(), lib));
}
}
if needs_cpp_shared {
let cpp_shared = ndk_sysroot_libs.join("libc++_shared.so");
apk.add_lib(target.android_abi(), &cpp_shared)?;
libraries.push((target.android_abi(), cpp_shared));
}
}

if env.config().android().gradle {
crate::gradle::build(env, libraries, &out)?;
runner.end_verbose_task();
return Ok(());
} else {
let mut apk = Apk::new(
out,
env.config().android().manifest.clone(),
env.target().opt() != Opt::Debug,
)?;
apk.add_res(env.icon(), &env.android_jar())?;

for asset in &env.config().android().assets {
let path = env.cargo().package_root().join(asset.path());

if !asset.optional() || path.exists() {
apk.add_asset(&path, asset.alignment().to_zip_file_options())?
}
}

for (target, lib) in libraries {
apk.add_lib(target, &lib)?;
}

apk.finish(env.target().signer().cloned())?;
}
}
Expand Down
18 changes: 8 additions & 10 deletions xbuild/src/gradle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cargo::CrateType;
use crate::{task, BuildEnv, Format, Opt};
use anyhow::Result;
use std::path::Path;
use anyhow::{Context, Result};
use apk::Target;
use std::path::{Path, PathBuf};
use std::process::Command;

static BUILD_GRADLE: &[u8] = include_bytes!("./build.gradle");
Expand Down Expand Up @@ -33,7 +33,7 @@ pub fn prepare(env: &BuildEnv) -> Result<()> {
Ok(())
}

pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> Result<()> {
let platform_dir = env.platform_dir();
let gradle = platform_dir.join("gradle");
let app = gradle.join("app");
Expand Down Expand Up @@ -146,13 +146,11 @@ pub fn build(env: &BuildEnv, out: &Path) -> Result<()> {
}
}

for target in env.target().compile_targets() {
let arch_dir = platform_dir.join(target.arch().to_string());
let lib = env.cargo_artefact(&arch_dir.join("cargo"), target, CrateType::Cdylib)?;
let lib_name = lib.file_name().unwrap();
let lib_dir = jnilibs.join(target.android_abi().android_abi());
for (target, lib) in libraries {
let name = lib.file_name().context("invalid path")?;
let lib_dir = jnilibs.join(target.as_str());
std::fs::create_dir_all(&lib_dir)?;
std::fs::copy(&lib, lib_dir.join(lib_name))?;
std::fs::copy(&lib, lib_dir.join(name))?;
}

let opt = env.target().opt();
Expand Down
Loading