From 2a47685e366741f6f737a98fd2e6032b7af8608d Mon Sep 17 00:00:00 2001 From: Armand <4831c0@proton.me> Date: Sun, 17 Aug 2025 16:17:58 +0200 Subject: [PATCH] android: parallelize optipng --- firka/android/app/build.gradle.kts | 41 +++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/firka/android/app/build.gradle.kts b/firka/android/app/build.gradle.kts index eae246c..6d0164c 100644 --- a/firka/android/app/build.gradle.kts +++ b/firka/android/app/build.gradle.kts @@ -2,6 +2,8 @@ import org.apache.commons.io.FileUtils import java.io.FileInputStream import java.security.MessageDigest import java.util.Properties +import java.util.concurrent.Executors +import java.util.concurrent.Future import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream.DEFLATED @@ -257,24 +259,39 @@ fun transformApk(input: File, output: File, compressionLevel: String = "Z") { val topDirL = tempDir.absolutePath.length + 1 val zos = ZipOutputStream(output.outputStream()) + + val coreCount = Runtime.getRuntime().availableProcessors() + val pngFiles = tempDir.walkTopDown().filter{f -> f.name.endsWith(".png")} + + if (compressionLevel == "Z" && optipng != null) { + val executor = Executors.newFixedThreadPool(coreCount) + val futures = mutableListOf>() + + pngFiles.forEach { pngFile -> + val future = executor.submit { + exec { + commandLine( + optipng, + "-zm", "9", + "-zw", "32k", + "-o9", + pngFile.absolutePath + ) + } + } + futures.add(future) + } + + futures.forEach { it.get() } + executor.shutdown() + } + tempDir.walkTopDown().forEach { f -> if (f.absolutePath == tempDir.absolutePath) return@forEach var relName = f.absolutePath.substring(topDirL).replace("\\", "/") if (f.isDirectory && !relName.endsWith("/")) relName += "/" - if (compressionLevel == "Z" && optipng != null && f.extension == "png") { - exec { - commandLine( - optipng, - "-zm", "9", - "-zw", "32k", - "-o9", - f.absolutePath - ) - } - } - val compress = !relName.endsWith(".so") && !relName.endsWith(".arsc") zos.setMethod(if (compress) { DEFLATED } else { STORED }) val entry = ZipEntry(relName)