Visitar URL original
[AGP 9.1.0 Migration #4] Replace plugin build-type copy with initWith on public DSL by reidbaker-agent · Pull Request #191606 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[AGP 9.1.0 Migration #4] Replace the plugin build-type copy with init…
…With on the public DSL

- PluginHandler no longer imports com.android.build.gradle.internal.dsl.BuildType.
  The build-type copy block that shared live legacy BuildType instances
  (addAll) for app-type plugin projects and hand-copied two properties
  for library plugin projects is replaced by a single initWith-based
  copy on the public DSL containers: missing build types are created on
  the plugin project with initWith(appBuildType) (which carries
  matchingFallbacks), and isDebuggable is additionally copied when both
  sides are application build types. Library build types cannot receive
  app-specific properties through the public DSL - this is a documented
  behavior change of the migration.
- Production sources are now free of com.android.build.gradle.internal
  imports; InternalAgpApiImportTest locks that in.
- PluginHandlerTest: replaced legacy mock-only copy tests with tests
  that run configurePlugins and assert the initWith copy for both library
  and app plugin projects, including the custom-debuggable-build-type ->
  debug engine artifact mapping and skipping pre-existing build types.
- Updated documentation with P3 pre-spike and finalizeDsl fallback details.
  • Loading branch information
reidbaker-agent committed Aug 25, 2026
commit 85e9f2bf342cd8019d64d862441c14941a890cc1
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ projects. That opt-out dies with AGP 10.
signal (the library-plugin build-type copy in `PluginHandler`). This
preserves add-to-app custom-debuggable matching (a host `staging`
debuggable build type maps to debug engine artifacts).
6. **P3 pre-spike / PR 4 (afterEvaluate DSL mutation under newDsl).** The planned scratch-app

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What purpose does this planning doc serve now that the work is done. Do any future prs in the 11 pr stack reference P3?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This design/migration doc serves as the persistent architectural record for the AGP 9 public API migration (#180137) documenting why specific public API approaches (like initWith, AgpCommonExtensionWrapper, and onVariants) were chosen and how edge cases (like finalizeDsl fallback) are handled. Future PRs in the stack (PR 10 and PR 11) reference this document for the complete migration context.

spike (AGP 9.1 + `newDsl=true` + custom build type, verifying that build-type
creation from `pluginProject.afterEvaluate` still works) could not run in the
implementation sandbox (no AGP artifact access). The `initWith` copy landed on the
primary approach; the `android_plugin_example_app_build` integration test and a
custom-build-type scratch build must confirm it in CI. Documented fallback if
`afterEvaluate` mutation is rejected under newDsl: perform the copy in
`androidComponents.finalizeDsl` on the plugin project instead.

## Replacement map

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

package com.flutter.gradle.plugins

import com.android.build.api.dsl.ApplicationBuildType
import com.flutter.gradle.CompileSdkVersion
import com.flutter.gradle.FlutterExtension
import com.flutter.gradle.FlutterPluginUtils
import com.flutter.gradle.FlutterPluginUtils.addApiDependencies
import com.flutter.gradle.FlutterPluginUtils.buildModeFor
import com.flutter.gradle.FlutterPluginUtils.getAndroidExtension
import com.flutter.gradle.FlutterPluginUtils.getCompileSdkFromProject
import com.flutter.gradle.FlutterPluginUtils.getLegacyAndroidExtension
import com.flutter.gradle.FlutterPluginUtils.isBuiltAsApp
import com.flutter.gradle.FlutterPluginUtils.supportsBuildMode
import com.flutter.gradle.NativePluginLoaderReflectionBridge
import org.gradle.api.Project
Expand Down Expand Up @@ -132,12 +131,41 @@ class PluginHandler(
)
}

copyAppBuildTypesToPlugin(project, pluginProject)

getAndroidExtension(project).buildTypes.forEach { buildType ->
addEmbeddingDependencyToPlugin(project, pluginProject, buildType, engineVersion)
}
}
}

private fun copyAppBuildTypesToPlugin(
project: Project,
pluginProject: Project
) {
if (!pluginProject.hasProperty("android")) {
return
}

// Copy the app project's build types onto the plugin project so that its variants
// resolve. These are `initWith` copies, not live aliases: `initWith` copies the
// properties both build types understand (matchingFallbacks included), and
// app-specific properties are additionally copied when both sides are application
// build types. Library build types cannot receive app-specific properties (such
// as isDebuggable) through the public DSL.
Comment on lines +150 to +155

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is the point of this work to help libraries handle the migration with fewer breaking changes? If so can you give an example? Or maybe is this a carry over from supporting flavors. Either way I dont understand why this change is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In Gradle / AGP, when an app defines custom build types (e.g. staging, profile_qa), Gradle variant matching requires plugin library subprojects (e.g. :camera_android) to define matching build types or compatible matchingFallbacks. Because Flutter plugins are authored independently and only define debug and release, building an app with custom build types would fail variant resolution without this copy step.

copyAppBuildTypesToPlugin copies missing build types from the app to the plugin project. This PR migrates that copy logic away from internal AGP types (com.android.build.gradle.internal.dsl.BuildType) to the public DSL create(name) { initWith(appBuildType) }.

val pluginProjectBuildTypes = getAndroidExtension(pluginProject).buildTypes
getAndroidExtension(project).buildTypes.forEach { appBuildType ->
if (pluginProjectBuildTypes.findByName(appBuildType.name) == null) {
pluginProjectBuildTypes.create(appBuildType.name) {
initWith(appBuildType)
if (this is ApplicationBuildType && appBuildType is ApplicationBuildType) {
isDebuggable = appBuildType.isDebuggable
}
}
}
}
}

private fun addEmbeddingDependencyToPlugin(
project: Project,
pluginProject: Project,
Expand All @@ -157,33 +185,6 @@ class PluginHandler(
return
}

// Copy build types from the app to the plugin.
// This allows to build apps with plugins and custom build types or flavors.
// However, only copy if the plugin is also an app project, since library projects
// cannot have applicationIdSuffix and other app-specific properties.
if (isBuiltAsApp(pluginProject)) {
getLegacyAndroidExtension(project).buildTypes.forEach { appBuildType ->
val pluginBuildTypes = getLegacyAndroidExtension(pluginProject).buildTypes
if (pluginBuildTypes.findByName(appBuildType.name) == null) {
pluginBuildTypes.create(appBuildType.name) {
initWith(appBuildType)
}
}
}
} else {
// For library projects, create compatible build types without app-specific properties
getLegacyAndroidExtension(project).buildTypes.forEach { appBuildType ->
if (getLegacyAndroidExtension(pluginProject).buildTypes.findByName(appBuildType.name) == null) {
getLegacyAndroidExtension(pluginProject).buildTypes.create(appBuildType.name) {
// Copy library-compatible properties only
isDebuggable = appBuildType.isDebuggable
isMinifyEnabled = appBuildType.isMinifyEnabled
// Note: applicationIdSuffix and other app-specific properties are intentionally not copied
}
}
}
}

// The embedding is API dependency of the plugin, so the AGP is able to desugar
// default method implementations when the interface is implemented by a plugin.
//
Expand Down Expand Up @@ -218,6 +219,9 @@ class PluginHandler(
}
val pluginProject: Project = project.rootProject.findProject(":$pluginName") ?: return

// Warning: Iterating buildTypes causes the project-level 'implementation' dependency to be added multiple times.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I added this comment because it came up in adversarial review but was unrelated to the change in this pr.

// When an app defines 3 build types (debug, profile, release), this registers 3
// separate afterEvaluate actions adding the same dependencyProject.
getAndroidExtension(project).buildTypes.forEach { buildType ->
val flutterBuildMode: String = buildModeFor(buildType)
if (flutterBuildMode == "release" && (pluginObject["dev_dependency"] as? Boolean == true)) {
Expand All @@ -235,7 +239,6 @@ class PluginHandler(
val dependencyProject =
project.rootProject.findProject(":$pluginDependencyName") ?: return@innerForEach
pluginProject.afterEvaluate {
// this.dependencies.add("implementation", dependencyProject)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Intentional removal of dead code.

pluginProject.dependencies.add("implementation", dependencyProject)
}
}
Expand Down
Loading