-
Notifications
You must be signed in to change notification settings - Fork 0
Auto-wire ProGuard rules into Android builds via Variant API #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package dev.androidbroadcast.featured.gradle | ||
|
|
||
| import com.android.build.api.variant.AndroidComponentsExtension | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.tasks.TaskProvider | ||
|
|
||
| /** | ||
| * Wires the generated ProGuard rules file into every Android variant via the AGP Variant API. | ||
| * | ||
| * Called lazily — only when `com.android.application` or `com.android.library` is present on | ||
| * the project. The task dependency is implicit through the [TaskProvider] chain, so no explicit | ||
| * `dependsOn` is required. | ||
| */ | ||
| 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 }, | ||
| ) | ||
|
Comment on lines
+21
to
+24
|
||
| } | ||
|
Comment on lines
+14
to
+25
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,10 +51,15 @@ public class FeaturedPlugin : Plugin<Project> { | |
|
|
||
| registerConfigParamTask(target, resolveTask) | ||
| registerFlagRegistrarTask(target, resolveTask) | ||
| registerProguardTask(target, resolveTask) | ||
| val proguardTask = registerProguardTask(target, resolveTask) | ||
| registerIosConstValTask(target, resolveTask) | ||
| registerXcconfigTask(target, resolveTask) | ||
| wireToRootAggregator(target, resolveTask) | ||
| listOf("com.android.application", "com.android.library").forEach { pluginId -> | ||
| target.plugins.withId(pluginId) { | ||
| wireProguardToVariants(target, proguardTask) | ||
| } | ||
| } | ||
|
Comment on lines
+58
to
+62
|
||
| } | ||
|
|
||
| private fun registerResolveFlagsTask(target: Project): TaskProvider<ResolveFlagsTask> = | ||
|
|
@@ -99,7 +104,7 @@ public class FeaturedPlugin : Plugin<Project> { | |
| private fun registerProguardTask( | ||
| target: Project, | ||
| resolveTask: TaskProvider<ResolveFlagsTask>, | ||
| ) { | ||
| ): TaskProvider<GenerateProguardRulesTask> = | ||
| target.tasks.register(GENERATE_PROGUARD_TASK_NAME, GenerateProguardRulesTask::class.java) { task -> | ||
| task.group = "featured" | ||
| task.description = "Generates ProGuard/R8 -assumevalues rules for local flags in '${target.path}'." | ||
|
|
@@ -108,7 +113,6 @@ public class FeaturedPlugin : Plugin<Project> { | |
| task.outputFile.set(target.layout.buildDirectory.file("featured/proguard-featured.pro")) | ||
| task.dependsOn(resolveTask) | ||
| } | ||
| } | ||
|
|
||
| private fun registerIosConstValTask( | ||
| target: Project, | ||
|
|
||
There was a problem hiding this comment.
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.tomlhasagp = "9.1.0"). Consider sourcing this dependency version from the catalog to avoid drift when AGP is bumped.