This examples shows the use of the androidx.annotation.keep Gradle plugin.
- This programmatically generates
keeprules when anapp/android library/java librarymodule usesandroidx.annotation:annotation-keep.
// App Modules
./gradlew <variantName>ExtractKeepRules
// For Android AAR Modules
./gradlew <variantName>KeepRulesTransformAarFor Java Libraries, you need to do the following.
- Apply the Gradle Plugin.
plugins {
alias(libs.plugins.androidx.annotation.keep)
}- Register a new task that can generate the artifact with keep rules.
val jarTask = project.tasks.named<org.gradle.jvm.tasks.Jar>("jar")
val outputJar = jarTask.flatMap { it.archiveFile }
val outputFile = jarTask.flatMap { it.archiveFileName }.map { fileName ->
"keep${fileName.uppercaseFirstChar()}"
}
// Register the JAR transform for extracting keep rules.
val artifactTask = annotationKeep.registerJavaArchiveTransform(
taskName = "keepRulesJar",
input = outputJar,
fileName = outputFile
)- Register the output artifact for publication.
// Register the new artifact.
project.configurations.configureEach {
if (name == "apiElements" || name == "runtimeElements") {
outgoing.artifact(artifactTask.flatMap { it.outputJar })
}
}