From 1f9e16f35b550660f922f14789cd7360d6b0d667 Mon Sep 17 00:00:00 2001 From: AhmadAddas <139542546+AhmadAddas@users.noreply.github.com> Date: Mon, 9 Nov 2026 21:27:59 +0400 Subject: [PATCH 1/2] Fix RecordTag NoClassDefFoundError in framework DEX Package AGP's generated record support with the framework so constructing an invoker no longer fails on the missing RecordTag superclass. Merge the finalized APK DEX set into one classes.dex, retain separate debug/release intermediate paths, and package it once as framework/vector.dex. Reject missing record support or output exceeding the single-DEX loader. Add direct debug/release packaging and missing-support regression tests. The regression suite passed in this fresh checkout. --- tests/recordtag/fixture/build.gradle.kts.in | 41 ++++++++ tests/recordtag/fixture/fixture-rules.pro | 1 + tests/recordtag/fixture/settings.gradle.kts | 5 + .../fixture/src/main/AndroidManifest.xml | 1 + .../matrix/vector/recordtest/RecordProbe.java | 4 + tests/recordtag/run.py | 77 +++++++++++++++ .../runtime/RecordTagRuntimeProbe.java | 51 ++++++++++ zygisk/build.gradle.kts | 99 ++++++++++++++++--- 8 files changed, 267 insertions(+), 12 deletions(-) create mode 100644 tests/recordtag/fixture/build.gradle.kts.in create mode 100644 tests/recordtag/fixture/fixture-rules.pro create mode 100644 tests/recordtag/fixture/settings.gradle.kts create mode 100644 tests/recordtag/fixture/src/main/AndroidManifest.xml create mode 100644 tests/recordtag/fixture/src/main/java/org/matrix/vector/recordtest/RecordProbe.java create mode 100644 tests/recordtag/run.py create mode 100644 tests/recordtag/runtime/RecordTagRuntimeProbe.java diff --git a/tests/recordtag/fixture/build.gradle.kts.in b/tests/recordtag/fixture/build.gradle.kts.in new file mode 100644 index 000000000..dce2fbc9f --- /dev/null +++ b/tests/recordtag/fixture/build.gradle.kts.in @@ -0,0 +1,41 @@ +import com.android.build.api.artifact.SingleArtifact +import com.android.tools.smali.dexlib2.DexFileFactory +import com.android.tools.smali.dexlib2.Opcodes +import java.util.zip.ZipFile +import org.gradle.process.ExecOperations +buildscript { + repositories { mavenCentral() } + dependencies { classpath("com.android.tools.smali:smali-dexlib2:3.0.10") } +} +plugins { id("com.android.application") version "@AGP@" } +android { + namespace = "org.matrix.vector.recordtest" + lint { checkReleaseBuilds = false } + compileSdk = @COMPILE_SDK@ + buildToolsVersion = "@BUILD_TOOLS@" + defaultConfig { minSdk = @MIN_SDK@; multiDexEnabled = !providers.gradleProperty("monoDex").isPresent && @MULTIDEX@ } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 + } + buildTypes { release { isMinifyEnabled = true; proguardFiles("fixture-rules.pro") } } +} +@PREPARE_FRAMEWORK_DEX@ + +androidComponents { onVariants(selector().all()) { variant -> + val variantCapped = variant.name.replaceFirstChar { it.uppercase() } + val variantLowered = variant.name.lowercase() +@REGISTER_FRAMEWORK_DEX@ + tasks.register("testFrameworkZip$variantCapped") { + dependsOn(prepareFrameworkDex) + destinationDirectory.set(layout.buildDirectory.dir("test-zips")) + archiveFileName.set("framework-${variant.name}.zip") + into("framework") { + from(dexOutPath) { + include("classes.dex") + rename("classes.dex", "vector.dex") + } + } + } + +} } diff --git a/tests/recordtag/fixture/fixture-rules.pro b/tests/recordtag/fixture/fixture-rules.pro new file mode 100644 index 000000000..4094eb451 --- /dev/null +++ b/tests/recordtag/fixture/fixture-rules.pro @@ -0,0 +1 @@ +-keep class org.matrix.vector.recordtest.** { *; } diff --git a/tests/recordtag/fixture/settings.gradle.kts b/tests/recordtag/fixture/settings.gradle.kts new file mode 100644 index 000000000..67f3a321c --- /dev/null +++ b/tests/recordtag/fixture/settings.gradle.kts @@ -0,0 +1,5 @@ +pluginManagement { + repositories { google(); mavenCentral(); gradlePluginPortal() } +} +dependencyResolutionManagement { repositories { google(); mavenCentral() } } +rootProject.name = "RecordTagFixture" diff --git a/tests/recordtag/fixture/src/main/AndroidManifest.xml b/tests/recordtag/fixture/src/main/AndroidManifest.xml new file mode 100644 index 000000000..f36f852bf --- /dev/null +++ b/tests/recordtag/fixture/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/tests/recordtag/fixture/src/main/java/org/matrix/vector/recordtest/RecordProbe.java b/tests/recordtag/fixture/src/main/java/org/matrix/vector/recordtest/RecordProbe.java new file mode 100644 index 000000000..83c214f22 --- /dev/null +++ b/tests/recordtag/fixture/src/main/java/org/matrix/vector/recordtest/RecordProbe.java @@ -0,0 +1,4 @@ +package org.matrix.vector.recordtest; +public final class RecordProbe { + public record Value(int value) {} +} diff --git a/tests/recordtag/run.py b/tests/recordtag/run.py new file mode 100644 index 000000000..2dfa41d6e --- /dev/null +++ b/tests/recordtag/run.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +"""Exercise the production packaging task with a small Java-record APK, no NDK build.""" +import argparse +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import zipfile + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument("--output", type=Path, required=True, help="New directory for logs and resulting DEX files") +parser.add_argument("--offline", action="store_true", help="Use cached Gradle dependencies only") +args = parser.parse_args() +output = args.output.resolve() +output.mkdir(parents=True, exist_ok=False) +root = Path(__file__).resolve().parents[2] +script = (root / "zygisk/build.gradle.kts").read_text() +root_build = (root / "build.gradle.kts").read_text() +versions = (root / "gradle/libs.versions.toml").read_text() +# Exercise the exact production task and registration, rather than a test reimplementation. +start = script.index("/** Package AGP") +implementation = script[start:script.index("\ndependencies {", start)] +start = script.index(" // Keep prepared output separate") +registration = script[start:script.index(" // Using Sync", start)] + +def setting(name): + return re.search(r"val " + name + r' = ("[^"\n]+"|[0-9]+)', root_build).group(1).strip('"') + +text = (root / "tests/recordtag/fixture/build.gradle.kts.in").read_text() +for key, value in { + "PREPARE_FRAMEWORK_DEX": implementation, + "REGISTER_FRAMEWORK_DEX": registration, + "AGP": re.search(r'agp = "([^"]+)"', versions).group(1), + "COMPILE_SDK": setting("androidCompileSdkVersion"), + "BUILD_TOOLS": setting("androidBuildToolsVersion"), + "MIN_SDK": setting("androidMinSdkVersion"), + "MULTIDEX": re.search(r"multiDexEnabled = (true|false)", script).group(1), +}.items(): + text = text.replace("@" + key + "@", value) + +with tempfile.TemporaryDirectory(prefix="vector-recordtag-") as tmp: + fixture = Path(tmp) + for name in ("settings.gradle.kts", "fixture-rules.pro"): + shutil.copyfile(root / "tests/recordtag/fixture" / name, fixture / name) + shutil.copytree(root / "tests/recordtag/fixture/src", fixture / "src") + (fixture / "build.gradle.kts").write_text(text) + + def run(name, tasks, negative=False): + with (output / (name + ".log")).open("w") as log: + result = subprocess.run(["bash", str(root / "gradlew"), "-p", str(fixture), + *(["--offline"] if args.offline else []), + "--console=plain", *tasks], stdout=log, stderr=subprocess.STDOUT) + if negative: + diagnostic = (output / (name + ".log")).read_text() + if result.returncode == 0 or "global synthetics are missing" not in diagnostic: + raise SystemExit("Negative regression failed; see " + str(output)) + if (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").exists(): + raise SystemExit("Failed packaging left a usable-looking output") + elif result.returncode: + raise SystemExit("Build failed; see " + str(output / (name + ".log"))) + + run("missing-support-rejected", ["prepareFrameworkDexDebug", "-PmonoDex"], negative=True) + run("debug", ["testFrameworkZipDebug"]) + shutil.copyfile(fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex", output / "debug-vector.dex") + run("release", ["testFrameworkZipRelease"]) + shutil.copyfile(fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex", output / "release-vector.dex") + for variant in ("debug", "release"): + with zipfile.ZipFile(fixture / f"build/test-zips/framework-{variant}.zip") as archive: + dex_entries = [name for name in archive.namelist() if name.endswith(".dex")] + if dex_entries != ["framework/vector.dex"]: + raise SystemExit(f"Unexpected or duplicate framework DEX entries: {dex_entries}") + if archive.read(dex_entries[0]) != (output / f"{variant}-vector.dex").read_bytes(): + raise SystemExit("Packaged DEX differs from prepared single-DEX output") + +print("PASS: missing record support rejected; debug/release each package exactly one framework/vector.dex") +print("Artifacts:", output) diff --git a/tests/recordtag/runtime/RecordTagRuntimeProbe.java b/tests/recordtag/runtime/RecordTagRuntimeProbe.java new file mode 100644 index 000000000..929789c52 --- /dev/null +++ b/tests/recordtag/runtime/RecordTagRuntimeProbe.java @@ -0,0 +1,51 @@ +import dalvik.system.InMemoryDexClassLoader; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** Disposable process: load API record types without initializing Vector's hooks. */ +public final class RecordTagRuntimeProbe { + public static void main(String[] args) throws Exception { + if (args.length < 1 || args.length > 2) throw new IllegalArgumentException("Expected DEX path and optional --fixture"); + ClassLoader loader = new InMemoryDexClassLoader( + ByteBuffer.wrap(Files.readAllBytes(Paths.get(args[0]))), + ClassLoader.getSystemClassLoader()); + if (args.length == 2) { + if (!"--fixture".equals(args[1])) throw new IllegalArgumentException("Unknown mode"); + Class record = Class.forName("org.matrix.vector.recordtest.RecordProbe$Value", true, loader); + Object first = record.getConstructor(int.class).newInstance(42); + Object equal = record.getConstructor(int.class).newInstance(42); + Object different = record.getConstructor(int.class).newInstance(43); + if (!first.equals(equal) || first.equals(different) + || first.hashCode() != equal.hashCode() + || !Integer.valueOf(42).equals(record.getMethod("value").invoke(first)) + || !first.toString().contains("42")) + throw new AssertionError("Fixture record contract failed"); + System.out.println("PASS: packaged fixture record loads and preserves value semantics"); + return; + } + String prefix = "io.github.libxposed.api.XposedInterface$Invoker$Type"; + Class chain = Class.forName(prefix + "$Chain", true, loader); + Object first = chain.getConstructor(int.class).newInstance(42); + Object equal = chain.getConstructor(int.class).newInstance(42); + Object different = chain.getConstructor(int.class).newInstance(43); + if (!first.equals(equal) || first.equals(different) + || first.hashCode() != equal.hashCode() + || !Integer.valueOf(42).equals(chain.getMethod("maxPriority").invoke(first)) + || !first.toString().contains("42") || chain.getField("FULL").get(null) == null) + throw new AssertionError("Chain record contract failed"); + Class origin = Class.forName(prefix + "$Origin", true, loader); + Object original = origin.getConstructor().newInstance(); + if (!original.equals(origin.getConstructor().newInstance())) + throw new AssertionError("Origin record contract failed"); + Class invoker = Class.forName("org.matrix.vector.impl.hooks.VectorMethodInvoker", true, loader); + java.lang.reflect.Constructor constructor = invoker.getDeclaredConstructor(java.lang.reflect.Method.class); + constructor.setAccessible(true); + Object instance = constructor.newInstance(String.class.getMethod("length")); + java.lang.reflect.Method setType = invoker.getMethod("setType", Class.forName(prefix, true, loader)); + setType.setAccessible(true); + if (setType.invoke(instance, original) != instance) + throw new AssertionError("Invoker type selection failed"); + System.out.println("PASS: Chain/Origin semantics, BaseInvoker construction and original-type selection"); + } +} diff --git a/zygisk/build.gradle.kts b/zygisk/build.gradle.kts index c03d2fa91..7b274a498 100644 --- a/zygisk/build.gradle.kts +++ b/zygisk/build.gradle.kts @@ -1,3 +1,4 @@ +import com.android.build.api.artifact.SingleArtifact import com.android.tools.smali.dexlib2.DexFileFactory import com.android.tools.smali.dexlib2.Opcodes import com.android.tools.smali.dexlib2.iface.ClassDef @@ -6,8 +7,10 @@ import com.android.tools.smali.dexlib2.iface.reference.FieldReference import com.android.tools.smali.dexlib2.iface.reference.MethodReference import com.android.tools.smali.dexlib2.iface.reference.TypeReference import java.security.MessageDigest +import java.util.zip.ZipFile import org.apache.commons.codec.binary.Hex import org.apache.tools.ant.filters.ReplaceTokens +import org.gradle.process.ExecOperations // Reading the optimised DEX needs a DEX reader; see checkXResourcesIsolation below. buildscript { @@ -45,7 +48,9 @@ android { namespace = "org.matrix.vector" defaultConfig { - multiDexEnabled = false + // AGP emits global synthetics (including the record superclass) separately. + // PrepareFrameworkDex merges the final APK's DEX files for our single-DEX loader. + multiDexEnabled = true buildConfigField("int", "HostPackageUid", "${injectedPackageUid}") buildConfigField("String", "InjectedPackageName", """"${injectedPackageName}"""") buildConfigField("String", "ManagerPackageName", """"${defaultManagerPackageName}"""") @@ -79,6 +84,63 @@ abstract class Injected @Inject constructor(val moduleDir: String) { @get:Inject abstract val factory: ObjectFactory } +/** Package AGP's final DEX set, including global synthetics, for the in-memory loader. */ +abstract class PrepareFrameworkDex : DefaultTask() { + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val apkDirectory: DirectoryProperty + + @get:InputFile + @get:PathSensitive(PathSensitivity.NONE) + abstract val d8Jar: RegularFileProperty + + @get:Input abstract val minSdk: Property + @get:OutputFile abstract val outputFile: RegularFileProperty + @get:Inject abstract val execOperations: ExecOperations + + @TaskAction + fun prepare() { + val output = outputFile.get().asFile + output.delete() + val apks = apkDirectory.get().asFile.listFiles { file -> file.extension == "apk" }.orEmpty() + check(apks.size == 1) { "Expected one framework APK, found ${apks.size}" } + val work = temporaryDir.resolve("dex") + work.deleteRecursively() + work.mkdirs() + val inputs = ZipFile(apks.single()).use { apk -> + apk.entries().asSequence() + .filter { Regex("classes(?:[2-9]|[1-9][0-9]+)?\\.dex").matches(it.name) } + .map { entry -> + work.resolve(entry.name).also { file -> + apk.getInputStream(entry).use { source -> + file.outputStream().use { target -> source.copyTo(target) } + } + } + }.toList() + } + check(inputs.isNotEmpty()) { "Framework APK has no DEX files" } + val candidate = if (inputs.size == 1) inputs.single() else { + val merged = work.resolve("merged").apply { mkdirs() } + execOperations.javaexec { + classpath(d8Jar) + mainClass.set("com.android.tools.r8.D8") + args("--no-desugaring", "--min-api", minSdk.get().toString(), "--output", merged.path) + args(inputs.map { it.path }) + }.assertNormalExitValue() + val dexFiles = merged.listFiles { file -> file.extension == "dex" }.orEmpty() + check(dexFiles.size == 1) { "Framework exceeds the single-DEX loader's capacity" } + dexFiles.single() + } + val classes = DexFileFactory.loadDexFile(candidate, Opcodes.forApi(minSdk.get())).classes + val recordTag = "Lcom/android/tools/r8/RecordTag;" + check(classes.none { it.superclass == recordTag } || classes.any { it.type == recordTag }) { + "Framework DEX references RecordTag without defining it; global synthetics are missing" + } + output.parentFile.mkdirs() + candidate.copyTo(output, overwrite = true) + } +} + dependencies { implementation(projects.hiddenapi.bridge) implementation(projects.legacy) @@ -101,12 +163,32 @@ androidComponents { val zipFileName = "Vector-v${versionNameProvider.get()}-${versionCodeProvider.get()}-$variantCapped.zip" + // Keep prepared output separate from AGP-owned DEX intermediates. + val dexOutPath = + if (variantLowered == "release") + layout.buildDirectory.dir( + "intermediates/framework_dex/$variantLowered/minify${variantCapped}WithR8" + ) + else + layout.buildDirectory.dir( + "intermediates/framework_dex/$variantLowered/mergeDex$variantCapped" + ) + val prepareFrameworkDex = tasks.register("prepareFrameworkDex$variantCapped") { + apkDirectory.set(variant.artifacts.get(SingleArtifact.APK)) + d8Jar.set(androidComponents.sdkComponents.sdkDirectory.map { + it.file("build-tools/${android.buildToolsVersion}/lib/d8.jar") + }) + minSdk.set(variant.minSdk.apiLevel) + outputFile.set(dexOutPath.map { it.file("classes.dex") }) + } + // Using Sync ensures that stale files from previous runs are removed. val prepareModuleFilesTask = tasks.register("prepareModuleFiles$variantCapped") { group = "Vector Module Packaging" dependsOn( "assemble$variantCapped", + prepareFrameworkDex, ":manager:package$variantCapped", ":daemon:package$variantCapped", ":dex2oat:externalNativeBuild$variantCapped", @@ -166,18 +248,11 @@ androidComponents { include("**/liboat_hook.so") } } - val dexOutPath = - if (variantLowered == "release") - layout.buildDirectory.dir( - "intermediates/dex/$variantLowered/minify${variantCapped}WithR8" - ) - else - layout.buildDirectory.dir( - "intermediates/dex/$variantLowered/mergeDex$variantCapped" - ) into("framework") { - from(dexOutPath) - rename("classes.dex", "vector.dex") + from(dexOutPath) { + include("classes.dex") + rename("classes.dex", "vector.dex") + } } val injected = objects.newInstance(tempModuleDir.get().asFile.path) doLast { From c4cad1993b82758e4afd38d4187134d95de1487b Mon Sep 17 00:00:00 2001 From: AhmadAddas <139542546+AhmadAddas@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:05:10 +0400 Subject: [PATCH 2/2] Harden RecordTag DEX packaging and expand regression coverage --- tests/recordtag/fixture/build.gradle.kts.in | 60 ++++++ tests/recordtag/run.py | 175 +++++++++++++----- tests/recordtag/runtime/NativeHookProbe.java | 97 ++++++++++ tests/recordtag/runtime/native_hook_probe.cpp | 39 ++++ tests/recordtag/runtime/run-native.py | 105 +++++++++++ zygisk/build.gradle.kts | 28 ++- 6 files changed, 457 insertions(+), 47 deletions(-) create mode 100644 tests/recordtag/runtime/NativeHookProbe.java create mode 100644 tests/recordtag/runtime/native_hook_probe.cpp create mode 100644 tests/recordtag/runtime/run-native.py diff --git a/tests/recordtag/fixture/build.gradle.kts.in b/tests/recordtag/fixture/build.gradle.kts.in index dce2fbc9f..8d199673f 100644 --- a/tests/recordtag/fixture/build.gradle.kts.in +++ b/tests/recordtag/fixture/build.gradle.kts.in @@ -1,7 +1,12 @@ import com.android.build.api.artifact.SingleArtifact import com.android.tools.smali.dexlib2.DexFileFactory import com.android.tools.smali.dexlib2.Opcodes +import java.nio.file.AtomicMoveNotSupportedException +import java.nio.file.Files +import java.nio.file.StandardCopyOption import java.util.zip.ZipFile +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream import org.gradle.process.ExecOperations buildscript { repositories { mavenCentral() } @@ -26,6 +31,61 @@ androidComponents { onVariants(selector().all()) { variant -> val variantCapped = variant.name.replaceFirstChar { it.uppercase() } val variantLowered = variant.name.lowercase() @REGISTER_FRAMEWORK_DEX@ + val failure = providers.gradleProperty("failureApk") + if (failure.isPresent) { + val sourceApk = variant.artifacts.get(SingleArtifact.APK) + val failureDirectory = layout.buildDirectory.dir("failure-apk/${variant.name}") + val damage = tasks.register("makeFailureApk$variantCapped") { + inputs.dir(sourceApk) + inputs.property("failure", failure) + outputs.dir(failureDirectory) + doLast { + val original = sourceApk.get().asFile.listFiles { f -> f.extension == "apk" }!!.single() + val destination = failureDirectory.get().asFile.apply { mkdirs() }.resolve("test.apk") + ZipFile(original).use { archive -> + val dex = archive.getInputStream(archive.getEntry("classes.dex")).use { it.readBytes() } + ZipOutputStream(destination.outputStream()).use { zip -> + zip.putNextEntry(ZipEntry("classes.dex")) + zip.write(dex) + zip.closeEntry() + zip.putNextEntry(ZipEntry("classes2.dex")) + zip.write(if (failure.get() == "duplicate") dex else byteArrayOf(0, 1, 2)) + zip.closeEntry() + } + } + } + } + prepareFrameworkDex.configure { + dependsOn(damage) + apkDirectory.set(failureDirectory) + } + } + + tasks.register("verifyFrameworkClasses$variantCapped") { + dependsOn(prepareFrameworkDex) + doLast { + val apk = variant.artifacts.get(SingleArtifact.APK).get().asFile + .listFiles { f -> f.extension == "apk" }!!.single() + val expected = mutableSetOf() + ZipFile(apk).use { archive -> + archive.entries().asSequence() + .filter { Regex("classes(?:[2-9]|[1-9][0-9]+)?\\.dex").matches(it.name) } + .forEach { entry -> + val dex = temporaryDir.resolve(entry.name) + archive.getInputStream(entry).use { input -> dex.outputStream().use { input.copyTo(it) } } + DexFileFactory.loadDexFile(dex, Opcodes.getDefault()).classes.forEach { + check(expected.add(it.type)) { "Duplicate input class: ${it.type}" } + } + } + } + val actual = DexFileFactory.loadDexFile( + prepareFrameworkDex.get().outputFile.get().asFile, Opcodes.getDefault() + ).classes.map { it.type }.toSet() + check(expected == actual) { + "Merged class mismatch: missing=${expected - actual}, extra=${actual - expected}" + } + } + } tasks.register("testFrameworkZip$variantCapped") { dependsOn(prepareFrameworkDex) destinationDirectory.set(layout.buildDirectory.dir("test-zips")) diff --git a/tests/recordtag/run.py b/tests/recordtag/run.py index 2dfa41d6e..f7ed59521 100644 --- a/tests/recordtag/run.py +++ b/tests/recordtag/run.py @@ -5,7 +5,6 @@ import re import shutil import subprocess -import tempfile import zipfile parser = argparse.ArgumentParser(description=__doc__) @@ -19,59 +18,145 @@ root_build = (root / "build.gradle.kts").read_text() versions = (root / "gradle/libs.versions.toml").read_text() # Exercise the exact production task and registration, rather than a test reimplementation. -start = script.index("/** Package AGP") -implementation = script[start:script.index("\ndependencies {", start)] -start = script.index(" // Keep prepared output separate") -registration = script[start:script.index(" // Using Sync", start)] +def section(source, beginning, end): + try: + start = source.index(beginning) + return source[start:source.index(end, start)] + except ValueError as error: + raise SystemExit(f"Production Gradle extraction failed: expected {beginning!r} through {end!r}") from error + + +def match(pattern, source, label): + result = re.search(pattern, source) + if result is None: + raise SystemExit(f"Cannot read {label} from production build configuration") + return result.group(1) + + +implementation = section(script, "/** Package AGP", "\ndependencies {") +registration = section(script, " // Keep prepared output separate", " // Using Sync") + def setting(name): - return re.search(r"val " + name + r' = ("[^"\n]+"|[0-9]+)', root_build).group(1).strip('"') + return match(r"val " + name + r' = ("[^"\n]+"|[0-9]+)', root_build, name).strip('"') text = (root / "tests/recordtag/fixture/build.gradle.kts.in").read_text() for key, value in { "PREPARE_FRAMEWORK_DEX": implementation, "REGISTER_FRAMEWORK_DEX": registration, - "AGP": re.search(r'agp = "([^"]+)"', versions).group(1), + "AGP": match(r'agp = "([^"]+)"', versions, "AGP version"), "COMPILE_SDK": setting("androidCompileSdkVersion"), "BUILD_TOOLS": setting("androidBuildToolsVersion"), "MIN_SDK": setting("androidMinSdkVersion"), - "MULTIDEX": re.search(r"multiDexEnabled = (true|false)", script).group(1), + "MULTIDEX": match(r"multiDexEnabled = (true|false)", script, "multidex setting"), }.items(): - text = text.replace("@" + key + "@", value) - -with tempfile.TemporaryDirectory(prefix="vector-recordtag-") as tmp: - fixture = Path(tmp) - for name in ("settings.gradle.kts", "fixture-rules.pro"): - shutil.copyfile(root / "tests/recordtag/fixture" / name, fixture / name) - shutil.copytree(root / "tests/recordtag/fixture/src", fixture / "src") - (fixture / "build.gradle.kts").write_text(text) - - def run(name, tasks, negative=False): - with (output / (name + ".log")).open("w") as log: - result = subprocess.run(["bash", str(root / "gradlew"), "-p", str(fixture), - *(["--offline"] if args.offline else []), - "--console=plain", *tasks], stdout=log, stderr=subprocess.STDOUT) - if negative: - diagnostic = (output / (name + ".log")).read_text() - if result.returncode == 0 or "global synthetics are missing" not in diagnostic: - raise SystemExit("Negative regression failed; see " + str(output)) - if (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").exists(): - raise SystemExit("Failed packaging left a usable-looking output") - elif result.returncode: - raise SystemExit("Build failed; see " + str(output / (name + ".log"))) - - run("missing-support-rejected", ["prepareFrameworkDexDebug", "-PmonoDex"], negative=True) - run("debug", ["testFrameworkZipDebug"]) - shutil.copyfile(fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex", output / "debug-vector.dex") - run("release", ["testFrameworkZipRelease"]) - shutil.copyfile(fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex", output / "release-vector.dex") - for variant in ("debug", "release"): - with zipfile.ZipFile(fixture / f"build/test-zips/framework-{variant}.zip") as archive: - dex_entries = [name for name in archive.namelist() if name.endswith(".dex")] - if dex_entries != ["framework/vector.dex"]: - raise SystemExit(f"Unexpected or duplicate framework DEX entries: {dex_entries}") - if archive.read(dex_entries[0]) != (output / f"{variant}-vector.dex").read_bytes(): - raise SystemExit("Packaged DEX differs from prepared single-DEX output") - -print("PASS: missing record support rejected; debug/release each package exactly one framework/vector.dex") + token = "@" + key + "@" + if token not in text: + raise SystemExit(f"Fixture template is missing {token}") + text = text.replace(token, value) +remaining = re.findall(r"@[A-Z][A-Z0-9_]*@", text) +if remaining: + raise SystemExit(f"Unresolved fixture placeholders: {remaining}") + +# Keep the generated build and its inputs alongside logs for failure diagnosis. +fixture = output / "fixture" +fixture.mkdir() +for name in ("settings.gradle.kts", "fixture-rules.pro"): + shutil.copyfile(root / "tests/recordtag/fixture" / name, fixture / name) +shutil.copytree(root / "tests/recordtag/fixture/src", fixture / "src") +(fixture / "build.gradle.kts").write_text(text) + +def run(name, tasks, negative=False, expected="global synthetics are missing"): + with (output / (name + ".log")).open("w") as log: + result = subprocess.run(["bash", str(root / "gradlew"), "-p", str(fixture), + *(["--offline"] if args.offline else []), + "--console=plain", *tasks], stdout=log, stderr=subprocess.STDOUT) + if negative: + diagnostic = (output / (name + ".log")).read_text() + if result.returncode == 0 or expected not in diagnostic: + raise SystemExit("Negative regression failed; see " + str(output)) + if (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").exists(): + raise SystemExit("Failed packaging left a usable-looking output") + elif result.returncode: + raise SystemExit("Build failed; see " + str(output / (name + ".log"))) + +run("missing-support-rejected", ["prepareFrameworkDexDebug", "-PmonoDex"], negative=True) +run("debug", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug"]) +run("debug-repeat", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug"]) +if ":prepareFrameworkDexDebug UP-TO-DATE" not in (output / "debug-repeat.log").read_text(): + raise SystemExit("Repeated debug build did not reuse prepared DEX; see debug-repeat.log") +previous = (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").read_bytes() +probe = fixture / "src/main/java/org/matrix/vector/recordtest/AddedProbe.java" +probe.write_text("package org.matrix.vector.recordtest; public class AddedProbe { public static int value() { return 123; } }") +run("debug-changed", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug"]) +if previous == (fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex").read_bytes(): + raise SystemExit("Source change did not regenerate prepared DEX") +shutil.copyfile(fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex", output / "debug-vector.dex") +run("release", ["testFrameworkZipRelease", "verifyFrameworkClassesRelease"]) +run("release-repeat", ["testFrameworkZipRelease", "verifyFrameworkClassesRelease"]) +if ":prepareFrameworkDexRelease UP-TO-DATE" not in (output / "release-repeat.log").read_text(): + raise SystemExit("Repeated release build did not reuse prepared DEX") +previous = (fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex").read_bytes() +probe.write_text(probe.read_text().replace("return 123", "return 124")) +run("release-changed", ["testFrameworkZipRelease", "verifyFrameworkClassesRelease"]) +if previous == (fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex").read_bytes(): + raise SystemExit("Release source change did not regenerate prepared DEX") +shutil.copyfile(fixture / "build/intermediates/framework_dex/release/minifyReleaseWithR8/classes.dex", output / "release-vector.dex") +for variant in ("debug", "release"): + with zipfile.ZipFile(fixture / f"build/test-zips/framework-{variant}.zip") as archive: + dex_entries = [name for name in archive.namelist() if name.endswith(".dex")] + if dex_entries != ["framework/vector.dex"]: + raise SystemExit(f"Unexpected or duplicate framework DEX entries: {dex_entries}") + if archive.read(dex_entries[0]) != (output / f"{variant}-vector.dex").read_bytes(): + raise SystemExit("Packaged DEX differs from prepared single-DEX output") + +# Simulate the documented NIO exception without adding a filesystem dependency. +# Only the first move is replaced; the production fallback still performs the move. +def force_non_atomic_move(source): + # Match the operation's arguments, allowing Kotlin's normal whitespace/trailing comma styles. + operation = r"""Files\s*\.\s*move\s*\( + \s*staged\s*\.\s*toPath\s*\(\s*\)\s*, + \s*output\s*\.\s*toPath\s*\(\s*\)\s*, + \s*StandardCopyOption\s*\.\s*ATOMIC_MOVE\s*, + \s*StandardCopyOption\s*\.\s*REPLACE_EXISTING\s*,?\s*\)""" + pattern = re.compile(operation, re.VERBOSE) + if len(list(pattern.finditer(source))) != 1: + raise SystemExit("Cannot inject atomic-move failure: expected exactly one staged atomic move in production task") + return pattern.sub('throw AtomicMoveNotSupportedException(staged.path, output.path, "fixture")', source) + +forced_fallback = force_non_atomic_move(text) +(fixture / "build.gradle.kts").write_text(forced_fallback) +run("atomic-move-fallback", ["testFrameworkZipDebug", "verifyFrameworkClassesDebug", "--rerun-tasks"]) +(fixture / "build.gradle.kts").write_text(text) +# Restore the Debug fixture snapshot after the Release source mutation above. +shutil.copyfile(fixture / "build/intermediates/framework_dex/debug/mergeDexDebug/classes.dex", output / "debug-vector.dex") +with zipfile.ZipFile(fixture / "build/test-zips/framework-debug.zip") as archive: + if archive.read("framework/vector.dex") != (output / "debug-vector.dex").read_bytes(): + raise SystemExit("Atomic-move fallback packaged incorrect bytes") + +archive_path = fixture / "build/test-zips/framework-debug.zip" +previous_archive = archive_path.read_bytes() +run("stale-output-rejected", ["testFrameworkZipDebug", "-PmonoDex"], negative=True) +if archive_path.read_bytes() != previous_archive: + raise SystemExit("Failed DEX preparation unexpectedly replaced the module ZIP") +if list((fixture / "build/intermediates/framework_dex").rglob("*.pending")): + raise SystemExit("DEX preparation left an unfinished staged output") + +for failure, diagnostic in (("duplicate", "defined multiple times"), ("corrupt", "Compilation failed")): + run(f"d8-{failure}-rejected", ["testFrameworkZipDebug", f"-PfailureApk={failure}"], + negative=True, expected=diagnostic) + if archive_path.read_bytes() != previous_archive: + raise SystemExit(f"D8 {failure} failure replaced the previous ZIP") + +# Two individually valid classes whose combined method references exceed one DEX. +for index in range(2): + (probe.parent / f"Overflow{index}.java").write_text( + f"package org.matrix.vector.recordtest; public class Overflow{index} {{\n" + + "".join(f"public static int m{method}() {{ return {method}; }}\n" for method in range(33000)) + "}\n") +run("single-dex-overflow-rejected", ["testFrameworkZipDebug"], negative=True, + expected="single-DEX loader's capacity") +if archive_path.read_bytes() != previous_archive: + raise SystemExit("Single-DEX overflow replaced the previous ZIP") + +print("PASS: class preservation, incremental builds, single-DEX ZIPs, missing support, corrupt DEX, duplicate classes and single-DEX overflow rejection") print("Artifacts:", output) diff --git a/tests/recordtag/runtime/NativeHookProbe.java b/tests/recordtag/runtime/NativeHookProbe.java new file mode 100644 index 000000000..9d26f09ef --- /dev/null +++ b/tests/recordtag/runtime/NativeHookProbe.java @@ -0,0 +1,97 @@ +import java.lang.reflect.Constructor; +import java.lang.reflect.Executable; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** Run only in a disposable app_process, never inside system_server. */ +public final class NativeHookProbe { + private static native boolean init(ClassLoader loader); + + private static ClassLoader loader; + private static Class invokerClass; + private static Class typeClass; + private static Method invoke; + private static Object full; + private static int hits; + + public static int target(int value) { + return value * 3; + } + + private static Object invoker(Method target, Object type) throws Exception { + Constructor constructor = invokerClass.getDeclaredConstructor(Method.class); + constructor.setAccessible(true); + Object instance = constructor.newInstance(target); + loader.loadClass("io.github.libxposed.api.XposedInterface$Invoker") + .getMethod("setType", typeClass).invoke(instance, type); + return instance; + } + + /** LSPlant's trampoline delegates to the production invoker and interceptor chain. */ + public static final class Hooker { + public Hooker(Executable target) {} + + public Object callback(Object[] args) throws Exception { + return invoke.invoke(full, null, args); + } + } + + private static void check(Object actual, int expected) { + if (!Integer.valueOf(expected).equals(actual)) { + throw new AssertionError(actual + " != " + expected); + } + } + + public static void main(String[] args) throws Exception { + if (args.length != 4) { + throw new IllegalArgumentException("Expected DEX, JNI library, invoker and hook-record class names"); + } + loader = (ClassLoader) Class.forName("dalvik.system.InMemoryDexClassLoader") + .getConstructor(ByteBuffer.class, ClassLoader.class) + .newInstance(ByteBuffer.wrap(Files.readAllBytes(Paths.get(args[0]))), + NativeHookProbe.class.getClassLoader()); + System.load(args[1]); + if (!init(loader)) throw new AssertionError("Native initialization failed"); + Class bridge = loader.loadClass("org.matrix.vector.nativebridge.HookBridge"); + invokerClass = loader.loadClass(args[2]); + typeClass = loader.loadClass("io.github.libxposed.api.XposedInterface$Invoker$Type"); + Class chain = loader.loadClass(typeClass.getName() + "$Chain"); + Object all = chain.getField("FULL").get(null); + Object origin = loader.loadClass(typeClass.getName() + "$Origin").getConstructor().newInstance(); + Method target = NativeHookProbe.class.getMethod("target", int.class); + invoke = loader.loadClass("io.github.libxposed.api.XposedInterface$Invoker") + .getMethod("invoke", Object.class, Object[].class); + full = invoker(target, all); + Object original = invoker(target, origin); + Class hookInterface = loader.loadClass("io.github.libxposed.api.XposedInterface$Hooker"); + Class chainInterface = loader.loadClass("io.github.libxposed.api.XposedInterface$Chain"); + Object callback = Proxy.newProxyInstance(loader, new Class[]{hookInterface}, (proxy, method, arguments) -> { + if (!method.getName().equals("intercept")) throw new AssertionError(method); + hits++; + return ((Integer) chainInterface.getMethod("proceed").invoke(arguments[0])) + 100; + }); + Class mode = loader.loadClass("io.github.libxposed.api.XposedInterface$ExceptionMode"); + Object record = loader.loadClass(args[3]).getConstructors()[0] + .newInstance(callback, 50, mode.getEnumConstants()[0], "recordtag-probe"); + Method hook = bridge.getMethod("hookMethod", boolean.class, Executable.class, Class.class, int.class, Object.class); + check(target.invoke(null, 7), 21); + if (!Boolean.TRUE.equals(hook.invoke(null, true, target, Hooker.class, 50, record))) { + throw new AssertionError("Hook failed"); + } + check(target.invoke(null, 7), 121); + check(invoke.invoke(original, null, new Object[]{7}), 21); + Object filtered = invoker(target, chain.getConstructor(int.class).newInstance(49)); + check(invoke.invoke(filtered, null, new Object[]{7}), 21); + check(invoke.invoke(full, null, new Object[]{7}), 121); + if (hits != 2) throw new AssertionError("Hook hits=" + hits); + if (!Boolean.TRUE.equals(bridge.getMethod("unhookMethod", boolean.class, Executable.class, Object.class) + .invoke(null, true, target, record))) { + throw new AssertionError("Unhook failed"); + } + check(target.invoke(null, 7), 21); + System.out.println("PASS: real native hook dispatch, Chain.FULL, priority filtering, Origin bypass, original return values and unhook"); + } +} diff --git a/tests/recordtag/runtime/native_hook_probe.cpp b/tests/recordtag/runtime/native_hook_probe.cpp new file mode 100644 index 000000000..a60937985 --- /dev/null +++ b/tests/recordtag/runtime/native_hook_probe.cpp @@ -0,0 +1,39 @@ +#include "core/context.h" +#include "core/native_api.h" +#include "elf/elf_image.h" +#include "elf/symbol_cache.h" +#include "jni/jni_hooks.h" + +using namespace vector::native; + +// This context belongs exclusively to the disposable test process. +class ProbeContext : public Context { +public: + static void Setup(JNIEnv *env, jobject loader) { + auto context = new ProbeContext; + context->inject_class_loader_ = env->NewGlobalRef(loader); + instance_.reset(context); + } + void LoadDex(JNIEnv *, PreloadedDex &&) override {} + void SetupEntryClass(JNIEnv *) override {} +}; + +extern "C" JNIEXPORT jboolean JNICALL +Java_NativeHookProbe_init(JNIEnv *env, jclass, jobject loader) { + ProbeContext::Setup(env, loader); + const auto *art = ElfSymbolCache::GetArt(); + if (!art) return false; + if (!lsplant::Init(env, { + .inline_hooker = [](void *target, void *replace) -> void * { + void *backup = nullptr; + return HookInline(target, replace, &backup) == 0 ? backup : nullptr; + }, + .inline_unhooker = [](void *target) { return UnhookInline(target) == 0; }, + .art_symbol_resolver = [art](auto symbol) { return art->getSymbAddress(symbol); }, + .art_symbol_prefix_resolver = [art](auto symbol) { + return art->getSymbPrefixFirstAddress(symbol); + }, + })) return false; + jni::RegisterHookBridge(env); + return !env->ExceptionCheck(); +} diff --git a/tests/recordtag/runtime/run-native.py b/tests/recordtag/runtime/run-native.py new file mode 100644 index 000000000..d77211ab1 --- /dev/null +++ b/tests/recordtag/runtime/run-native.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +"""Build and run a disposable arm64 hook probe using the selected module's actual DEX. + +Requires an already built :zygisk:zipDebug or :zygisk:zipRelease, Linux NDK, +ANDROID_HOME, JAVA_HOME and an explicitly selected root-ADB device. No module +installation, policy changes, reboot or persistent hook configuration is performed. +""" +import argparse +import os +from pathlib import Path +import re +import subprocess +import sys +import zipfile + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('--variant', choices=('debug', 'release'), required=True) +parser.add_argument('--serial', required=True) +parser.add_argument('--output', type=Path, required=True) +parser.add_argument('--timeout', type=int, default=30, help='Device probe timeout in seconds (default: 30)') +args = parser.parse_args() +if args.timeout < 1: + parser.error('--timeout must be a positive number of seconds') +root = Path(__file__).resolve().parents[3] +out = args.output.resolve() +out.mkdir(parents=True, exist_ok=False) +sdk = Path(os.environ['ANDROID_HOME']) +java = Path(os.environ['JAVA_HOME']) +config = (root / 'build.gradle.kts').read_text() +def setting(name): + match = re.search(r'val ' + name + r' = ("[^"\n]+"|[0-9]+)', config) + if not match: + raise SystemExit(f'Cannot find {name} in root build configuration') + return match.group(1).strip('"') + +def run(command, timeout=180): + with (out / 'commands.log').open('a') as log: + try: + result = subprocess.run([str(value) for value in command], stdout=log, + stderr=subprocess.STDOUT, timeout=timeout) + except subprocess.TimeoutExpired as error: + raise SystemExit(f'Command timed out after {timeout}s; see {out / "commands.log"}') from error + if result.returncode: + raise SystemExit(f'Command failed ({result.returncode}); see {out / "commands.log"}') + +variant = args.variant +native_build = 'Debug' if variant == 'debug' else 'RelWithDebInfo' +builds = list((root / f'zygisk/build/{native_build}').glob('*/arm64-v8a/native/libnative.a')) +if len(builds) != 1: + raise SystemExit(f'Build :zygisk:zip{variant.capitalize()} first; expected one native build, found {len(builds)}') +build = builds[0].parents[1] +archives = list(build.rglob('*.a')) +include_dirs = ['native/include', 'external/lsplant/lsplant/src/main/jni/include', + 'external/dobby/include', 'external/fmt/include'] +compiler = sdk / f'ndk/{setting("androidCompileNdkVersion")}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android27-clang++' +run([compiler, '-shared', '-fPIC', '-std=c++23', '-static-libstdc++', '-DFMT_HEADER_ONLY', + '-DVERSION_CODE=0', '-DVERSION_NAME="runtime-probe"', + *(['-DNDEBUG'] if variant == 'release' else []), + *[arg for directory in include_dirs for arg in ('-I', root / directory)], + Path(__file__).with_name('native_hook_probe.cpp'), '-Wl,--start-group', *archives, + '-Wl,--end-group', '-llog', '-ldl', '-lz', '-o', out / 'libprobe.so']) +classes = out / 'classes' +classes.mkdir() +dex = out / 'dex' +dex.mkdir() +run([java / 'bin/javac', '--release', '8', '-Xlint:-options', '-d', classes, Path(__file__).with_name('NativeHookProbe.java')]) +run([sdk / f'build-tools/{setting("androidBuildToolsVersion")}/d8', '--min-api', '27', + '--output', dex, *classes.glob('*.class')]) +modules = list((root / 'zygisk/release').glob(f'*-{variant.capitalize()}.zip')) +if len(modules) != 1: + raise SystemExit(f'Expected one {variant} module ZIP') +with zipfile.ZipFile(modules[0]) as archive: + (out / 'framework.dex').write_bytes(archive.read('framework/vector.dex')) + +names = ['org.matrix.vector.impl.hooks.VectorMethodInvoker', 'org.matrix.vector.impl.hooks.VectorHookRecord'] +if variant == 'release': + mapping = (root / 'zygisk/build/outputs/mapping/release/mapping.txt').read_text() + for index, name in enumerate(names): + match = re.search(r'^' + re.escape(name) + r' -> ([^:]+):$', mapping, re.M) + if not match: + raise SystemExit(f'Release mapping missing {name}') + names[index] = match.group(1) +adb = [sdk / 'platform-tools/adb', '-s', args.serial] +# mktemp allocates a unique directory; the finally block removes only that directory. +try: + remote = subprocess.check_output([*map(str, adb), 'shell', + 'mktemp -d /data/local/tmp/vector-recordtag.XXXXXX'], text=True, timeout=15).strip() +except subprocess.TimeoutExpired as error: + raise SystemExit('Timed out creating temporary device directory') from error +if not re.fullmatch(r'/data/local/tmp/vector-recordtag\.[A-Za-z0-9]+', remote): + raise SystemExit(f'Unexpected temporary device directory: {remote!r}') +try: + for source, target in ((out / 'framework.dex', 'framework.dex'), (dex / 'classes.dex', 'probe.dex'), (out / 'libprobe.so', 'libprobe.so')): + run([*adb, 'push', source, remote + '/' + target]) + run([*adb, 'shell', f'timeout -s KILL {args.timeout} env CLASSPATH={remote}/probe.dex app_process /system/bin NativeHookProbe ' + f'{remote}/framework.dex {remote}/libprobe.so {names[0]} {names[1]}'], timeout=args.timeout + 15) +finally: + failed = sys.exc_info()[0] is not None + try: + run([*adb, 'shell', 'rm -rf ' + remote], timeout=15) + except SystemExit as error: + if not failed: + raise + print(f'Cleanup failed for {remote}: {error}', file=sys.stderr) +print(f'PASS: {variant} native hook/original-call probe; evidence: {out}') diff --git a/zygisk/build.gradle.kts b/zygisk/build.gradle.kts index 7b274a498..298079468 100644 --- a/zygisk/build.gradle.kts +++ b/zygisk/build.gradle.kts @@ -6,6 +6,9 @@ import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction import com.android.tools.smali.dexlib2.iface.reference.FieldReference import com.android.tools.smali.dexlib2.iface.reference.MethodReference import com.android.tools.smali.dexlib2.iface.reference.TypeReference +import java.nio.file.AtomicMoveNotSupportedException +import java.nio.file.Files +import java.nio.file.StandardCopyOption import java.security.MessageDigest import java.util.zip.ZipFile import org.apache.commons.codec.binary.Hex @@ -101,7 +104,7 @@ abstract class PrepareFrameworkDex : DefaultTask() { @TaskAction fun prepare() { val output = outputFile.get().asFile - output.delete() + check(!output.exists() || output.delete()) { "Cannot remove previous framework DEX: $output" } val apks = apkDirectory.get().asFile.listFiles { file -> file.extension == "apk" }.orEmpty() check(apks.size == 1) { "Expected one framework APK, found ${apks.size}" } val work = temporaryDir.resolve("dex") @@ -110,6 +113,10 @@ abstract class PrepareFrameworkDex : DefaultTask() { val inputs = ZipFile(apks.single()).use { apk -> apk.entries().asSequence() .filter { Regex("classes(?:[2-9]|[1-9][0-9]+)?\\.dex").matches(it.name) } + .sortedBy { + if (it.name == "classes.dex") 1 + else it.name.removePrefix("classes").removeSuffix(".dex").toInt() + } .map { entry -> work.resolve(entry.name).also { file -> apk.getInputStream(entry).use { source -> @@ -137,7 +144,24 @@ abstract class PrepareFrameworkDex : DefaultTask() { "Framework DEX references RecordTag without defining it; global synthetics are missing" } output.parentFile.mkdirs() - candidate.copyTo(output, overwrite = true) + val staged = output.resolveSibling("${output.name}.pending") + try { + candidate.copyTo(staged, overwrite = true) + try { + Files.move( + staged.toPath(), output.toPath(), + StandardCopyOption.ATOMIC_MOVE, + StandardCopyOption.REPLACE_EXISTING, + ) + } catch (_: AtomicMoveNotSupportedException) { + Files.move(staged.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING) + } + } catch (failure: Exception) { + output.delete() + throw failure + } finally { + staged.delete() + } } }