From 518c0370c807059b91e4a5725b9d5dc0f5a06f21 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 17 Sep 2020 16:09:13 +0100 Subject: [PATCH] Make briar-headless.jar deterministic. --- briar-headless/build.gradle | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/briar-headless/build.gradle b/briar-headless/build.gradle index 32dcec66f..581e69e95 100644 --- a/briar-headless/build.gradle +++ b/briar-headless/build.gradle @@ -1,3 +1,9 @@ +import java.util.jar.JarEntry +import java.util.jar.JarFile +import java.util.jar.JarOutputStream + +import static java.util.Collections.list + plugins { id 'java' id 'idea' @@ -47,6 +53,34 @@ jar { from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } + doLast() { + // Rename the original jar + File jar = project.jar.archivePath + String srcPath = jar.toString().replaceFirst('\\.jar$', '.unsorted.jar') + File srcFile = new File(srcPath) + jar.renameTo(srcFile) + JarFile srcJarFile = new JarFile(srcFile) + OutputStream destStream = new JarOutputStream(new FileOutputStream(jar)) + // Read and sort the entries + Map entries = new TreeMap() + for (JarEntry e : list(srcJarFile.entries())) entries.put(e.getName(), e) + // Write the sorted entries + for (JarEntry srcEntry : entries.values()) { + JarEntry destEntry = new JarEntry(srcEntry.getName()) + destEntry.setTime(0) + destStream.putNextEntry(destEntry) + InputStream srcStream = srcJarFile.getInputStream(srcEntry) + int read + byte[] buf = new byte[4096] + while ((read = srcStream.read(buf, 0, buf.length)) != -1) { + destStream.write(buf, 0, read) + } + destStream.closeEntry() + srcStream.close() + } + destStream.close() + srcJarFile.close() + } } // At the moment for non-Android projects we need to explicitly mark the code generated by kapt