Repository navigation
[beta] [flutter_tools] Do not copy app-only build type properties onto library plugins - #196757
Conversation
…ry plugins (flutter#196692) Fixes flutter#196412 Fixes flutter#196696 Part of flutter#196689 Since Flutter 3.50, an app fails to configure, even in debug mode, when it uses a plugin and one of its build types sets something that a library module can't have. Examples are `applicationIdSuffix` on `profile` (flutter#196412), `isShrinkResources` (flutter#196696) on a custom build type, or a `buildConfigField` or `resValue`. Example error: ``` A problem occurred configuring project ':jni'. > com.android.builder.errors.EvalIssueException: Library projects cannot set applicationIdSuffix. applicationIdSuffix is set to '.profile' in build type 'profile'. ``` This is a regression of flutter#169215. flutter#191606 changed the Flutter Gradle Plugin to copy each app build type that a plugin lacks onto the plugin with `initWith(appBuildType)`. In AGP 9.3.1, `initWith` copies every build type property (`AbstractBuildType.initWith`, `BaseConfigImpl._initWith`, `BuildType._initWith`), including several that AGP rejects in a library: | Property | AGP error in the library | | --- | --- | | `applicationIdSuffix` | `Library projects cannot set applicationIdSuffix` | | `isShrinkResources` | `Resource shrinker cannot be used for libraries.` The check runs inside `initWith`, so the value can't be reset afterwards. | | `buildConfigFields` | `contains custom BuildConfig fields, but the feature is disabled` | | `resValues` | `contains custom resource values, but the feature is disabled` | The `this is ApplicationBuildType` check that was meant to keep app-only properties away from library plugins is always true. AGP uses one build type class for every module type, and it implements `ApplicationBuildType`, `LibraryBuildType`, `DynamicFeatureBuildType` and `TestBuildType`. This PR: - Uses `initWith` only for plugins that are built as apps (`isBuiltAsApp`, as flutter#172704 did). An app accepts every build type property, and `initWith` already copies `isDebuggable`, so app plugins no longer set it separately. - Gives library plugins only `matchingFallbacks` and `isDebuggable`, in a separate `copyLibraryBuildTypeProperties` function: - `matchingFallbacks` lets the plugin's own dependencies resolve. flutter#191606 meant to add it. - `isDebuggable` isn't in the public library DSL, but library variants still read it, for example for `BuildConfig.DEBUG`, R8's debuggable flag and the CMake build type of FFI plugins. It is set through the `ApplicationBuildType` interface that AGP's build type class implements, as in Flutter 3.47. - `isMinifyEnabled` is no longer copied (3.47 copied it). A minified library build type makes AGP run R8 on the plugin with only the plugin's keep rules. That removes the plugin class that `GeneratedPluginRegistrant` uses, and the app's R8 run fails with `Missing class io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin`. The app's R8 run already shrinks plugin code. - Updates the unit tests. The library build type mock implements both the library and application interfaces, like the real AGP class. The test checks that `initWith` isn't called and that `isMinifyEnabled` isn't set. - Extends `android_plugin_example_app_build_test.dart`, which builds the example app of the `plugin` and `plugin_ffi` templates against the real AGP. Before the first build, the test now adds `applicationIdSuffix`, `versionNameSuffix`, `buildConfigField` and `resValue` to the `profile` build type, and adds a `staging` build type with `isMinifyEnabled` and `isShrinkResources`. For the `plugin` template, it also runs `./gradlew :app:assembleStaging`. The earlier tests were mock-only and checked that `initWith` was called, so they could not catch this. Verified locally on 3.50.0 and master: - On stock 3.50.0, the repro from flutter#196412 fails, and so do apps with only `isShrinkResources` or only `buildConfigField`. 3.47.9 builds them. - With this change, the flutter#196412 repro builds with `--debug` and `--profile`. `aapt2 dump badging` shows package `com.example.repro350.profile` and versionName `1.0.0-PROFILE` for the profile APK. An app with a `staging` build type that uses `initWith(release)`, `isMinifyEnabled` and `isShrinkResources` builds with `./gradlew :app:assembleStaging`. - On stock master and stock 3.50.0, both cases in `android_plugin_example_app_build_test.dart` fail with `Resource shrinker cannot be used for libraries.` and `contains custom BuildConfig fields, but the feature is disabled`. With this change, both cases pass on master and 3.50.0. If `isMinifyEnabled` is copied again, the `plugin` case fails with `R8: Missing class com.example.plugin_test.PluginTestPlugin`. - All Flutter Gradle Plugin unit tests pass on master against the default AGP and AGP 9.3.1, and on 3.50.0. ktlint and `dart analyze` are clean. - Tested on both ends of the supported AGP range: AGP 9.3.1 (the template default, with Gradle 9.5.0) and AGP 9.1.1 (the minimum in `DependencyVersionChecker.kt`, with Gradle 9.3.1). On AGP 9.1.1, both cases of `android_plugin_example_app_build_test.dart` pass with this change, covering the release APK, `:app:assembleStaging` and a profile APK. Without the change, they fail with the shrinker and BuildConfig errors. The Flutter Gradle Plugin unit tests also pass with `-Pflutter.internal.agpVersion=9.1.1`. I intend to cherry-pick this into 3.50.x. The change applies to 3.50 without edits, because the 3 files it changes are the same on 3.50.0 and master. I ran the tests above on 3.50.0 with this change applied. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md (cherry picked from commit 534fb4d)
|
This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter. Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed. |
There was a problem hiding this comment.
Code Review
This pull request updates the PluginHandler in the Flutter tools to distinguish between application and library plugin projects when configuring build types. It introduces a copyLibraryBuildTypeProperties method to avoid applying app-specific properties (like applicationIdSuffix or isMinifyEnabled) to library plugins, which causes build errors in the Android Gradle Plugin. Corresponding tests were updated to reflect these changes, including new integration tests that verify build type configuration for library plugins. I have no feedback to provide as there were no review comments.
dda8112
into
flutter:flutter-3.51-candidate.0
…nto library plugins (#196756) ### Issue Link: #196412 and #196696 Companion beta cherry-pick: #196757 ### Impact Description: [#191606](#191606), part of the migration to AGP's public Variant API, changed how the Flutter Gradle Plugin creates missing app build types on plugin projects. It shipped in 3.50.0 and in the current beta, 3.51.0-0.1.pre. Up to 3.47, library plugins received a compatible build type without app-only properties (#172704). Since #191606, `PluginHandler` called `initWith(appBuildType)` for every plugin. In AGP 9, `initWith` copies every build type property, and AGP rejects several of them in a library module: `applicationIdSuffix` (#196412), `isShrinkResources` (including any custom build type created with `initWith(getByName("release"))`, because Flutter enables `isShrinkResources` on `release` by default; #196696), `buildConfigFields`, and `resValues`. Because Gradle configures every build type of every subproject, affected apps fail to configure even in `debug` mode (`flutter run`). In addition, copying `isMinifyEnabled` onto library plugins caused R8 to strip plugin classes on custom minified build types (`Missing class ...Plugin`). This cherry-pick, [#196692](#196692) (`534fb4d6247`), restores separate handling for library plugins: plugins built as apps keep `initWith`, while library plugins receive only `matchingFallbacks` and `isDebuggable` (skipping app-only properties and `isMinifyEnabled`, because the app's R8 pass shrinks plugin code). It applied to `flutter-3.50-candidate.0` without conflicts (`0` diff vs `534fb4d6247`). ### Changelog Description: [flutter/196412] When building an Android app with plugins and custom or app-only buildType settings, Gradle configuration fails. ### Workaround: Pre-create the custom or `profile` build type in every library subproject from the root `android/build.gradle.kts` before Flutter copies it (`subprojects { pluginManager.withPlugin("com.android.library") { extensions.configure<com.android.build.api.dsl.LibraryExtension>("android") { buildTypes.maybeCreate("profile") } } }`), or for `applicationIdSuffix` set it via `androidComponents.onVariants`. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. Run `flutter create app && cd app && flutter pub add shared_preferences`. 2. In `android/app/build.gradle.kts`, set `applicationIdSuffix = ".profile"` on `profile` and add `create("staging") { initWith(getByName("release")) }` under `buildTypes`. 3. Run `flutter build apk --debug`, `flutter build apk --profile`, and `./gradlew -p android :app:assembleStaging`. All three succeed, and `aapt2 dump badging` on the profile APK shows package suffix `.profile`. 4. Covered by `PluginHandlerTest` and `android_plugin_example_app_build_test.dart` (verified on both AGP 9.1.1 and AGP 9.3.1).
…nto library plugins (#196756) ### Issue Link: flutter/flutter#196412 and flutter/flutter#196696 Companion beta cherry-pick: flutter/flutter#196757 ### Impact Description: [#191606](flutter/flutter#191606), part of the migration to AGP's public Variant API, changed how the Flutter Gradle Plugin creates missing app build types on plugin projects. It shipped in 3.50.0 and in the current beta, 3.51.0-0.1.pre. Up to 3.47, library plugins received a compatible build type without app-only properties (#172704). Since #191606, `PluginHandler` called `initWith(appBuildType)` for every plugin. In AGP 9, `initWith` copies every build type property, and AGP rejects several of them in a library module: `applicationIdSuffix` (#196412), `isShrinkResources` (including any custom build type created with `initWith(getByName("release"))`, because Flutter enables `isShrinkResources` on `release` by default; #196696), `buildConfigFields`, and `resValues`. Because Gradle configures every build type of every subproject, affected apps fail to configure even in `debug` mode (`flutter run`). In addition, copying `isMinifyEnabled` onto library plugins caused R8 to strip plugin classes on custom minified build types (`Missing class ...Plugin`). This cherry-pick, [#196692](flutter/flutter#196692) (`534fb4d6247`), restores separate handling for library plugins: plugins built as apps keep `initWith`, while library plugins receive only `matchingFallbacks` and `isDebuggable` (skipping app-only properties and `isMinifyEnabled`, because the app's R8 pass shrinks plugin code). It applied to `flutter-3.50-candidate.0` without conflicts (`0` diff vs `534fb4d6247`). ### Changelog Description: [flutter/196412] When building an Android app with plugins and custom or app-only buildType settings, Gradle configuration fails. ### Workaround: Pre-create the custom or `profile` build type in every library subproject from the root `android/build.gradle.kts` before Flutter copies it (`subprojects { pluginManager.withPlugin("com.android.library") { extensions.configure<com.android.build.api.dsl.LibraryExtension>("android") { buildTypes.maybeCreate("profile") } } }`), or for `applicationIdSuffix` set it via `androidComponents.onVariants`. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. Run `flutter create app && cd app && flutter pub add shared_preferences`. 2. In `android/app/build.gradle.kts`, set `applicationIdSuffix = ".profile"` on `profile` and add `create("staging") { initWith(getByName("release")) }` under `buildTypes`. 3. Run `flutter build apk --debug`, `flutter build apk --profile`, and `./gradlew -p android :app:assembleStaging`. All three succeed, and `aapt2 dump badging` on the profile APK shows package suffix `.profile`. 4. Covered by `PluginHandlerTest` and `android_plugin_example_app_build_test.dart` (verified on both AGP 9.1.1 and AGP 9.3.1).
Issue Link:
#196412
and #196696
Companion stable cherry-pick: #196756
Impact Description:
#191606, part of the migration to AGP's public Variant API, changed how the Flutter Gradle Plugin creates missing app build types on plugin projects. It shipped in 3.50.0 and in the current beta, 3.51.0-0.1.pre. Up to 3.47, library plugins received a compatible build type without app-only properties (#172704). Since #191606,
PluginHandlercalledinitWith(appBuildType)for every plugin. In AGP 9,initWithcopies every build type property, and AGP rejects several of them in a library module:applicationIdSuffix(#196412),isShrinkResources(including any custom build type created withinitWith(getByName("release")), because Flutter enablesisShrinkResourcesonreleaseby default; #196696),buildConfigFields, andresValues. Because Gradle configures every build type of every subproject, affected apps fail to configure even indebugmode (flutter run). In addition, copyingisMinifyEnabledonto library plugins caused R8 to strip plugin classes on custom minified build types (Missing class ...Plugin).This cherry-pick, #196692 (
534fb4d6247), restores separate handling for library plugins: plugins built as apps keepinitWith, while library plugins receive onlymatchingFallbacksandisDebuggable(skipping app-only properties andisMinifyEnabled, because the app's R8 pass shrinks plugin code). It applied toflutter-3.51-candidate.0without conflicts (0diff vs534fb4d6247).Changelog Description:
[flutter/196412] When building an Android app with plugins and custom or app-only buildType settings, Gradle configuration fails.
Workaround:
Pre-create the custom or
profilebuild type in every library subproject from the rootandroid/build.gradle.ktsbefore Flutter copies it (subprojects { pluginManager.withPlugin("com.android.library") { extensions.configure<com.android.build.api.dsl.LibraryExtension>("android") { buildTypes.maybeCreate("profile") } } }), or forapplicationIdSuffixset it viaandroidComponents.onVariants.Risk:
What is the risk level of this cherry-pick?
Test Coverage:
Are you confident that your fix is well-tested by automated tests?
Validation Steps:
flutter create app && cd app && flutter pub add shared_preferences.android/app/build.gradle.kts, setapplicationIdSuffix = ".profile"onprofileand addcreate("staging") { initWith(getByName("release")) }underbuildTypes.flutter build apk --debug,flutter build apk --profile, and./gradlew -p android :app:assembleStaging. All three succeed, andaapt2 dump badgingon the profile APK shows package suffix.profile.PluginHandlerTestandandroid_plugin_example_app_build_test.dart(verified on both AGP 9.1.1 and AGP 9.3.1).