Skip to content

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

@alestiago

Description

@alestiago

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!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: assetsPackaging, accessing, or using assetsfound in release: 3.24Found to occur in 3.24found in release: 3.26Found to occur in 3.26has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions