Skip to content

Auto-wire ProGuard rules into Android builds via Variant API#155

Open
kirich1409 wants to merge 1 commit intomainfrom
feat/proguard-autowire
Open

Auto-wire ProGuard rules into Android builds via Variant API#155
kirich1409 wants to merge 1 commit intomainfrom
feat/proguard-autowire

Conversation

@kirich1409
Copy link
Copy Markdown
Contributor

Summary

  • Add AndroidProguardWiring.kt that uses the AGP Variant API to inject generated ProGuard rules into every Android variant
  • Update FeaturedPlugin to detect com.android.application/com.android.library and call wireProguardToVariants() lazily
  • Change registerProguardTask() to return TaskProvider<GenerateProguardRulesTask> for downstream wiring
  • Add compileOnly dependency on AGP 9.1.0
  • Update GenerateProguardRulesTask KDoc 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

  • Apply the plugin to an Android app/library module and verify assembleRelease includes the generated proguard-featured.pro
  • Verify non-Android modules (pure JVM/KMP) still work without AGP on the classpath
  • Verify generateProguardRules task still works standalone

🤖 Generated with Claude Code

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>
Copilot AI review requested due to automatic review settings April 10, 2026 11:57
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.kt to inject the generated ProGuard rules into Android variants via the Variant API.
  • Update FeaturedPlugin to lazily wire ProGuard rules only when an Android plugin is applied, and return a TaskProvider from registerProguardTask().
  • Add a compileOnly dependency 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.

Comment on lines +58 to +62
listOf("com.android.application", "com.android.library").forEach { pluginId ->
target.plugins.withId(pluginId) {
wireProguardToVariants(target, proguardTask)
}
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +24
androidComponents.onVariants { variant ->
variant.proguardFiles.add(
proguardTask.flatMap { it.outputFile },
)
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
}

dependencies {
compileOnly("com.android.tools.build:gradle:9.1.0")
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
compileOnly("com.android.tools.build:gradle:9.1.0")
compileOnly("com.android.tools.build:gradle:${libs.versions.agp.get()}")

Copilot uses AI. Check for mistakes.
Comment on lines +14 to +25
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 },
)
}
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants