From a8c542b7d3400d259b29fbc741b4487012ecac1f Mon Sep 17 00:00:00 2001 From: rushiiMachine <33725716+rushiiMachine@users.noreply.github.com> Date: Mon, 3 Mar 2025 19:39:41 -0800 Subject: [PATCH] feat: strip native libs of other archs to shrink APK size Fixes #76 --- .../manager/patcher/steps/install/AlignmentStep.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/kotlin/com/aliucord/manager/patcher/steps/install/AlignmentStep.kt b/app/src/main/kotlin/com/aliucord/manager/patcher/steps/install/AlignmentStep.kt index a2e4395..ebdaaa8 100644 --- a/app/src/main/kotlin/com/aliucord/manager/patcher/steps/install/AlignmentStep.kt +++ b/app/src/main/kotlin/com/aliucord/manager/patcher/steps/install/AlignmentStep.kt @@ -21,6 +21,7 @@ class AlignmentStep : Step(), KoinComponent { override val localizedName = R.string.patch_step_alignment override suspend fun execute(container: StepRunner) { + val currentDeviceArch = Build.SUPPORTED_ABIS.first() val apk = container.getStep().patchedApk var resourcesArscBytes: ByteArray? = null @@ -52,12 +53,19 @@ class AlignmentStep : Step(), KoinComponent { // Align native libs due to using extractNativeLibs nativeLibPaths = ZipReader(apk).use { zip -> val libPaths = zip.entryNames.filter { it.endsWith(".so") } + + // Extract to disk temporarily for ((idx, path) in libPaths.withIndex()) { + // Ignore lib architectures that don't match this device + if (!path.startsWith("lib/$currentDeviceArch")) + continue + // Index is just used as a placeholder id to cache on disk val bytes = zip.openEntry(path)!!.read() val file = paths.patchingWorkingDir().resolve("$idx.so") file.writeBytes(bytes) } + libPaths } @@ -84,6 +92,10 @@ class AlignmentStep : Step(), KoinComponent { // Write back native libraries aligned to 16KiB page boundary for ((idx, path) in nativeLibPaths.withIndex()) { + // Ignore lib architectures that don't match this device + if (!path.startsWith("lib/$currentDeviceArch")) + continue + val file = paths.patchingWorkingDir().resolve("$idx.so") val bytes = file.readBytes() zip.writeEntry(path, bytes, ZipCompression.NONE, 16384)