Skip to content

On Android when there are multiple flavors and no --flavor flag is specified then use a default one #22856

Closed

Description

@radiKal07

On Android, when there are multiple flavors defined, and the user runs the app but does not specify a value for the --flavor flag then the following error is shown:

You must specify a --flavor option to select one of them.

This is implemented like that here: https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/android/gradle.dart#L342

This makes it so that everytime we have to specifiy a value for --flavor.
I would propose when the flag is not specified to just default to the first one.
This will make it faster to start your Flutter app, not having to write --flavor _flavor_ everytime.

Activity

  1. added
    toolAffects the "flutter" command-line tool. See also t: labels.
    t: gradle"flutter build" and "flutter run" on Android
    on Oct 9, 2018
  2. added this to the milestone on Oct 9, 2018
  3. matthewcook333 commented on Dec 7, 2018

    @matthewcook333

    Also running into this issue, it is a pain to not be able to just run with default commands. Would be nice to have a default flavor.

  4. royorange commented on Feb 14, 2019

    @royorange

    +1 Also face the same issue.

  5. bradyt commented on Aug 19, 2019

    @bradyt

    @cmwarrington, do you know if flutter should be able to move forward on this? I saw your involvement at the following commits, and made a naive guess you might have an idea for this.

  6. mewadaArvind commented on Oct 11, 2019

    @mewadaArvind

    Also face the same issue. please give a answer this question.

  7. jackyhieu1211-hn commented on Mar 4, 2020

    @jackyhieu1211-hn

    Hello. I have the same problem.
    I fixed it the following way
    I use Android Studio.
    open Edit Configurations -> build flavor -> enter flavor name (example : develop)

  8. erabti commented on May 19, 2020

    @erabti

    Any work around for this? This is really needed for us.

  9. msmaromi commented on May 20, 2020

    @msmaromi

    in vscode you can setup debug configuration

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Flutter Dev",
                "request": "launch",
                "args": ["--flavor", "development"],
                "type": "dart"
            }
        ]
    }
    
  10. jackyhieu1211-hn commented on May 20, 2020

    @jackyhieu1211-hn

    @erabti If you use Android Studio. You can try my way.
    open Edit Configurations -> build flavor -> enter flavor name (example : develop)

  11. removed this from the milestone on Jun 1, 2020
  12. nazdream commented on Apr 13, 2021

    @nazdream

    Does anybody know how to set default flavor in Bitrise? I am getting the same error when Bitrise is trying to build the app, and I can't find a way to set flavor.

  13. 11 remaining items

  14. marianhlavac commented on Jul 10, 2023

    @marianhlavac

    Can this be given a higher priority? This is confusing for almost everyone onboarding a project having flavors, especially when iOS behaves differently, and especially when Flutter gives cryptic error messages that doesn't suggest the flavor is missing?

  15. pattobrien commented on Aug 2, 2023

    @pattobrien

    ... especially when Flutter gives cryptic error messages that doesn't suggest the flavor is missing?

    Here's the cryptic message in question:

    Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
    main.dart:1
    Exception: Gradle build failed to produce an .apk file. It's likely that this file was generated under /Users/pattobrien/dev/fine_designs/template/src/frontend/build, but the tool couldn't find it.
    Exited
    

    This is not a helpful message in the slightest, especially because there is an apk file in that directory, it's just not the name expected.

    If a default flavor cannot be selected, then it would be helpful to at least mention what the expected file name is, so that a user can easily spot the difference between the file that does exist vs. the file that the flutter tool is expecting. This seems like a quick win IMO.

  16. vietstone-ng commented on Aug 8, 2023

    @vietstone-ng

    Please support this

  17. giljulio commented on Aug 16, 2023

    @giljulio

    Not a default, but if you rather make the issue more explicit - you can use the following code to fail the build when it's not specified

    gradle.taskGraph.whenReady { taskGraph ->
        if (!project.hasProperty('android.injected.build.model') // ensure we're not in sync mode
            && taskGraph.allTasks.any { it.name.toLowerCase().equals("assembledebug") || it.name.toLowerCase().equals("assemblerelease") }) {
            throw new Exception("Please specify a flavor with --flavor argument")
        }
    }
    
  18. SilenceCodder commented on Oct 7, 2023

    @SilenceCodder

    https://stackoverflow.com/a/60197203/3607358

    ... especially when Flutter gives cryptic error messages that doesn't suggest the flavor is missing?

    Here's the cryptic message in question:

    Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
    main.dart:1
    Exception: Gradle build failed to produce an .apk file. It's likely that this file was generated under /Users/pattobrien/dev/fine_designs/template/src/frontend/build, but the tool couldn't find it.
    Exited
    

    This is not a helpful message in the slightest, especially because there is an apk file in that directory, it's just not the name expected.

    If a default flavor cannot be selected, then it would be helpful to at least mention what the expected file name is, so that a user can easily spot the difference between the file that does exist vs. the file that the flutter tool is expecting. This seems like a quick win IMO.

    Does this error mean that it's missing a Flavor name?
    Can't the project run by default using flutter run command?

  19. slaci commented on Feb 12, 2024

    @slaci

    I just bumped into this and with flutter 3.16.x (and gradle 8.2) flutter picks a default flavor (I don't know since which version).

    By default gradle sorts the flavor names in alphabetical order and picks the first one. Seems like flutter only supports this.

    In Gradle it is possible to mark a flavor as default, but from my tests flutter ignores it:

    flavorDimensions=["version"]
        productFlavors {
            b {
                getIsDefault().set(true)
                // isDefault true
                dimension "version"
            }
            a {
                dimension "version"
            }
        }

    When defining this I would expect b as the default , but when running the app with flutter run then it picks a instead.

    Edit: The alphabetical order trick worked randomly only, could not reproduce between two full builds. So I reduce my points to just: the gradle isDefault setting is ignored. So a default flavor is indeed picked, but I can't figure out how to control which one.

  20. ChaseGuru commented on May 10, 2024

    @ChaseGuru

    Why setting a default doesn't work

    Flutter seems to by default do all flavors.

        productFlavors {
            dev {
                dimension "default"
                resValue "string", "app_name", "Myapp Dev"
                applicationIdSuffix ".dev"
            }
            live {
                dimension "default"
                resValue "string", "app_name", "Myapp"
                applicationIdSuffix null
            }
            beta {
                dimension "default"
                resValue "string", "app_name", "Myapp Beta"
                applicationIdSuffix ".beta"
            }
        }
    
    > rm -r ./build/app/outputs/flutter-apk
    > flutter build apk
    ..snip..
    Gradle build failed to produce an .apk file. It's likely that this file was generated under /path/to/project/build, but the tool couldn't find it.
    > ls -la build/app/outputs/flutter-apk
    -rw-rw-r-- 1 chase chase 29548328 May  9 20:21 app-dev-release.apk
    -rw-rw-r-- 1 chase chase 29548326 May  9 20:21 app-beta-release.apk
    -rw-rw-r-- 1 chase chase 29548300 May  9 20:21 app-live-release.apk

    Contrarily though, appbundle seems to only produce one.

    flutter run also fails for me without a flavor

  21. hasanm08 commented on May 11, 2024

    @hasanm08

    can you please merge this PR? #147968

  22. added a commit that references this issue on May 22, 2024
  23. andrewkolos commented on May 22, 2024

    @andrewkolos
    Contributor

    #147968 will allow users to specify a default-flavor in their pubspec and should be available in the next stable release (whatever comes after 3.22). I believe this satisfies the primary concern/request of this issue.

    #139286 tracks the issue of the confusing error message shown when an Android-enabled project has flavors configured but --flavor is not provided.

  24. github-actions commented on Jun 5, 2024

    @github-actions

    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.

  25. locked as resolved and limited conversation to collaborators on Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityt: gradle"flutter build" and "flutter run" on Androidteam-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