From 033a0e170962497915b7006dc019e5f25122f4e2 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:49:44 +0000 Subject: [PATCH] chore: Check the whole patches file against the Android API Bundled dependencies were not checked, so a library that uses AWT in its signatures passed the build and then broke loading in Manager. Run D8 at min API 26 with class file output on the patches file and check the result, so bundled dependencies are included and methods D8 backports, such as List.of() and Optional.isEmpty(), are not flagged. Use the gummy-bears API 26 signature. --- gradle/libs.versions.toml | 4 +-- patches/build.gradle.kts | 71 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1155a65bf..fc29d7ed0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,11 +4,11 @@ morphe-patcher = "2.5.0" smali = "5eaff5a2c9" gson = "2.15.0" animalsniffer = "2.0.1" -android-api-signature = "8.0.0_r2" +gummy-bears = "0.16.0" [libraries] gson = { module = "com.google.code.gson:gson", version.ref = "gson" } -android-api-signature = { module = "net.sf.androidscents.signature:android-api-level-26", version.ref = "android-api-signature" } +android-api-signature = { module = "com.toasttab.android:gummy-bears-api-26", version.ref = "gummy-bears" } # Gradle plugins adds the dependency during build, this declaration is here only for dependabot to provide updates. #noinspection UnusedVersionCatalogEntry morphe-patcher = { module = "app.morphe:morphe-patcher", version.ref = "morphe-patcher" } diff --git a/patches/build.gradle.kts b/patches/build.gradle.kts index dd569a26a..f1eba17ca 100644 --- a/patches/build.gradle.kts +++ b/patches/build.gradle.kts @@ -1,5 +1,12 @@ +import com.android.tools.r8.CompilationMode +import com.android.tools.r8.D8 +import com.android.tools.r8.D8Command +import com.android.tools.r8.OutputMode +import com.android.tools.r8.utils.ArchiveResourceProvider +import ru.vyarus.gradle.plugin.animalsniffer.AnimalSniffer + plugins { - // Fails the build if patch code uses JDK APIs that do not exist on Android. + // Fails the build if the patches file uses JDK APIs that do not exist on Android. alias(libs.plugins.animalsniffer) } @@ -31,8 +38,9 @@ dependencies { } animalsniffer { - // Only the main source set ships in the patches file. - sourceSets = listOf(project.sourceSets["main"]) + // Checking only the compiled sources would miss bundled dependencies, + // so the whole patches file is checked by checkAndroidApi below. + sourceSets = emptyList() } tasks { @@ -45,9 +53,62 @@ tasks { mainClass.set("util.PatchListGeneratorKt") } - // The check task already runs it, but buildAndroid does not depend on check. + // 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") + + inputs.file(patchesFile) + inputs.files(classpath) + outputs.file(output) + + doLast { + val patchesPath = patchesFile.get().asFile.toPath() + val outputPath = output.get().asFile.toPath() + + D8.run( + D8Command.builder() + .addProgramResourceProvider(ArchiveResourceProvider.fromArchive(patchesPath, true)) + .addClasspathFiles(classpath.files.filter { it.exists() }.map { it.toPath() }) + .setMode(CompilationMode.DEBUG) + .setMinApiLevel(26) + .setOutput(outputPath, OutputMode.ClassFile) + .build() + ) + } + } + + val extractPatchesClasses = register("extractPatchesClasses") { + from(desugarPatchesClasses.map { zipTree(it.outputs.files.singleFile) }) { + include("**/*.class") + } + into(layout.buildDirectory.dir("animalsniffer/patches-classes")) + } + + val checkAndroidApi = register("checkAndroidApi") { + description = "Checks that the patches file only uses APIs available on Android." + group = "verification" + + source(extractPatchesClasses) + classpath = sourceSets["main"].compileClasspath + animalsnifferSignatures = configurations["signature"] + animalsnifferClasspath = configurations["animalsniffer"] + // Only used to show source lines in the report. + sourcesDirs = sourceSets["main"].allSource.sourceDirectories + reports.text.outputLocation = layout.buildDirectory.file("reports/animalsniffer/patches.text") + reports.csv.outputLocation = layout.buildDirectory.file("reports/animalsniffer/patches.csv") + } + + check { + dependsOn(checkAndroidApi) + } + + // buildAndroid does not depend on check. named("buildAndroid") { - dependsOn("animalsnifferMain") + dependsOn(checkAndroidApi) } // Used by gradle-semantic-release-plugin.