Files
morphe-patches-template/patches/build.gradle.kts
T
LisoUseInAIKyrios 033a0e1709 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.
2026-11-21 12:49:44 +00:00

119 lines
4.2 KiB
Kotlin

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 the patches file uses JDK APIs that do not exist on Android.
alias(libs.plugins.animalsniffer)
}
group = "app.template"
patches {
// TODO: Update this section with your project details.
about {
name = "UserXYZ Patches"
description = "Patches for apps I like"
source = "[email protected]:UserXYZ/morphe-patches.git"
author = "Awesome dev"
contact = "na"
website = "na"
license = "GPLv3"
}
}
// Separate configuration so gson is available at runtime for the
// generatePatchesList task but never bundled into the APK.
val patchListGeneratorClasspath = configurations.create("patchListGeneratorClasspath")
dependencies {
compileOnly(libs.gson)
patchListGeneratorClasspath(libs.gson)
// Android API level 26 is the minimum API the patches are compiled for (see buildAndroid).
signature(libs.android.api.signature) { artifact { type = "signature" } }
}
animalsniffer {
// Checking only the compiled sources would miss bundled dependencies,
// so the whole patches file is checked by checkAndroidApi below.
sourceSets = emptyList()
}
tasks {
register<JavaExec>("generatePatchesList") {
description = "Build patch with patch list"
dependsOn(build)
classpath = sourceSets["main"].runtimeClasspath + patchListGeneratorClasspath
mainClass.set("util.PatchListGeneratorKt")
}
// 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(checkAndroidApi)
}
// Used by gradle-semantic-release-plugin.
publish {
dependsOn("generatePatchesList")
}
}