Skip to content

Conversation

@hm21
Copy link
Contributor

@hm21 hm21 commented Oct 2, 2025

Description

This PR introduces platform-specific asset support in pubspec.yaml.

Currently, Flutter does not allow specifying which platforms an asset should be included for.
This results in all declared assets being bundled for every target platform, even if some are irrelevant (e.g. desktop-only or mobile-only images).

What this PR changes

  • Adds a new optional platforms field under each asset in pubspec.yaml.
  • The field accepts a list of strings (platform identifiers, e.g. ["android", "ios", "web", "windows", "macos", "linux"]).
  • Assets with a platforms restriction are only included in the bundle when building for a matching platform.
  • Invalid values (non-strings or unknown platform names) log an error.

Example

flutter:
  assets:
    - path: assets/logo.png
   
    - path: assets/web_worker.js
      platforms: [web]

    - path: assets/desktop_icon.png
      platforms: [windows, linux, macos]

Before

All assets (logo.png, web_worker.js, desktop_icon.png) are bundled into every build, regardless of platform.

After

  • logo.png is included on all platforms.
  • web_worker.js is included only on web builds.
  • desktop_icon.png is included only on desktop builds.

Why this is useful

This significantly improves bundle size, prevents unused resources from being shipped, and gives developers better control over asset management.

Issues

Fixes #65065

Reviewer note

Would a design document be helpful for this change, or is the current explanation sufficient?

Pre-launch Checklist

Copilot AI review requested due to automatic review settings October 2, 2025 08:01
@github-actions github-actions bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Oct 2, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds platform-specific asset filtering support to Flutter by introducing a new platforms field in pubspec.yaml asset entries. This allows developers to specify which platforms should include specific assets, reducing bundle sizes by excluding irrelevant assets from builds.

  • Adds platforms field to AssetsEntry class with validation for supported platform names
  • Implements platform filtering logic in the asset bundling process to skip assets not matching the target platform
  • Updates asset bundle entry constructors to include the new platforms parameter

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/flutter_tools/lib/src/flutter_manifest.dart Adds platforms field parsing and validation to AssetsEntry
packages/flutter_tools/lib/src/asset.dart Updates AssetBundleEntry and _Asset classes to support platforms field
packages/flutter_tools/lib/src/build_system/targets/assets.dart Implements platform filtering logic during asset copying
packages/flutter_tools/lib/src/base/deferred_component.dart Adds platforms display to deferred component output
packages/flutter_tools/lib/src/widget_preview/preview_pubspec_builder.dart Preserves platforms field when transforming assets
packages/flutter_tools/test/general.shard/flutter_manifest_test.dart Adds comprehensive tests for platforms field parsing and validation
packages/flutter_tools/test/general.shard/devfs_test.dart Updates test asset bundle entries to include platforms parameter
packages/flutter_tools/test/general.shard/bundle_builder_test.dart Updates test asset bundle entries to include platforms parameter

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable feature for platform-specific asset filtering in pubspec.yaml, which can significantly improve bundle sizes. The implementation correctly propagates the new platforms field through the asset pipeline, from parsing in flutter_manifest.dart to the filtering logic in build_system/targets/assets.dart. The changes include necessary updates to data classes, equality checks, and hashing, and are accompanied by relevant tests. I have one suggestion to improve the efficiency and clarity of the platform validation logic.

@hm21 hm21 force-pushed the master branch 2 times, most recently from 4c3f52d to 414d66b Compare October 22, 2025 09:40
@Piinks Piinks added the team-tool Owned by Flutter Tool team label Oct 27, 2025
@bkonyi bkonyi requested review from bkonyi and chingjun November 3, 2025 15:15
Copy link
Contributor

@bkonyi bkonyi left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

Overall, this LGTM with some minor comments that should be addressed.

@github-actions github-actions bot removed the team-tool Owned by Flutter Tool team label Nov 5, 2025
@hm21
Copy link
Contributor Author

hm21 commented Nov 5, 2025

Ready for re-review.

FYI, the Linux tests are currently failing, but currently I don't think my changes are related. I quickly checked a few other new PRs and saw that the Linux tests failed for them as well. However, please let me know if you think my changes are the problem. I will look into it further.

@hm21 hm21 requested a review from chingjun November 5, 2025 14:11
@bkonyi
Copy link
Contributor

bkonyi commented Nov 5, 2025

Looks good to me! Thanks @hm21!

I think we're experiencing some infrastructure issues, so I don't think any of those Linux failures are the result of your change.

Copy link
Contributor

@chingjun chingjun left a comment

Choose a reason for hiding this comment

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

Thank you for the fixes, this now looks a lot cleaner!

There is still an open question about what assets should be included in tests, but even with that unanswered I would still be ok landing this PR, and figure that out in the future.


The following are fully optional. It is out of scope for this PR, and is is fully optional whether you want to fix it in this PR, in a separate PR, or leave it for now.

The PR led me to think whether targetPlatform should still be an optional parameter in assetBundle.build(). Perhaps we should make it a required parameter.

Again, fully optional.

@hm21
Copy link
Contributor Author

hm21 commented Nov 6, 2025

Okay targetPlatform is now required, so it’s ready for review. I think the PR is complete now, but if you have any other suggestions or ideas to extend it before merging, let me know. Thanks again for your reviews.

Copy link
Contributor

@chingjun chingjun left a comment

Choose a reason for hiding this comment

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

LGTM!

One more minor nit, and you probably want to rebase your branch as it's now 200+ commits behind HEAD.

Thank you again for your contribution, and specifically for adding the changes to make the code base more robust. Thank you!

@fluttergithubbot
Copy link
Contributor

An existing Git SHA, 414d66bf4f353eea6e98e0c0d70a2db9bea9b9fc, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

- Introduce `platforms` key in `flutter.assets` entries in pubspec.yaml
  to restrict assets to specific build targets (e.g. windows, android, ios, web).
- Update asset loading logic in `asset.dart` and `targets/assets.dart`
  to skip assets not matching the current `TargetPlatform`.
- Extend `flutter_manifest.dart` to parse and validate `platforms`.
- Update preview pubspec builder and deferred component handling
  to be aware of platform-specific assets.
- Add tests for manifest parsing, invalid platform values,
  and bundle builder behavior with platform-restricted assets.

Example:
```yaml
flutter:
  assets:
    - path: assets/icon.png
      platforms: [android, ios]
```
@fluttergithubbot
Copy link
Contributor

An existing Git SHA, 414d66bf4f353eea6e98e0c0d70a2db9bea9b9fc, was detected, and no actions were taken.

To re-trigger presubmits after closing or re-opeing a PR, or pushing a HEAD commit (i.e. with --force) that already was pushed before, push a blank commit (git commit --allow-empty -m "Trigger Build") or rebase to continue.

@chingjun
Copy link
Contributor

I just fixed the Google Testing for this PR. Good to be submitted now. @bkonyi do you want to take a final look at it before adding the autosubmit tag?

@bkonyi
Copy link
Contributor

bkonyi commented Nov 11, 2025

I just fixed the Google Testing for this PR. Good to be submitted now. @bkonyi do you want to take a final look at it before adding the autosubmit tag?

Thanks! I was going to take a look at that today :) I've approved the G3Fix and this change LGTM.

We should look into adding this feature to our documentation (filed #178294).

Thanks for your contribution @hm21!

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 11, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 12, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 17, 2025
IvoneDjaja pushed a commit to IvoneDjaja/flutter that referenced this pull request Nov 22, 2025
…lutter#176393)

## Description

This PR introduces platform-specific asset support in `pubspec.yaml`.

Currently, Flutter does not allow specifying which platforms an asset
should be included for.
This results in all declared assets being bundled for every target
platform, even if some are irrelevant (e.g. desktop-only or mobile-only
images).

### What this PR changes
- Adds a new optional `platforms` field under each asset in
`pubspec.yaml`.
- The field accepts a list of strings (platform identifiers, e.g.
`["android", "ios", "web", "windows", "macos", "linux"]`).
- Assets with a `platforms` restriction are only included in the bundle
when building for a matching platform.
- Invalid values (non-strings or unknown platform names) log an error.

### Example
```yaml
flutter:
  assets:
    - path: assets/logo.png
   
    - path: assets/web_worker.js
      platforms: [web]

    - path: assets/desktop_icon.png
      platforms: [windows, linux, macos]
```

#### Before
All assets (`logo.png`, `web_worker.js`, `desktop_icon.png`) are bundled
into **every build**, regardless of platform.

#### After
- `logo.png` is included on all platforms.
- `web_worker.js` is included only on web builds.
- `desktop_icon.png` is included only on desktop builds.

### Why this is useful
This significantly improves bundle size, prevents unused resources from
being shipped, and gives developers better control over asset
management.

## Issues
Fixes flutter#65065

## Reviewer note
Would a design document be helpful for this change, or is the current
explanation sufficient?

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [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 documentation (doc comments with `///`).
- [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
[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
[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
mboetger pushed a commit to mboetger/flutter that referenced this pull request Dec 2, 2025
…lutter#176393)

## Description

This PR introduces platform-specific asset support in `pubspec.yaml`.

Currently, Flutter does not allow specifying which platforms an asset
should be included for.
This results in all declared assets being bundled for every target
platform, even if some are irrelevant (e.g. desktop-only or mobile-only
images).

### What this PR changes
- Adds a new optional `platforms` field under each asset in
`pubspec.yaml`.
- The field accepts a list of strings (platform identifiers, e.g.
`["android", "ios", "web", "windows", "macos", "linux"]`).
- Assets with a `platforms` restriction are only included in the bundle
when building for a matching platform.
- Invalid values (non-strings or unknown platform names) log an error.

### Example
```yaml
flutter:
  assets:
    - path: assets/logo.png
   
    - path: assets/web_worker.js
      platforms: [web]

    - path: assets/desktop_icon.png
      platforms: [windows, linux, macos]
```

#### Before
All assets (`logo.png`, `web_worker.js`, `desktop_icon.png`) are bundled
into **every build**, regardless of platform.

#### After
- `logo.png` is included on all platforms.
- `web_worker.js` is included only on web builds.
- `desktop_icon.png` is included only on desktop builds.

### Why this is useful
This significantly improves bundle size, prevents unused resources from
being shipped, and gives developers better control over asset
management.

## Issues
Fixes flutter#65065

## Reviewer note
Would a design document be helpful for this change, or is the current
explanation sufficient?

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [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 documentation (doc comments with `///`).
- [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
[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
[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
reidbaker pushed a commit to AbdeMohlbi/flutter that referenced this pull request Dec 10, 2025
…lutter#176393)

## Description

This PR introduces platform-specific asset support in `pubspec.yaml`.

Currently, Flutter does not allow specifying which platforms an asset
should be included for.
This results in all declared assets being bundled for every target
platform, even if some are irrelevant (e.g. desktop-only or mobile-only
images).

### What this PR changes
- Adds a new optional `platforms` field under each asset in
`pubspec.yaml`.
- The field accepts a list of strings (platform identifiers, e.g.
`["android", "ios", "web", "windows", "macos", "linux"]`).
- Assets with a `platforms` restriction are only included in the bundle
when building for a matching platform.
- Invalid values (non-strings or unknown platform names) log an error.

### Example
```yaml
flutter:
  assets:
    - path: assets/logo.png
   
    - path: assets/web_worker.js
      platforms: [web]

    - path: assets/desktop_icon.png
      platforms: [windows, linux, macos]
```

#### Before
All assets (`logo.png`, `web_worker.js`, `desktop_icon.png`) are bundled
into **every build**, regardless of platform.

#### After
- `logo.png` is included on all platforms.
- `web_worker.js` is included only on web builds.
- `desktop_icon.png` is included only on desktop builds.

### Why this is useful
This significantly improves bundle size, prevents unused resources from
being shipped, and gives developers better control over asset
management.

## Issues
Fixes flutter#65065

## Reviewer note
Would a design document be helpful for this change, or is the current
explanation sufficient?

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [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 documentation (doc comments with `///`).
- [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
[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
[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bundling assets only on a specific platform (and remove assets on another platform)

5 participants