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

Add support for dynamically loaded libraries #138

Merged
merged 19 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 17 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
2 changes: 2 additions & 0 deletions cargo-apk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Added `runtime_libs` path to android metadata for packaging extra dynamic libraries into the apk.

# 0.7.0 (2021-05-10)

- Added `cargo apk check`. Useful for compile-testing crates that contain C/C++ dependencies or
Expand Down
5 changes: 5 additions & 0 deletions cargo-apk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ icon = "@mipmap/ic_launcher"
# Defaults to the compiled artifact's name.
label = "Application Name"

# Folder containing extra shared libraries intended to be dynamically loaded at runtime.
# Files matching `libs_folder/${android_abi}/*.so` are added to the apk
# according to the specified build_targets.
runtime_libs = "path/to/libs_folder"
agrande marked this conversation as resolved.
Show resolved Hide resolved

# See https://developer.android.com/guide/topics/manifest/meta-data-element
#
# Note: there can be several .meta_data entries.
Expand Down
43 changes: 21 additions & 22 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,23 @@ impl<'a> ApkBuilder<'a> {
manifest.application.label = artifact.name().to_string();
}

let assets = self.manifest.assets.as_ref().map(|assets| {
dunce::simplified(
&self
.cmd
.manifest()
.parent()
.expect("invalid manifest path")
.join(&assets),
)
.to_owned()
});
let resources = self.manifest.resources.as_ref().map(|res| {
dunce::simplified(
&self
.cmd
.manifest()
.parent()
.expect("invalid manifest path")
.join(&res),
)
.to_owned()
});
let crate_path = self.cmd.manifest().parent().expect("invalid manifest path");

let assets = self
.manifest
.assets
.as_ref()
.map(|assets| dunce::simplified(&crate_path.join(&assets)).to_owned());
let resources = self
.manifest
.resources
.as_ref()
.map(|res| dunce::simplified(&crate_path.join(&res)).to_owned());
let runtime_libs = self
.manifest
.runtime_libs
.as_ref()
.map(|libs| dunce::simplified(&crate_path.join(&libs)).to_owned());
let apk_name = self
.manifest
.apk_name
Expand Down Expand Up @@ -190,6 +185,10 @@ impl<'a> ApkBuilder<'a> {
.collect::<Vec<_>>();

apk.add_lib_recursively(&artifact, *target, libs_search_paths.as_slice())?;

if let Some(runtime_libs) = &runtime_libs {
apk.add_runtime_libs(runtime_libs, *target, libs_search_paths.as_slice())?;
}
}

Ok(apk.align()?.sign(config.ndk.debug_key()?)?)
Expand Down
3 changes: 3 additions & 0 deletions cargo-apk/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Manifest {
pub build_targets: Vec<Target>,
pub assets: Option<String>,
pub resources: Option<String>,
pub runtime_libs: Option<String>,
}

impl Manifest {
Expand All @@ -30,6 +31,7 @@ impl Manifest {
build_targets: metadata.build_targets,
assets: metadata.assets,
resources: metadata.resources,
runtime_libs: metadata.runtime_libs,
})
}
}
Expand Down Expand Up @@ -59,4 +61,5 @@ struct AndroidMetadata {
build_targets: Vec<Target>,
assets: Option<String>,
resources: Option<String>,
runtime_libs: Option<String>,
}
2 changes: 2 additions & 0 deletions ndk-build/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Added `add_runtime_libs` function for including extra dynamic libraries in the APK.

# 0.3.0 (2021-05-10)

- New `ApkConfig` field `apk_name` is now used for APK file naming, instead of the application label.
Expand Down
19 changes: 19 additions & 0 deletions ndk-build/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::error::NdkError;
use crate::manifest::AndroidManifest;
use crate::ndk::{Key, Ndk};
use crate::target::Target;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand Down Expand Up @@ -94,6 +96,23 @@ impl<'a> UnalignedApk<'a> {
Ok(())
}

pub fn add_runtime_libs(
&self,
path: &Path,
target: Target,
search_paths: &[&Path],
) -> Result<(), NdkError> {
let abi_dir = path.join(target.android_abi());
for entry in fs::read_dir(abi_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension() == Some(OsStr::new("so")) {
self.add_lib_recursively(&path, target, search_paths)?;
}
}
Ok(())
}

pub fn align(self) -> Result<UnsignedApk<'a>, NdkError> {
let mut zipalign = self.0.build_tool(bin!("zipalign"))?;
zipalign
Expand Down