Skip to content

Flavor-conditional asset bundling does not work when declared within a direct main path dependencies #155755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alestiago opened this issue Sep 26, 2024 · 8 comments · Fixed by #156687 or #157306
Assignees
Labels
a: assets Packaging, accessing, or using assets found in release: 3.24 Found to occur in 3.24 found in release: 3.26 Found to occur in 3.26 has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list r: fixed Issue is closed as already fixed in a newer version team-tool Owned by Flutter Tool team tool Affects the "flutter" command-line tool. See also t: labels. triaged-tool Triaged by Flutter Tool team

Comments

@alestiago
Copy link
Contributor

alestiago commented Sep 26, 2024

Description

Flutter 3.22 introduced “Flavor-conditional asset bundling”, read more about them in the “Conditionally bundling assets based on flavor” documentation.

When declaring assets outside of the main application package and defining flavors for them the main application bundle does not account for these assets as expected.

Steps to reproduce

  1. Create a new flutter application for Android:
flutter create my_app --platforms android
  1. Create a new flutter package:
flutter create my_app/packages/my_assets --template package
  1. Add “package:my_assets” as a direct main dependency on “package:my_app”:
flutter pub add 'my_assets:{"path":"my_app/packages/my_assets"}' --directory my_app
  1. Create new assets directories in “package:my_app” and “package:my_assets”:
mkdir my_app/assets
mkdir my_app/packages/my_assets/assets
  1. Save new assets on each assets directory, such that:
my_app/
├─ assets/
│  ├─ my_app_logo.png
│  ├─ my_app_logo-dev.png
│  ├─ my_app_logo-prod.png
├─ packages/
│  ├─ my_assets/
│  │  ├─ assets/
│  │  │  ├─ my_assets_logo-dev.png
│  │  │  ├─ my_assets_logo-prod.png
│  │  │  ├─ my_assets_logo.png
  1. Declare the assets as flavored assets in “package:my_app”:
# file: my_app/pubspec.yaml

# ...

flutter:
 assets:
   - assets/my_app_logo.png
   - path: assets/my_app_logo-dev.png
     flavors:
       - dev
   - path: assets/my_app_logo-prod.png
     flavors:
       - prod
  1. Declare the assets as flavored assets in “package:my_assets”:
# file: my_app/packages/my_assets/pubspec.yaml

# ...

flutter:
 assets:
   - assets/my_assets_logo.png
   - path: assets/my_assets_logo-dev.png
     flavors:
       - dev
   - path: assets/my_assets_logo-prod.png
     flavors:
       - prod
  1. Define “dev” and “prod” Android flavors in “my_app/android/app/build.gradle”:
android {
   // ...

   flavorDimensions "default"

   productFlavors {
       dev {
           dimension "default"
           resValue "string", "app_name", "dev flavor example"
           applicationIdSuffix ".dev"
       }
       prod {
           dimension "default"
           resValue "string", "app_name", "prod flavor example"
           applicationIdSuffix ".prod"
       }
   }
}
  1. Bundle the main application as an APK:
flutter build apk --flavor dev
  1. Analyze the generated APK using Android Studio’s APK Analyzer.

The built APK is expected to be at: my_app/build/app/outputs/flutter-apk/app-dev-release.apk

Expected results

my_assets_logo-dev.png should have also been bundled in the APK but it was not.

Actual results

The following assets were included in the APK:

  • my_app_logo.png
  • my_assets_logo.png

Code sample

Not applicable, see reproductive steps instead.

Screenshots or Video

Actual APK Analyzer results apk-analyzer

Logs

Not applicable.

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.7 23H124 darwin-arm64, locale en-US)
    • Flutter version 3.24.3 on channel stable at /Users/alestiago-nbcu/Developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (2 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/alestiago-nbcu/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] VS Code (version 1.93.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.96.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.7 23H124 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.7 23H124 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 129.0.6668.60

[✓] Network resources
    • All expected network resources are available.

• No issues found!
@huycozy huycozy added the in triage Presently being triaged by the triage team label Sep 27, 2024
@huycozy
Copy link
Member

huycozy commented Sep 27, 2024

Hi @alestiago
I think the problem comes from assets path. Per the example on the docs, you should create separate directories for each flavors, for eg:

flutter:
 assets:
   - assets/common/
   - path: assets/dev/
     flavors:
       - dev
   - path: assets/prod/
     flavors:
       - prod
├── assets
│   ├── common
│   │   └── flutter.jpeg
│   ├── dev
│   │   └── flutter.jpeg
│   └── prod
│       └── flutter.jpeg

And similar assets declaration in the package. Can you try to see if it works?

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 27, 2024
@alestiago
Copy link
Contributor Author

alestiago commented Sep 27, 2024

Hi @huycozy, thanks for chiming in.

Your suggestion is invalid, it does not bundle the flavored assets when defined in a dependency package, see below details for additional information.

Additional information

For clarity, this was the new directory structure:

my_app/
├─ assets/
│  ├─ my_app_logo.png
│  ├─ my_app_logo-dev.png
│  ├─ my_app_logo-prod.png
├─ packages/
│  ├─ my_assets/
│  │  ├─ assets/
│  │  │  ├─ my_assets_logo.png
│  │  │  ├─ dev/
│  │  │  │  ├─ my_assets_logo-dev.png
│  │  │  ├─ prod/
│  │  │  │  ├─ my_assets_logo-prod.png

This was the new pubspec within the package:my_assets:

# file: my_app/packages/my_assets/pubspec.yaml

flutter:
  assets:
    - assets/my_assets_logo.png
    - path: assets/dev/
      flavors:
        - dev
    - path: assets/prod/
      flavors:
        - prod

And this was the examined APK after being built with:

flutter build apk --flavor dev
Screenshot 2024-09-27 at 09 15 36

NOTE: Regardless if it can only accept directories in dependencies or not (which also failed) what has been described in the issue is clearly showing inconsistent behavior in pubspecs when using flavor-conditional bundling. The same syntax was used for the main application package and the direct main dependency package, and the conditional bundling only worked as expected for the main application package.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 27, 2024
@huycozy
Copy link
Member

huycozy commented Sep 30, 2024

Thanks for your response. I can also see the issue with my assets structure. When building apk with flavor, conditionally bundling does not work on package.

My assets structure
.
├── assets
│   ├── common
│   │   └── flutter.jpeg
│   ├── dev
│   │   └── flutter-dev.jpeg
│   └── prod
│       └── flutter-prod.jpeg
├── packages
│   └── my_assets
│       ├── assets
│       │   ├── common
│       │   │   └── flutter.jpeg
│       │   ├── dev
│       │   │   └── flutter-dev.jpeg
│       │   └── prod
│       │       └── flutter-prod.jpeg

Output APK analysis

Screenshot 2024-09-30 at 12 41 00

The complete sample project: flutter-flavor-conditionally-bundling-assets. Reproduced on Flutter master 3.26.0-1.0.pre.263.

@huycozy huycozy added platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels. a: assets Packaging, accessing, or using assets has reproducible steps The issue has been confirmed reproducible and is ready to work on team-tool Owned by Flutter Tool team found in release: 3.24 Found to occur in 3.24 found in release: 3.26 Found to occur in 3.26 and removed in triage Presently being triaged by the triage team labels Sep 30, 2024
@bkonyi
Copy link
Contributor

bkonyi commented Oct 1, 2024

FYI @andrewkolos

@bkonyi bkonyi added P2 Important issues not at the top of the work list triaged-tool Triaged by Flutter Tool team labels Oct 1, 2024
@andrewkolos andrewkolos self-assigned this Oct 1, 2024
@feduke-nukem
Copy link

I would like to point out that this issue is not specific only to Android, as it also arises on iOS, according to #155404.

@huycozy
Copy link
Member

huycozy commented Oct 3, 2024

Thank @feduke-nukem. Taking off Android label as this concerns a tool issue in general.

@huycozy huycozy removed the platform-android Android applications specifically label Oct 3, 2024
auto-submit bot pushed a commit that referenced this issue Oct 15, 2024
Fixes #155755

When building the asset bundle during, the `--flavor` option isn't considered when searching for assets from dependencies. This PR fixes that.

It's possible that when initially implementing this feature, I thought that flavor-conditional assets didn't make sense for packages. While I still think that way regarding pub packages, using this feature makes a lot more sense for monorepo projects.

<details>

<summary> Pre-launch checklist </summary> 

</details>
@huycozy huycozy added the r: fixed Issue is closed as already fixed in a newer version label Oct 16, 2024
andrewkolos added a commit to andrewkolos/flutter that referenced this issue Oct 21, 2024
…156687)

Fixes flutter#155755

When building the asset bundle during, the `--flavor` option isn't considered when searching for assets from dependencies. This PR fixes that.

It's possible that when initially implementing this feature, I thought that flavor-conditional assets didn't make sense for packages. While I still think that way regarding pub packages, using this feature makes a lot more sense for monorepo projects.

<details>

<summary> Pre-launch checklist </summary> 

</details>
@andrewkolos
Copy link
Contributor

This fix should be available in the next Flutter release (3.27 as far as I'm aware), starting with the beta branch.

auto-submit bot pushed a commit that referenced this issue Nov 5, 2024
…#157306)

Fixes #155755 on the beta branch so that this will be available with the 3.27 release.

<details>

<summary> Pre-launch checklist </summary> 

</details>
Copy link

github-actions bot commented Nov 6, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: assets Packaging, accessing, or using assets found in release: 3.24 Found to occur in 3.24 found in release: 3.26 Found to occur in 3.26 has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list r: fixed Issue is closed as already fixed in a newer version team-tool Owned by Flutter Tool team tool Affects the "flutter" command-line tool. See also t: labels. triaged-tool Triaged by Flutter Tool team
Projects
None yet
5 participants