Repository navigation
Conversation
…uild types Since flutter#191606, the Flutter Gradle Plugin copies missing app build types onto plugin projects with `initWith(appBuildType)`. AGP's `initWith` also copies `applicationIdSuffix`, which AGP rejects in library modules, so an app that set `applicationIdSuffix` on its profile build type failed to configure when it used a plugin such as shared_preferences. Use `isBuiltAsApp` again to tell app plugins from library plugins. For library plugins, clear `applicationIdSuffix` after `initWith`. Adds a unit test and an integration test that builds against the real AGP. Fixes flutter#196412
`initWith` also copies isShrinkResources, buildConfigFields and resValues. AGP rejects all of them in a library that has not enabled the matching feature, and the isShrinkResources check fails inside `initWith` itself. Library plugins now get only isMinifyEnabled, matchingFallbacks and isDebuggable, which is what Flutter 3.47 copied, plus matchingFallbacks. Extends the integration test to cover these settings and updates the migration docs.
| if (pluginIsApp) { | ||
| initWith(appBuildType) | ||
| } else { | ||
| isMinifyEnabled = appBuildType.isMinifyEnabled | ||
| matchingFallbacks.addAll(appBuildType.matchingFallbacks) | ||
| } | ||
| // AGP uses one build type class for every module type, so this check | ||
| // also passes for library build types. | ||
| if (this is ApplicationBuildType && appBuildType is ApplicationBuildType) { | ||
| isDebuggable = appBuildType.isDebuggable |
There was a problem hiding this comment.
This fix seems odd because why would we have an app check call init with the add build type then later check if this is an ApplicationBuildType and set debuggable.
There is a pattern here we need to check our assumptions on. What needs to be copied and how do we make sure the values are safe for apps and libraries (and are those the only types we need to ensure work).
There was a problem hiding this comment.
You're right. Two things were wrong with that pattern:
- For app plugins, the separate
isDebuggablecopy did nothing. AGP'sinitWithalready copiesisDebuggable(AbstractBuildType.initWith,com/android/builder/core/AbstractBuildType.kt:43in AGP 9.3.1). this is ApplicationBuildTypecan't tell an app from a library. AGP uses oneBuildTypeclass for every module type, and that class implementsApplicationBuildType,LibraryBuildType,DynamicFeatureBuildTypeandTestBuildType.
b08757b splits the code into two paths, chosen by isBuiltAsApp(pluginProject):
- App plugins (
com.android.application) useinitWithand nothing else. An app accepts every build type property. - Library plugins use
copyLibraryBuildTypeProperties, which copies only what a library needs.
What a library plugin needs. The main point of the copy is the build type itself. The app adds a <buildType>Api dependency on every plugin, so each plugin needs a build type with the same name for its variants to resolve. After that, the function copies:
matchingFallbacks, so the plugin's own dependencies resolve. It's public onBuildTypeand valid for libraries.isDebuggable. The public library DSL doesn't have it, but library variants still read it (LibraryVariantDslInfoImpl.isDebuggable), forBuildConfig.DEBUG, R8's debuggable flag and the CMake build type of FFI plugins. It's set through theApplicationBuildTypecast, as in 3.47.
What it doesn't copy.
applicationIdSuffix,isShrinkResources,buildConfigFieldsandresValues: AGP rejects them in a library.isMinifyEnabled: checking this turned up a second bug, fixed in 19b0cee. 3.47 copied it, but a minified library build type makes AGP run R8 on the plugin with only the plugin's keep rules. Astaging { initWith(release) }build then fails in the app's R8 step withMissing class io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin. The app's R8 run already shrinks plugin code.signingConfig,isJniDebuggable,isProfileableand the build typendk/externalNativeBuildoptions: AGP uses them only to package or sign an app. The native options would pass the app's CMake arguments to an FFI plugin.
Module types.
- Plugins are
com.android.application,com.android.libraryorcom.android.kotlin.multiplatform.library. - KMP plugins have no top-level
androidextension, socopyAppBuildTypesToPluginreturns early athasProperty("android"). They have no build types to copy. - In add-to-app,
projectis itself a library. Its build types go through the same library path. - Any other module type would also take the library path, which copies nothing that AGP rejects.
The unit tests model AGP's single build type class and check that library plugins never get initWith or isMinifyEnabled. The integration test (see the other thread) checks the same thing against the real AGP.
There was a problem hiding this comment.
Instead of a new test I think we should modify android_plugin_example_app_build_test.dart
There was a problem hiding this comment.
Done in b08757b and 19b0cee. The coverage now lives in android_plugin_example_app_build_test.dart, for both the plugin and plugin_ffi templates. android_plugin_app_build_type_properties_test.dart and its tool_test_durations.json entries are gone.
Before the first build, the test adds this to the example app's build.gradle.kts: a profile build type with applicationIdSuffix, versionNameSuffix, buildConfigField and resValue, and a staging build type with isMinifyEnabled and isShrinkResources. For the plugin template, it also runs ./gradlew :app:assembleStaging.
Local results:
- Stock master and stock 3.50.0: both cases fail with
Resource shrinker cannot be used for libraries.andcontains custom BuildConfig fields, but the feature is disabled. - With this PR, on master and 3.50.0: both cases pass, in about 80 seconds for the whole file.
- With this PR, but with
isMinifyEnabledcopied again: theplugincase fails withR8: Missing class com.example.plugin_test.PluginTestPlugin.
- App plugins copy each missing build type with initWith only. initWith already copies isDebuggable, so the separate isDebuggable copy was redundant for them. - Library plugins copy isMinifyEnabled, matchingFallbacks and isDebuggable in a dedicated helper that documents why initWith can't be used and why the isDebuggable cast works. - Move the integration coverage into android_plugin_example_app_build_test.dart, which now adds app-only build type settings to the example app for both plugin templates. Delete android_plugin_app_build_type_properties_test.dart and its tool_test_durations.json entries.
A minified library build type makes AGP run R8 on the plugin with only the plugin's own keep rules, which removes the plugin classes that the app's GeneratedPluginRegistrant uses. The app's R8 run then fails with "Missing class". The app's R8 run already shrinks the plugin code, so library plugins don't need the setting. The integration test now also builds the example app's staging variant, which fails with "Missing class" if isMinifyEnabled is copied again.
| isDebuggable = appBuildType.isDebuggable | ||
| if (pluginIsApp) { | ||
| // An app accepts every build type property, so copy all of them. | ||
| // `initWith` copies isDebuggable too. |
There was a problem hiding this comment.
What does debuggable need to be mentioned separately?
There was a problem hiding this comment.
It doesn't. That comment was left over from the old code, which set isDebuggable after initWith. Removed in a4584de, along with the matching comment in the unit test.
| * isMinifyEnabled is deliberately not copied either. A minified library runs R8 with | ||
| * only its own keep rules, which removes the plugin classes that the app's | ||
| * GeneratedPluginRegistrant uses. The app's R8 run already shrinks the plugin code. |
There was a problem hiding this comment.
Collapse this minified prose to something shorter.
There was a problem hiding this comment.
Shortened in a4584de to one sentence each for what is skipped and why.
| * Copies the properties of [appBuildType] that a library needs onto | ||
| * [libraryBuildType], a build type of a library plugin. | ||
| * | ||
| * This deliberately doesn't use `initWith`, which copies every property. AGP rejects | ||
| * some of those properties in a library: applicationIdSuffix and isShrinkResources | ||
| * always, and buildConfigField and resValue unless the library enables those build | ||
| * features. See https://github.com/flutter/flutter/issues/169215, | ||
| * https://github.com/flutter/flutter/issues/196412 and | ||
| * https://github.com/flutter/flutter/issues/196696. | ||
| * | ||
| * isMinifyEnabled is deliberately not copied either. A minified library runs R8 with |
There was a problem hiding this comment.
way too long and no need to mention every bug that is related.
| // The public library DSL has no isDebuggable, but library variants still use it, | ||
| // for example for BuildConfig.DEBUG. AGP implements every build type with one class | ||
| // that also implements ApplicationBuildType, so this check passes for libraries. | ||
| if (libraryBuildType is ApplicationBuildType && appBuildType is ApplicationBuildType) { | ||
| libraryBuildType.isDebuggable = appBuildType.isDebuggable |
There was a problem hiding this comment.
WHy is this the one type that is special?
There was a problem hiding this comment.
isDebuggable is the only property we copy that the library DSL hides. In AGP 9.3.1 (javap on gradle-api-9.3.1.jar), isDebuggable is declared on ApplicationBuildType, TestBuildType and DynamicFeatureBuildType, but not on BuildType or LibraryBuildType. matchingFallbacks is on BuildType, so it needs no cast.
Library variants still read isDebuggable, for BuildConfig.DEBUG, R8's debuggable flag and the CMake build type of FFI plugins. Without the copy, a staging { initWith(debug) } build type would be debuggable in the app and not in the plugins. AGP's single build type class implements ApplicationBuildType, so the cast is the only way to set it on a library build type without internal AGP classes.
a4584de shortens the comment above the cast to say this.
|
|
||
| @Test | ||
| fun `configurePlugins copies app-specific properties when the plugin project is an app`( | ||
| fun `configurePlugins copies every build type property with initWith when the plugin project is an app`( |
There was a problem hiding this comment.
Where is the test that is similar to this one but for a library?
There was a problem hiding this comment.
It is configurePlugins copies only library build type properties when the plugin project is a library. It was further up the file and named differently, so it was hard to find. In a4584de the two tests are next to each other and have matching names. The library test checks that initWith and isMinifyEnabled are not used, and that matchingFallbacks and isDebuggable are copied.
| // Regression test for https://github.com/flutter/flutter/issues/169215, | ||
| // https://github.com/flutter/flutter/issues/196412 and | ||
| // https://github.com/flutter/flutter/issues/196696. |
There was a problem hiding this comment.
Again you dont need to mention every bug that a pr fixes in the tests. Maybe one would make sense here.
| // The Flutter Gradle Plugin copies the app's build types onto library | ||
| // plugins that don't define them, such as this plugin and integration_test. | ||
| // The settings below break a library plugin if they are copied onto it: | ||
| // applicationIdSuffix and isShrinkResources are app-only, the plugins don't | ||
| // enable the buildConfig or resValues build features, and isMinifyEnabled | ||
| // strips the plugin classes. Gradle configures every build type, so any | ||
| // build catches the configuration errors. |
There was a problem hiding this comment.
I dont think this whole explanation makes sense at this point in the test code.
There was a problem hiding this comment.
Cut to two lines in a4584de: what the block adds and why. I also shortened the comment before the assembleStaging build.
There was a problem hiding this comment.
Code Review
This pull request updates PluginHandler.kt to prevent copying incompatible application-specific build type properties onto library plugin projects. When a plugin is not built as an app, only library-compatible properties such as matchingFallbacks and isDebuggable are copied via a new copyLibraryBuildTypeProperties method, avoiding the use of initWith. Unit tests in PluginHandlerTest.kt and integration tests in android_plugin_example_app_build_test.dart have been updated to verify these changes. No review comments were provided for this pull request.
| * Unlike `initWith`, this skips app-only properties, such as applicationIdSuffix, that | ||
| * AGP rejects in a library (https://github.com/flutter/flutter/issues/196412). It also | ||
| * skips isMinifyEnabled, because the app's R8 run already shrinks the plugin code. |
There was a problem hiding this comment.
These are comment for maintainers and belong in // comment in the method not in the kt doc.
There was a problem hiding this comment.
Done in 563a5dd. The KDoc is now one sentence about what the method does. The notes on skipping applicationIdSuffix and isMinifyEnabled are now // comments at the start of the method body.
gmackall
left a comment
There was a problem hiding this comment.
lgtm in the sense that it makes sense for us to land this so our cherry pick more closely mirrors what we had before #191606, in order to lower risk
I'm skeptical that the pluginIsApp case is necessary or correct though - a plugin should not be an app, I think?
@gmackall and i talked out of band and I agree. It appears that this was incidental from 191606 and not intentional. There is other follow up work I want to land related to this but not cherry pick so I have added proving or disproving grays thoughts to that work. |
|
autosubmit label was removed for flutter/flutter/196692, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label. |
…ication-id-suffix
flutter/flutter@d8bd864...2f09f96 2026-11-17 [email protected] Analyze every flutter create template, and fix the package_ffi lint (flutter/flutter#196165) 2026-11-17 [email protected] Roll Dart SDK from 50debf3c9757 to 26e38955d9d4 (3 revisions) (flutter/flutter#196772) 2026-11-17 [email protected] Manual roll ICU from b09986242cfb to d3c612f44146 (1 revision) (flutter/flutter#196713) 2026-11-17 [email protected] Roll Skia from 4f6b3a8d8565 to 503a380ebecc (19 revisions) (flutter/flutter#196768) 2026-11-17 [email protected] iOS: Don't mock UIView animations in keyboard spring animation test (flutter/flutter#196755) 2026-11-17 [email protected] [flutter_tools] Preserve index.html across --web-content-hash transitions (flutter/flutter#196491) 2026-11-17 [email protected] Fix TextStyle background color fade-out when lerping to null (flutter/flutter#196153) 2026-11-17 [email protected] Update desktop engine TESTOWNERS (flutter/flutter#196732) 2026-11-17 [email protected] [flutter_tools] Do not copy app-only build type properties onto library plugins (flutter/flutter#196692) 2026-11-17 [email protected] [flutter_tools] Remove service and runner ambient context getters (flutter/flutter#196571) 2026-11-17 [email protected] Bump customer tests (flutter/flutter#196740) 2026-11-17 [email protected] Take customer_testing_flame out of bringup (flutter/flutter#196570) 2026-11-17 [email protected] [devicelab] Read AssetManifest.bin from the APK in flavors_test (flutter/flutter#196736) 2026-11-17 [email protected] [flutter_tools] Fix flaky tool_tests_general by moving spawnUri tests to integration shard (flutter/flutter#195999) 2026-11-17 [email protected] Roll Dart SDK from 3e069c6fdbde to 50debf3c9757 (2 revisions) (flutter/flutter#196706) 2026-11-16 [email protected] [web] Record build finish synchronously in renderScene and fix dropped frame timings (flutter/flutter#195947) 2026-11-16 [email protected] Do not reveal obscured character when input is rejected by inputFormatters (flutter/flutter#195022) 2026-11-16 [email protected] Cla draft follow up: add waiting for response (flutter/flutter#196686) 2026-11-16 [email protected] Roll pub packages (flutter/flutter#196684) 2026-11-16 [email protected] Roll Packages from 015df97 to 4c6a7d3 (8 revisions) (flutter/flutter#196681) 2026-11-16 [email protected] Skip Vulkan impeller_unittests on macOS (flutter/flutter#196282) 2026-11-16 [email protected] Move the task runners out of Flutter Engine (flutter/flutter#196267) 2026-11-16 [email protected] [FGP] Add Flutter assets after AGP's asset merge so dotfiles are pack… (flutter/flutter#196465) 2026-11-16 [email protected] iOS: Add Obj-C model for platform view composition params (flutter/flutter#196498) 2026-11-16 [email protected] [Windows] Normalize Shift key messages with a non-standard scan code (flutter/flutter#193675) 2026-11-16 [email protected] Roll Skia from 118eaff3deb4 to 4f6b3a8d8565 (3 revisions) (flutter/flutter#196672) 2026-11-16 [email protected] Roll Dart SDK from c91e8b6674e2 to 3e069c6fdbde (4 revisions) (flutter/flutter#196669) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…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).
…o library plugins (#196757) ### Issue Link: #196412 and #196696 Companion stable cherry-pick: #196756 ### 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.51-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).
Fixes #196412
Fixes #196696
Part of #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
applicationIdSuffixonprofile(#196412),isShrinkResources(#196696) on a custom build type, or abuildConfigFieldorresValue. Example error:This is a regression of #169215. #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,initWithcopies every build type property (AbstractBuildType.initWith,BaseConfigImpl._initWith,BuildType._initWith), including several that AGP rejects in a library:applicationIdSuffixLibrary projects cannot set applicationIdSuffixisShrinkResourcesResource shrinker cannot be used for libraries.The check runs insideinitWith, so the value can't be reset afterwards.buildConfigFieldscontains custom BuildConfig fields, but the feature is disabledresValuescontains custom resource values, but the feature is disabledThe
this is ApplicationBuildTypecheck 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 implementsApplicationBuildType,LibraryBuildType,DynamicFeatureBuildTypeandTestBuildType.This PR:
initWithonly for plugins that are built as apps (isBuiltAsApp, as fix: only use library props for libraries #172704 did). An app accepts every build type property, andinitWithalready copiesisDebuggable, so app plugins no longer set it separately.matchingFallbacksandisDebuggable, in a separatecopyLibraryBuildTypePropertiesfunction:matchingFallbackslets the plugin's own dependencies resolve. [AGP 9.1.0 Migration #4] Replace plugin build-type copy with initWith on public DSL #191606 meant to add it.isDebuggableisn't in the public library DSL, but library variants still read it, for example forBuildConfig.DEBUG, R8's debuggable flag and the CMake build type of FFI plugins. It is set through theApplicationBuildTypeinterface that AGP's build type class implements, as in Flutter 3.47.isMinifyEnabledis 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 thatGeneratedPluginRegistrantuses, and the app's R8 run fails withMissing class io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin. The app's R8 run already shrinks plugin code.initWithisn't called and thatisMinifyEnabledisn't set.android_plugin_example_app_build_test.dart, which builds the example app of thepluginandplugin_ffitemplates against the real AGP. Before the first build, the test now addsapplicationIdSuffix,versionNameSuffix,buildConfigFieldandresValueto theprofilebuild type, and adds astagingbuild type withisMinifyEnabledandisShrinkResources. For theplugintemplate, it also runs./gradlew :app:assembleStaging. The earlier tests were mock-only and checked thatinitWithwas called, so they could not catch this.Verified locally on 3.50.0 and master:
applicationIdSuffixin the profile mode (Flutter 3.50) #196412 fails, and so do apps with onlyisShrinkResourcesor onlybuildConfigField. 3.47.9 builds them.applicationIdSuffixin the profile mode (Flutter 3.50) #196412 repro builds with--debugand--profile.aapt2 dump badgingshows packagecom.example.repro350.profileand versionName1.0.0-PROFILEfor the profile APK. An app with astagingbuild type that usesinitWith(release),isMinifyEnabledandisShrinkResourcesbuilds with./gradlew :app:assembleStaging.android_plugin_example_app_build_test.dartfail withResource shrinker cannot be used for libraries.andcontains custom BuildConfig fields, but the feature is disabled. With this change, both cases pass on master and 3.50.0. IfisMinifyEnabledis copied again, theplugincase fails withR8: Missing class com.example.plugin_test.PluginTestPlugin.dart analyzeare clean.DependencyVersionChecker.kt, with Gradle 9.3.1). On AGP 9.1.1, both cases ofandroid_plugin_example_app_build_test.dartpass with this change, covering the release APK,:app:assembleStagingand 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
///).