Auto-wire ProGuard rules into Android builds via Variant API#155
Auto-wire ProGuard rules into Android builds via Variant API#155kirich1409 wants to merge 1 commit intomainfrom
Conversation
Add AndroidProguardWiring that uses the AGP Variant API to inject the generated proguard-featured.pro into every Android variant automatically. The plugin now detects com.android.application/library and wires the task output — no manual proguardFiles() configuration needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR makes the Featured Gradle plugin automatically add its generated ProGuard/R8 -assumevalues rules file to Android builds using the AGP Variant API, removing the need for manual proguardFiles(...) configuration by consumers.
Changes:
- Add
AndroidProguardWiring.ktto inject the generated ProGuard rules into Android variants via the Variant API. - Update
FeaturedPluginto lazily wire ProGuard rules only when an Android plugin is applied, and return aTaskProviderfromregisterProguardTask(). - Add a
compileOnlydependency on the Android Gradle Plugin and update task KDoc to reflect the new auto-wiring behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
featured-gradle-plugin/src/main/kotlin/dev/androidbroadcast/featured/gradle/GenerateProguardRulesTask.kt |
Updates KDoc to describe automatic wiring via AGP Variant API. |
featured-gradle-plugin/src/main/kotlin/dev/androidbroadcast/featured/gradle/FeaturedPlugin.kt |
Returns the ProGuard task provider and wires the generated rules into Android variants when Android plugins are present. |
featured-gradle-plugin/src/main/kotlin/dev/androidbroadcast/featured/gradle/AndroidProguardWiring.kt |
New Variant API wiring that adds the generated rules file to variants. |
featured-gradle-plugin/build.gradle.kts |
Adds a compileOnly dependency on AGP for compiling Variant API integration. |
| listOf("com.android.application", "com.android.library").forEach { pluginId -> | ||
| target.plugins.withId(pluginId) { | ||
| wireProguardToVariants(target, proguardTask) | ||
| } | ||
| } |
There was a problem hiding this comment.
wireProguardToVariants() is only registered for com.android.application and com.android.library. This repo also applies com.android.kotlin.multiplatform.library (Android KMP) in multiple modules (e.g. core/build.gradle.kts), so those Android variants won't get the generated ProGuard rules automatically. Consider adding that plugin id (and any other Android plugin ids you support) to this wiring list so the behavior matches the PR's goal of auto-wiring for Android projects.
| androidComponents.onVariants { variant -> | ||
| variant.proguardFiles.add( | ||
| proguardTask.flatMap { it.outputFile }, | ||
| ) |
There was a problem hiding this comment.
This wires the generated rules into variant.proguardFiles for all variants. For Android library modules, proguardFiles affects only the library’s own minification, while consumers typically pick up rules via consumerProguardFiles (see e.g. providers/sharedpreferences/build.gradle.kts). If the intent is for consuming apps to benefit from dead-code elimination, you likely need to add the generated file as a consumer ProGuard file for library variants (or wire both appropriately for app vs library).
| } | ||
|
|
||
| dependencies { | ||
| compileOnly("com.android.tools.build:gradle:9.1.0") |
There was a problem hiding this comment.
AGP version is hardcoded here even though the repo uses a version catalog (gradle/libs.versions.toml has agp = "9.1.0"). Consider sourcing this dependency version from the catalog to avoid drift when AGP is bumped.
| compileOnly("com.android.tools.build:gradle:9.1.0") | |
| compileOnly("com.android.tools.build:gradle:${libs.versions.agp.get()}") |
| internal fun wireProguardToVariants( | ||
| project: Project, | ||
| proguardTask: TaskProvider<GenerateProguardRulesTask>, | ||
| ) { | ||
| val androidComponents = | ||
| project.extensions | ||
| .getByType(AndroidComponentsExtension::class.java) | ||
| androidComponents.onVariants { variant -> | ||
| variant.proguardFiles.add( | ||
| proguardTask.flatMap { it.outputFile }, | ||
| ) | ||
| } |
There was a problem hiding this comment.
This introduces new behavior that depends on the AGP Variant API (wiring into variants). There are unit tests for task registration, but no tests exercising this wiring on an actual Android project/variant (e.g., via Gradle TestKit) to verify the generated file is added for release variants and library consumer rules as intended. Adding an integration-style test would help prevent regressions across AGP upgrades.
Summary
AndroidProguardWiring.ktthat uses the AGP Variant API to inject generated ProGuard rules into every Android variantFeaturedPluginto detectcom.android.application/com.android.libraryand callwireProguardToVariants()lazilyregisterProguardTask()to returnTaskProvider<GenerateProguardRulesTask>for downstream wiringcompileOnlydependency on AGP 9.1.0GenerateProguardRulesTaskKDoc to reflect automatic wiring (remove manual configuration example)Consumers no longer need to manually add
proguardFiles(...)for the Featured plugin's generated rules — the plugin handles it automatically via the Variant API.Test plan
assembleReleaseincludes the generatedproguard-featured.progenerateProguardRulestask still works standalone🤖 Generated with Claude Code