Repository navigation
[beta] [FGP] Add Flutter assets after AGP's asset merge so dotfiles are packaged - #196747
Conversation
flutter#196465) Since flutter#192488, the Flutter Gradle plugin registered flutter_assets/ as a generated assets source directory. AGP filters assets source directories with androidResources.ignoreAssetsPattern, whose default drops files whose name starts with "." (such as .env) and directories whose name starts with "_". Those assets were missing from the APK and failed to load at runtime. CopyFlutterAssetsTask now transforms SingleArtifact.ASSETS instead: its output is AGP's merged assets with flutter_assets/ copied over them, so Flutter assets bypass the filter. This restores the pre-3.50 behavior, and matches web and macOS, which ship these files without filtering. The same wiring applies to library variants, so `flutter build aar` outputs include these files again. The task copies with a Gradle sync spec, so Gradle's default excludes (.gitignore, .DS_Store, ._*, *~, ...) apply, as they did with the `Copy` task before flutter#192488. AGP's compress step drops those files from the APK either way. The default excludes don't cover `.env` or `_`-prefixed directories. Fixes flutter#196444 ## Behavior changes (for the release notes) - A custom `ignoreAssetsPattern` no longer applies to Flutter assets. It still applies to the app's own `src/main/assets`, and AGP's default filtering of those is unchanged. - When a file exists both in `flutter_assets/` and in the app's `src/main/assets/flutter_assets/`, Flutter's copy wins again, as in 3.47. ## Not covered by this PR - **Add-to-app, module included from source:** the host app's own asset merge still drops `.env` and `_private/` with AGP's default pattern. The Flutter Gradle plugin isn't applied to the host, so it can't change this. This isn't a 3.50 regression: it happens on master, with this PR, and at 557f863 (the parent of flutter#192488). Workaround: set a custom `ignoreAssetsPattern` in the host. AARs from `flutter build aar` aren't affected. - **Deferred components:** feature modules register assets with `srcDirs +=` (`templates/module/android/deferred_component/build.gradle.tmpl`), so they drop these files on every version. I'll follow up on master separately. ## 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. --------- Co-authored-by: Reid-Agent <[email protected]>
|
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. |
flavors_test read AssetManifest.bin from AGP's merge<Variant>Assets intermediate directory. Since flutter#196465, the Flutter Gradle Plugin adds its assets with a transform of SingleArtifact.ASSETS, so that directory no longer holds flutter_assets and the test failed with PathNotFoundException. Read the manifest from the built APK instead, which does not depend on AGP intermediates. Fixes flutter#196735 (cherry picked from commit 9ea75ec)
package:archive 3.x types ArchiveFile.content as dynamic, and a decompressed entry is only guaranteed to be a List<int>. (cherry picked from commit 7220d98)
There was a problem hiding this comment.
Code Review
This pull request updates the Flutter Gradle Plugin to add Flutter assets as a transform of the merged assets (SingleArtifact.ASSETS) instead of registering them as a generated assets source directory. This change prevents the Android Gradle Plugin (AGP) from filtering out valid Flutter assets (such as .env or directories starting with an underscore) via its default ignoreAssetsPattern. The CopyFlutterAssetsTask is updated to copy files from the merged assets directory and overlay the Flutter assets, ensuring Flutter assets take precedence on collision. Unit and integration tests, as well as documentation, have been updated to reflect this new asset-merging strategy. I have no additional feedback to provide.
2e0c376
into
flutter:flutter-3.51-candidate.0
… are packaged (#196721) ### Issue Link: #196444 original issue and #196735 post submit failure that turned the tree red Companion beta cherry-pick: #196747 ### Impact Description: [#192488](#192488), part of the migration to AGP's public Variant API, changed how the Flutter Gradle Plugin adds Flutter assets to an Android build. It shipped in 3.50.0 and in the current beta, 3.51.0-0.1.pre. Up to 3.47, a Gradle `Copy` task copied Flutter assets into the merged assets with Gradle's default excludes. Since #192488, they are registered as a generated assets source directory (`variant.sources.assets.addGeneratedSourceDirectory`). AGP filters asset source directories with its `ignoreAssetsPattern`, which has a different default from Gradle's. That pattern drops files whose names start with `.` (such as `.env`) and folders whose names start with `_` (such as `_private/`). On 3.50.0 and 3.51.0-0.1.pre, Android builds silently leave out assets declared in `pubspec.yaml` that load on iOS, on web, and in 3.47. At runtime, `rootBundle.load` fails with "Unable to load asset". This affects production apps that ship a `.env` file, for example with `flutter_dotenv`. The build gives no warning, so developers find out only when the app runs. This cherry-pick, [#196465](#196465), adds Flutter's assets as a transform of AGP's merged assets (`SingleArtifact.ASSETS`) after AGP's filtering, using a Gradle copy spec. That restores the set of files that 3.47 packaged while staying on the public Variant API. ### Changelog Description: [flutter/196444] When building for Android with Flutter 3.50.0, assets whose names start with a dot (such as `.env`) or an underscore folder were silently left out of the APK or AAB which caused a runtime failure. ### Workaround: Rename the affected assets and folders so they don't start with `.` or `_`, or stay on Flutter 3.47. ### 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`, add `assets/.env` and `assets/_private/file.txt` to `pubspec.yaml`, and load them with `rootBundle`. 2. Run `flutter build apk`, then `unzip -l build/app/outputs/flutter-apk/app-release.apk | grep flutter_assets`. Both files are listed. 3. The integration test `android_gradle_asset_merging_test.dart` and the `CopyFlutterAssetsTaskTest` unit tests cover dotfiles, Gradle default excludes, stale-file removal, and collisions. ### Cherry-pick conflict resolution: `FlutterPlugin.kt` and `FlutterPluginTest.kt` conflicted. The 3.50 branch had a guard that threw a `GradleException` when AGP did not expose `variant.sources.assets`, plus a unit test for it. #196465 replaces that code path with a transform on `SingleArtifact.ASSETS`, so `sources.assets` is no longer used. The resolution takes #196465's version, which removes the guard and its test, matching master. The Flutter Gradle Plugin unit tests pass on this branch. ### Bundled test fix: This CP also includes the devicelab `flavors_test` fix from [#196736](#196736) (`2b91459d5fe`), which fixes [#196735](#196735). `flavors_test` read `AssetManifest.bin` from AGP's `merge<Variant>Assets` intermediate folder, which no longer contains Flutter's assets after #196465 moves them to a `SingleArtifact.ASSETS` transform. The app still builds and installs; the test now reads `AssetManifest.bin` from the built APK. Without this fix, `Linux_pixel_7pro flavors_test` and `Windows_mokey flavors_test_win` (`presubmit: false` in `.ci.yaml`) would fail in `flutter-3.50-candidate.0` postsubmit.
… are packaged (#196721) ### Issue Link: flutter/flutter#196444 original issue and flutter/flutter#196735 post submit failure that turned the tree red Companion beta cherry-pick: flutter/flutter#196747 ### Impact Description: [#192488](flutter/flutter#192488), part of the migration to AGP's public Variant API, changed how the Flutter Gradle Plugin adds Flutter assets to an Android build. It shipped in 3.50.0 and in the current beta, 3.51.0-0.1.pre. Up to 3.47, a Gradle `Copy` task copied Flutter assets into the merged assets with Gradle's default excludes. Since #192488, they are registered as a generated assets source directory (`variant.sources.assets.addGeneratedSourceDirectory`). AGP filters asset source directories with its `ignoreAssetsPattern`, which has a different default from Gradle's. That pattern drops files whose names start with `.` (such as `.env`) and folders whose names start with `_` (such as `_private/`). On 3.50.0 and 3.51.0-0.1.pre, Android builds silently leave out assets declared in `pubspec.yaml` that load on iOS, on web, and in 3.47. At runtime, `rootBundle.load` fails with "Unable to load asset". This affects production apps that ship a `.env` file, for example with `flutter_dotenv`. The build gives no warning, so developers find out only when the app runs. This cherry-pick, [#196465](flutter/flutter#196465), adds Flutter's assets as a transform of AGP's merged assets (`SingleArtifact.ASSETS`) after AGP's filtering, using a Gradle copy spec. That restores the set of files that 3.47 packaged while staying on the public Variant API. ### Changelog Description: [flutter/196444] When building for Android with Flutter 3.50.0, assets whose names start with a dot (such as `.env`) or an underscore folder were silently left out of the APK or AAB which caused a runtime failure. ### Workaround: Rename the affected assets and folders so they don't start with `.` or `_`, or stay on Flutter 3.47. ### 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`, add `assets/.env` and `assets/_private/file.txt` to `pubspec.yaml`, and load them with `rootBundle`. 2. Run `flutter build apk`, then `unzip -l build/app/outputs/flutter-apk/app-release.apk | grep flutter_assets`. Both files are listed. 3. The integration test `android_gradle_asset_merging_test.dart` and the `CopyFlutterAssetsTaskTest` unit tests cover dotfiles, Gradle default excludes, stale-file removal, and collisions. ### Cherry-pick conflict resolution: `FlutterPlugin.kt` and `FlutterPluginTest.kt` conflicted. The 3.50 branch had a guard that threw a `GradleException` when AGP did not expose `variant.sources.assets`, plus a unit test for it. #196465 replaces that code path with a transform on `SingleArtifact.ASSETS`, so `sources.assets` is no longer used. The resolution takes #196465's version, which removes the guard and its test, matching master. The Flutter Gradle Plugin unit tests pass on this branch. ### Bundled test fix: This CP also includes the devicelab `flavors_test` fix from [#196736](flutter/flutter#196736) (`2b91459d5fe`), which fixes [#196735](flutter/flutter#196735). `flavors_test` read `AssetManifest.bin` from AGP's `merge<Variant>Assets` intermediate folder, which no longer contains Flutter's assets after #196465 moves them to a `SingleArtifact.ASSETS` transform. The app still builds and installs; the test now reads `AssetManifest.bin` from the built APK. Without this fix, `Linux_pixel_7pro flavors_test` and `Windows_mokey flavors_test_win` (`presubmit: false` in `.ci.yaml`) would fail in `flutter-3.50-candidate.0` postsubmit.
Issue Link:
#196444
Bundled test fix for #196735 (postsubmit failure; see below).
Companion stable cherry-pick: #196721
Impact Description:
#192488, part of the migration to AGP's public Variant API, changed how the Flutter Gradle Plugin adds Flutter assets to an Android build. It shipped in 3.50.0 and in the current beta, 3.51.0-0.1.pre. Up to 3.47, a Gradle
Copytask copied Flutter assets into the merged assets with Gradle's default excludes. Since #192488, they are registered as a generated assets source directory (variant.sources.assets.addGeneratedSourceDirectory).AGP filters asset source directories with its
ignoreAssetsPattern, which has a different default from Gradle's. That pattern drops files whose names start with.(such as.env) and folders whose names start with_(such as_private/). On 3.50.0 and 3.51.0-0.1.pre, Android builds silently leave out assets declared inpubspec.yamlthat load on iOS, on web, and in 3.47. At runtime,rootBundle.loadfails with "Unable to load asset". This affects production apps that ship a.envfile, for example withflutter_dotenv. The build gives no warning, so developers find out only when the app runs.This cherry-pick, #196465, adds Flutter's assets as a transform of AGP's merged assets (
SingleArtifact.ASSETS) after AGP's filtering, using a Gradle copy spec. That restores the set of files that 3.47 packaged while staying on the public Variant API.Changelog Description:
[flutter/196444] When building for Android, assets declared in
pubspec.yamlwhose names start with.(such as.env), or inside folders whose names start with_, are silently omitted from the APK and fail to load at runtime.Workaround:
Rename the affected assets and folders so they don't start with
.or_, or stay on Flutter 3.47.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, addassets/.envandassets/_private/file.txttopubspec.yaml, and load them withrootBundle.flutter build apk, thenunzip -l build/app/outputs/flutter-apk/app-release.apk | grep flutter_assets. Both files are listed.android_gradle_asset_merging_test.dartand theCopyFlutterAssetsTaskTestunit tests cover dotfiles, Gradle default excludes, stale-file removal, and collisions.Bundled test fix:
This CP also includes the devicelab
flavors_testfix from #196736 (2b91459d5fe), which fixes #196735.flavors_testreadAssetManifest.binfrom AGP'smerge<Variant>Assetsintermediate folder, which no longer contains Flutter's assets after #196465 moves them to aSingleArtifact.ASSETStransform. The app still builds and installs; the test now readsAssetManifest.binfrom the built APK. Without this fix,Linux_pixel_7pro flavors_testandWindows_mokey flavors_test_win(presubmit: falsein.ci.yaml) would fail influtter-3.51-candidate.0postsubmit.