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.
This commit is contained in:
LisoUseInAIKyrios committed 2026-11-21 12:49:44 +00:00
1 parent 1d5c5702cc
commit 033a0e1709
2 files changed
+68 -7

No files matched your search

+66 -5
View File
@@ -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<Sync>("extractPatchesClasses") {
from(desugarPatchesClasses.map { zipTree(it.outputs.files.singleFile) }) {
include("**/*.class")
}
into(layout.buildDirectory.dir("animalsniffer/patches-classes"))
}
val checkAndroidApi = register<AnimalSniffer>("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.