Skip to content

Added support for adding extra files to the APK. #141

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

Closed

Conversation

agrande
Copy link
Contributor

@agrande agrande commented Apr 16, 2021

This replaces #138 as a more generic solution.

# Path to a folder containing a hierarchy of extra files to be added at the root of the APK.
# All files will be added to the APK recursively.
# If not specified, no extra files will be added to the APK
extra_content = "path/to/extra_content_folder"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess assets/resources are intended to be relative paths included in version control, while in your use case it's probably an os specific path. Also this doesn't help select the right library for the target architecture. Are there additional use cases for extra-content?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting that I write the example as an absolute path?
In my particular use-case I do have the shared library file under my repository directory (although not included in version control), so I'm using a relative path.

Regarding selection of the right library for the target architecture, if the APK contains multiple architecture, I think what happens is that dlopen() already appropriately uses the correct set of paths (including the proper lib subfolders in the APK) depending on which architecture is used when running the app.

As for additional use cases, honestly, I can't think of any off the top of my head.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we produce fat apk's because that can lead to very large apks. We could add the feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvc94ch Apparently cargo-apk already produces fat apks when more target architectures are specified in build_targets. I don't know if separate APKs can be distributed anyway unless we dive into the new "app bundles" system.

Regarding assets/resources we have to be very careful that those still travel the normal aapt package -A/-S as they need additional processing and are not "zipped" into the target APK verbatim.

Copy link
Member

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this deal with symlinks?

fn add_entry(&self, base_path: &Path, path: &Path) -> Result<(), NdkError> {
let full_path = base_path.join(path);
if full_path.is_dir() {
for entry in fs::read_dir(full_path)? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we pass a directory to aapt add so that it adds them recursively?

If not you're better off building an array of filenames and adding them to the archive in one go, spawning an aapt add process per file can potentially become very costly and wasteful.

std::fs::copy(base_path.join(path), out)?;

let mut aapt = self.0.build_tool(bin!("aapt"))?;
aapt.arg("add").arg(self.0.unaligned_apk()).arg(path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make sure that aapt runs relative to the build directory here? There are environment variables dealing with moved target/ directories and I don't exactly remember how they are handled here, if we keep running in the right PWD.

If we do that this might as well run aapt with base_path as PWD.

Comment on lines +185 to +187
} else if full_path.is_file() {
self.add_file(base_path, path)?;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need an error if it's not a directory nor a file (ie. a special file like a character device) just in case?


pub fn add_content_dir_recursively(&self, content_dir: &Path) -> Result<(), NdkError> {
if !content_dir.is_dir() {
return Err(NdkError::PathInvalid(content_dir.to_path_buf()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say something along the lines of "Is not a directory". That way the path can either not exist at all, or be a file. Invalid is a tad too generic IMO.

@MarijnS95
Copy link
Member

One thing to note while seeing #142 pop up just a couple hours ago: a generic codepath might not be desired for libraries. add_lib_recursively takes care of using readelf to find and embed extra dynamic library dependencies, which you would now have to explicitly specify/copy yourself. For example where your Rust library does not need libc++_shared.so from the NDK but one of the copied libs does.

@agrande
Copy link
Contributor Author

agrande commented Apr 20, 2021

#142 looks good to me, I'll test it against my use-case, but it's probably a better solutions for various reasons:

  • automatic dependencies scan, as you mentioned
  • there may not be that many additional use-cases for adding arbitrary files, and providing the feature may push users to do things wrongly (regarding resources and assets)
  • it has a smaller code footprint

@MarijnS95
Copy link
Member

@agrande @kanerogers then I prefer #138 which was here first and takes care of the target appropriately by only including the target ABIs (can still be multiple!) that are actually defined in Cargo.toml.

@agrande
Copy link
Contributor Author

agrande commented Apr 20, 2021

Ok, then, I'll re-open and modify #138 to use add_lib_recursively instead, and specify the directory through config.

@agrande agrande closed this Apr 20, 2021
@kanerogers
Copy link

@agrande @kanerogers then I prefer #138 which was here first and takes care of the target appropriately by only including the target ABIs (can still be multiple!) that are actually defined in Cargo.toml.

Ah, that's a very good point. I hadn't thought of that!

@agrande agrande deleted the feature/additional-apk-content branch July 17, 2021 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants