import java.io.*; import java.nio.file.*; import java.util.zip.*; /** * Repack an APK so that shared libraries are STORED (uncompressed). * * Why: System.loadLibrary resolves lib//x.so directly inside the APK * (base.apk!/lib//...). That requires the entry to be STORED and page * aligned, otherwise the loader reports * UnsatisfiedLinkError: couldn't find "libx.so" * * .NET's ZipArchive CompressionLevel.NoCompression does not reliably produce * STORED entries from PowerShell, so this tiny tool does it explicitly. * * Usage: java ZipStore in.apk out.apk [prefix ...] * default prefixes: lib/ */ public final class ZipStore { public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("usage: ZipStore [storedPrefix...]"); System.exit(2); } Path in = Path.of(args[0]); Path out = Path.of(args[1]); String[] prefixes = args.length > 2 ? java.util.Arrays.copyOfRange(args, 2, args.length) : new String[]{"lib/"}; try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(Files.newInputStream(in))); ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(out)))) { ZipEntry e; byte[] buf = new byte[1 << 16]; while ((e = zis.getNextEntry()) != null) { if (e.isDirectory()) { continue; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); int n; while ((n = zis.read(buf)) > 0) { bos.write(buf, 0, n); } byte[] data = bos.toByteArray(); ZipEntry ne = new ZipEntry(e.getName()); ne.setTime(0L); // reproducible boolean store = false; for (String p : prefixes) { if (e.getName().startsWith(p)) { store = true; break; } } if (store) { CRC32 crc = new CRC32(); crc.update(data); ne.setMethod(ZipEntry.STORED); ne.setSize(data.length); ne.setCompressedSize(data.length); ne.setCrc(crc.getValue()); } else { ne.setMethod(ZipEntry.DEFLATED); } zos.putNextEntry(ne); zos.write(data); zos.closeEntry(); System.out.println((store ? "STORED " : "deflated ") + e.getName() + " " + data.length); } } } }