From 7e349943fc3a106748ec634bea0f0626e020dcd6 Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Tue, 30 Aug 2022 16:03:23 +0200 Subject: [PATCH 1/4] First stab at deduping libraries --- cargo-apk/src/apk.rs | 4 +-- ndk-build/src/apk.rs | 56 ++++++++++++++++++++++++---------------- ndk-build/src/readelf.rs | 2 +- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/cargo-apk/src/apk.rs b/cargo-apk/src/apk.rs index de09fdca..9e9cad9f 100644 --- a/cargo-apk/src/apk.rs +++ b/cargo-apk/src/apk.rs @@ -171,7 +171,7 @@ impl<'a> ApkBuilder<'a> { manifest, disable_aapt_compression: is_debug_profile, }; - let apk = config.create_apk()?; + let mut apk = config.create_apk()?; for target in &self.build_targets { let triple = target.rust_triple(); @@ -231,7 +231,7 @@ impl<'a> ApkBuilder<'a> { (None, false) => return Err(Error::MissingReleaseKey(profile_name.to_owned())), }; - Ok(apk.align()?.sign(signing_key)?) + Ok(apk.add_pending_libs_and_align()?.sign(signing_key)?) } pub fn run(&self, artifact: &Artifact) -> Result<(), Error> { diff --git a/ndk-build/src/apk.rs b/ndk-build/src/apk.rs index 2ba1e047..8de20ee4 100644 --- a/ndk-build/src/apk.rs +++ b/ndk-build/src/apk.rs @@ -6,6 +6,7 @@ use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; +use std::collections::HashSet; pub struct ApkConfig { pub ndk: Ndk, @@ -68,24 +69,30 @@ impl ApkConfig { return Err(NdkError::CmdFailed(aapt)); } - Ok(UnalignedApk(self)) + Ok(UnalignedApk { + config: self, + pending_libs: HashSet::default() + }) } } -pub struct UnalignedApk<'a>(&'a ApkConfig); +pub struct UnalignedApk<'a> { + config: &'a ApkConfig, + pending_libs: HashSet, +} impl<'a> UnalignedApk<'a> { pub fn config(&self) -> &ApkConfig { - self.0 + self.config } - pub fn add_lib(&self, path: &Path, target: Target) -> Result<(), NdkError> { + pub fn add_lib(&mut self, path: &Path, target: Target) -> Result<(), NdkError> { if !path.exists() { return Err(NdkError::PathNotFound(path.into())); } let abi = target.android_abi(); let lib_path = Path::new("lib").join(abi).join(path.file_name().unwrap()); - let out = self.0.build_dir.join(&lib_path); + let out = self.config.build_dir.join(&lib_path); std::fs::create_dir_all(out.parent().unwrap())?; std::fs::copy(path, out)?; @@ -94,23 +101,13 @@ impl<'a> UnalignedApk<'a> { // Otherwise, it results in a runtime error when loading the NativeActivity `.so` library. let lib_path_unix = lib_path.to_str().unwrap().replace('\\', "/"); - let mut aapt = self.0.build_tool(bin!("aapt"))?; - aapt.arg("add"); - - if self.0.disable_aapt_compression { - aapt.arg("-0").arg(""); - } - - aapt.arg(self.0.unaligned_apk()).arg(lib_path_unix); + self.pending_libs.insert(lib_path_unix); - if !aapt.status()?.success() { - return Err(NdkError::CmdFailed(aapt)); - } Ok(()) } pub fn add_runtime_libs( - &self, + &mut self, path: &Path, target: Target, search_paths: &[&Path], @@ -126,18 +123,33 @@ impl<'a> UnalignedApk<'a> { Ok(()) } - pub fn align(self) -> Result, NdkError> { - let mut zipalign = self.0.build_tool(bin!("zipalign"))?; + pub fn add_pending_libs_and_align(self) -> Result, NdkError> { + for lib_path_unix in self.pending_libs { + let mut aapt = self.config.build_tool(bin!("aapt"))?; + aapt.arg("add"); + + if self.config.disable_aapt_compression { + aapt.arg("-0").arg(""); + } + + aapt.arg(self.config.unaligned_apk()).arg(lib_path_unix); + + if !aapt.status()?.success() { + return Err(NdkError::CmdFailed(aapt)); + } + } + + let mut zipalign = self.config.build_tool(bin!("zipalign"))?; zipalign .arg("-f") .arg("-v") .arg("4") - .arg(self.0.unaligned_apk()) - .arg(self.0.apk()); + .arg(self.config.unaligned_apk()) + .arg(self.config.apk()); if !zipalign.status()?.success() { return Err(NdkError::CmdFailed(zipalign)); } - Ok(UnsignedApk(self.0)) + Ok(UnsignedApk(self.config)) } } diff --git a/ndk-build/src/readelf.rs b/ndk-build/src/readelf.rs index 050fcc1c..1f8778f2 100644 --- a/ndk-build/src/readelf.rs +++ b/ndk-build/src/readelf.rs @@ -8,7 +8,7 @@ use std::process::Command; impl<'a> UnalignedApk<'a> { pub fn add_lib_recursively( - &self, + &mut self, lib: &Path, target: Target, search_paths: &[&Path], From 2c761acf66777e21e0040756003dc2f0edba0dcb Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Tue, 30 Aug 2022 16:03:56 +0200 Subject: [PATCH 2/4] fmt --- ndk-build/src/apk.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ndk-build/src/apk.rs b/ndk-build/src/apk.rs index 8de20ee4..7cd495e6 100644 --- a/ndk-build/src/apk.rs +++ b/ndk-build/src/apk.rs @@ -2,11 +2,11 @@ use crate::error::NdkError; use crate::manifest::AndroidManifest; use crate::ndk::{Key, Ndk}; use crate::target::Target; +use std::collections::HashSet; use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; -use std::collections::HashSet; pub struct ApkConfig { pub ndk: Ndk, @@ -71,7 +71,7 @@ impl ApkConfig { Ok(UnalignedApk { config: self, - pending_libs: HashSet::default() + pending_libs: HashSet::default(), }) } } @@ -138,7 +138,7 @@ impl<'a> UnalignedApk<'a> { return Err(NdkError::CmdFailed(aapt)); } } - + let mut zipalign = self.config.build_tool(bin!("zipalign"))?; zipalign .arg("-f") From b13965773b3a115d309c3f523b60abb9d4f672bb Mon Sep 17 00:00:00 2001 From: Jasper Bekkers Date: Wed, 31 Aug 2022 12:19:06 +0200 Subject: [PATCH 3/4] Only invoke aapt once to add all libraries --- ndk-build/src/apk.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ndk-build/src/apk.rs b/ndk-build/src/apk.rs index 7cd495e6..3d5fcca4 100644 --- a/ndk-build/src/apk.rs +++ b/ndk-build/src/apk.rs @@ -124,19 +124,21 @@ impl<'a> UnalignedApk<'a> { } pub fn add_pending_libs_and_align(self) -> Result, NdkError> { - for lib_path_unix in self.pending_libs { - let mut aapt = self.config.build_tool(bin!("aapt"))?; - aapt.arg("add"); + let mut aapt = self.config.build_tool(bin!("aapt"))?; + aapt.arg("add"); - if self.config.disable_aapt_compression { - aapt.arg("-0").arg(""); - } + if self.config.disable_aapt_compression { + aapt.arg("-0").arg(""); + } - aapt.arg(self.config.unaligned_apk()).arg(lib_path_unix); + aapt.arg(self.config.unaligned_apk()); - if !aapt.status()?.success() { - return Err(NdkError::CmdFailed(aapt)); - } + for lib_path_unix in self.pending_libs { + aapt.arg(lib_path_unix); + } + + if !aapt.status()?.success() { + return Err(NdkError::CmdFailed(aapt)); } let mut zipalign = self.config.build_tool(bin!("zipalign"))?; From 183716fc831756d5d4efad012b60935af950bc74 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 5 Sep 2022 12:24:36 +0200 Subject: [PATCH 4/4] Add changelog entries --- cargo-apk/CHANGELOG.md | 2 ++ ndk-build/CHANGELOG.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/cargo-apk/CHANGELOG.md b/cargo-apk/CHANGELOG.md index cbaa28a6..630060cc 100644 --- a/cargo-apk/CHANGELOG.md +++ b/cargo-apk/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- Upgrade to latest `ndk-build` to deduplicate libraries before packaging them into the APK. ([#333](https://github.com/rust-windowing/android-ndk-rs/pull/333)) + # 0.9.3 (2022-07-05) - Allow configuration of alternate debug keystore location; require keystore location for release builds. ([#299](https://github.com/rust-windowing/android-ndk-rs/pull/299)) diff --git a/ndk-build/CHANGELOG.md b/ndk-build/CHANGELOG.md index 8e697825..97d2af9f 100644 --- a/ndk-build/CHANGELOG.md +++ b/ndk-build/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- **Breaking:** Postpone APK library packaging until before zip alignment, to deduplicate possibly overlapping entries. ([#333](https://github.com/rust-windowing/android-ndk-rs/pull/333)) + # 0.7.0 (2022-07-05) - Fix NDK r23 `-lgcc` workaround for target directories containing spaces. ([#298](https://github.com/rust-windowing/android-ndk-rs/pull/298))