From 8c0fe5646b704aeb09a7f666ace187c83ce1b872 Mon Sep 17 00:00:00 2001 From: Nullptr Date: Tue, 24 Aug 2021 18:48:49 +0800 Subject: [PATCH] Manually generate cd and eocd before sign --- .../main/java/org/lsposed/patch/LSPatch.java | 35 ++++++++++--------- .../org/lsposed/patch/util/NestedZipLink.java | 6 ++++ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/patch/src/main/java/org/lsposed/patch/LSPatch.java b/patch/src/main/java/org/lsposed/patch/LSPatch.java index 1bde466..a685a19 100644 --- a/patch/src/main/java/org/lsposed/patch/LSPatch.java +++ b/patch/src/main/java/org/lsposed/patch/LSPatch.java @@ -258,6 +258,25 @@ public class LSPatch { dstZFile.realign(); + // create zip link + if (verbose) + System.out.println("Creating nested apk link..."); + + NestedZipLink nestedZipLink = new NestedZipLink(dstZFile); + StoredEntry originalZipEntry = dstZFile.get(ORIGINAL_APK_ASSET_PATH); + NestedZip nestedZip = new NestedZip(srcZFile, originalZipEntry); + for (StoredEntry entry : srcZFile.entries()) { + String name = entry.getCentralDirectoryHeader().getName(); + if (name.startsWith("classes") && name.endsWith(".dex")) continue; + if (dstZFile.get(name) != null) continue; + if (name.equals("AndroidManifest.xml")) continue; + if (name.startsWith("META-INF/CERT")) continue; + if (name.equals("META-INF/MANIFEST.MF")) continue; + nestedZip.addFileLink(name); + } + nestedZipLink.nestedZips.add(nestedZip); + dstZFile.addZFileExtension(nestedZipLink); + // sign apk System.out.println("Signing apk..."); try { @@ -278,22 +297,6 @@ public class LSPatch { throw new PatchError("Failed to sign apk: " + e.getMessage()); } - // create zip link - if (verbose) - System.out.println("Creating nested apk link..."); - - NestedZipLink nestedZipLink = new NestedZipLink(dstZFile); - StoredEntry originalZipEntry = dstZFile.get(ORIGINAL_APK_ASSET_PATH); - NestedZip nestedZip = new NestedZip(srcZFile, originalZipEntry); - for (StoredEntry entry : srcZFile.entries()) { - String name = entry.getCentralDirectoryHeader().getName(); - if (name.startsWith("classes") && name.endsWith(".dex")) continue; - if (name.equals("AndroidManifest.xml")) continue; - nestedZip.addFileLink(name); - } - nestedZipLink.nestedZips.add(nestedZip); - dstZFile.addZFileExtension(nestedZipLink); - dstZFile.update(); FileUtils.copyFile(tmpApk, outputFile); diff --git a/patch/src/main/java/org/lsposed/patch/util/NestedZipLink.java b/patch/src/main/java/org/lsposed/patch/util/NestedZipLink.java index 7192ee9..4f99ba5 100644 --- a/patch/src/main/java/org/lsposed/patch/util/NestedZipLink.java +++ b/patch/src/main/java/org/lsposed/patch/util/NestedZipLink.java @@ -54,6 +54,12 @@ public class NestedZipLink extends ZFileExtension { deleteDirectoryAndEocd.setAccessible(true); deleteDirectoryAndEocd.invoke(zFile); appendEntries(); + Method computeCentralDirectory = ZFile.class.getDeclaredMethod("computeCentralDirectory"); + computeCentralDirectory.setAccessible(true); + computeCentralDirectory.invoke(zFile); + Method computeEocd = ZFile.class.getDeclaredMethod("computeEocd"); + computeEocd.setAccessible(true); + computeEocd.invoke(zFile); } catch (Exception e) { e.printStackTrace(); var ex = new IOException("Error when writing link entries");