From 50f717d6f9c0b544ed711d908338f27b04e638ba Mon Sep 17 00:00:00 2001 From: Chethan <76928501+ch3thanhs@users.noreply.github.com> Date: Fri, 27 Nov 2026 15:15:42 +0530 Subject: [PATCH] chore: Generate patch metadata from the current bundle (#97) --- patches/build.gradle.kts | 6 +++- .../main/kotlin/util/PatchListGenerator.kt | 30 +++++-------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/patches/build.gradle.kts b/patches/build.gradle.kts index f1eba17ca..d5109925d 100644 --- a/patches/build.gradle.kts +++ b/patches/build.gradle.kts @@ -44,6 +44,8 @@ animalsniffer { } tasks { + val patchesFile = jar.flatMap { it.archiveFile } + register("generatePatchesList") { description = "Build patch with patch list" @@ -51,13 +53,15 @@ tasks { classpath = sourceSets["main"].runtimeClasspath + patchListGeneratorClasspath mainClass.set("util.PatchListGeneratorKt") + argumentProviders.add(CommandLineArgumentProvider { + listOf(patchesFile.get().asFile.absolutePath) + }) } // Runs D8 on every class in the patches file (the patches and all bundled dependencies) // but outputs class files instead of dex, so the check sees the code as it runs on Android, // after D8 has replaced methods it backports such as List.of() and Optional.isEmpty(). val desugarPatchesClasses = register("desugarPatchesClasses") { - val patchesFile = jar.flatMap { it.archiveFile } val classpath = sourceSets["main"].compileClasspath val output = layout.buildDirectory.file("animalsniffer/patches-desugared.jar") diff --git a/patches/src/main/kotlin/util/PatchListGenerator.kt b/patches/src/main/kotlin/util/PatchListGenerator.kt index 0b920ff48..4ef89a07f 100644 --- a/patches/src/main/kotlin/util/PatchListGenerator.kt +++ b/patches/src/main/kotlin/util/PatchListGenerator.kt @@ -10,30 +10,16 @@ import app.morphe.patcher.patch.loadPatchesFromJar import com.google.gson.GsonBuilder import com.google.gson.JsonObject import java.io.File -import java.net.URLClassLoader -import java.util.jar.Manifest +import java.util.jar.JarFile -fun main() { - val patchFiles = setOf( - File("build/libs/").listFiles { file -> - val fileName = file.name - !fileName.contains("javadoc") && - !fileName.contains("sources") && - fileName.endsWith(".mpp") - }!!.first() - ) - val loadedPatches = loadPatchesFromJar(patchFiles) - val patchClassLoader = URLClassLoader(patchFiles.map { it.toURI().toURL() }.toTypedArray()) - val manifest = patchClassLoader.getResources("META-INF/MANIFEST.MF") +fun main(args: Array) { + require(args.size == 1) { "Expected a patch bundle path." } + val patchBundle = File(args[0]) + val version = JarFile(patchBundle).use { + it.manifest?.mainAttributes?.getValue("Version") + } ?: error("Patch bundle has no Version in its manifest: $patchBundle") - while (manifest.hasMoreElements()) { - Manifest(manifest.nextElement().openStream()) - .mainAttributes - .getValue("Version") - ?.let { - generatePatchList(it, loadedPatches) - } - } + generatePatchList(version, loadPatchesFromJar(setOf(patchBundle))) } @Suppress("DEPRECATION")