Flutter app build fails when trying to use flavors

4,563

The answer has been staring me in the face for a whole day.

If you change the applicationId you need a new google-services.json file.

The solution is to generate the file for every "Flavor", or more specifically for every applicationId, and to include these into the application resources.

Here is a good answer on how to do this: https://stackoverflow.com/a/34364376/1810447

Share:
4,563
The Tahaan
Author by

The Tahaan

My feet on a mountain, I am alive.

Updated on December 14, 2022

Comments

  • The Tahaan
    The Tahaan over 1 year

    I am trying to use Flavors in my app. I followed various guides and it failed to build each time with the same problem. (Since essentially every blog post and SE question quotes the same articles and resources I won't do it again)

    In order to debug, I decided to simplify the problem and started a new project (The normal sample project), and added flavors with only one element per flavor: The application. This worked. Various other more complex attributes, eg using res/values/strings.xml for the app label also worked fine.

    I then tried the simplest possible flavor setup with my app, with the grade.build as per below, which then promptly failed with the same error.

    flavorDimensions "app"
    productFlavors {
        dev {
            dimension "app"
        }
        qa {
            dimension "app"
            applicationId "com.example.app1"
        }
    }
    

    Note: I'm also using separate entry points to implement run-time configurations. This works as expected and has been in use for a while now.

    johan@komp:~/AndroidStudioProjects/parent_lite$ flutter run --flavor qa -t lib/main-qa.dart
    Using hardware rendering with device Android SDK built for x86. If you get graphics artifacts, consider enabling software rendering with "--enable-software-rendering".
    Launching lib/main-qa.dart on Android SDK built for x86 in debug mode...
    Initializing gradle...                                              0.4s
    Resolving dependencies...                                           1.2s
    Running Gradle task 'assembleQaDebug'...                                
    Running Gradle task 'assembleQaDebug'... Done                       1.1s
    Gradle task assembleQaDebug failed with exit code 1
    

    When launching the "dev" flavor which does not include any custom properties (via flutter run --flavor dev -t lib/main-dev.dart) it builds and runs.

    The interesting part of running with --verbose is as follow

    [ +317 ms] Running Gradle task 'assembleQaDebug'... (completed in 1.1s)
    [   +2 ms] "flutter run" took 3,055ms.
    [        ] "flutter run" took 3,055ms.
    Gradle task assembleQaDebug failed with exit code 1
    
    #0      throwToolExit (package:flutter_tools/src/base/common.dart:28:3)
    #1      _buildGradleProjectV2 (package:flutter_tools/src/android/gradle.dart:507:5)
    <asynchronous suspension>
    #2      buildGradleProject (package:flutter_tools/src/android/gradle.dart:346:14)
    <asynchronous suspension>
    #3      buildApk (package:flutter_tools/src/android/apk.dart:34:9)
    <asynchronous suspension>
    #4      AndroidDevice.startApp (package:flutter_tools/src/android/android_device.dart:426:13)
    <asynchronous suspension>
    #5      FlutterDevice.runHot (package:flutter_tools/src/resident_runner.dart:371:54)
    <asynchronous suspension>
    #6      HotRunner.run (package:flutter_tools/src/run_hot.dart:251:39)
    <asynchronous suspension>
    #7      RunCommand.runCommand (package:flutter_tools/src/commands/run.dart:469:37)
    <asynchronous suspension>
    #8      FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:478:18)
    <asynchronous suspension>
    #9      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:383:33)
    <asynchronous suspension>
    #10     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:29)
    <asynchronous suspension>
    #11     _rootRun (dart:async/zone.dart:1124:13)
    #12     _CustomZone.run (dart:async/zone.dart:1021:19)
    #13     _runZoned (dart:async/zone.dart:1516:10)
    #14     runZoned (dart:async/zone.dart:1463:12)
    #15     AppContext.run (package:flutter_tools/src/base/context.dart:152:18)
    <asynchronous suspension>
    #16     FlutterCommand.run (package:flutter_tools/src/runner/flutter_command.dart:375:20)
    #17     CommandRunner.runCommand (package:args/command_runner.dart:197:27)
    <asynchronous suspension>
    #18     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:396:21)
    <asynchronous suspension>
    #19     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:29)
    <asynchronous suspension>
    #20     _rootRun (dart:async/zone.dart:1124:13)
    #21     _CustomZone.run (dart:async/zone.dart:1021:19)
    #22     _runZoned (dart:async/zone.dart:1516:10)
    #23     runZoned (dart:async/zone.dart:1463:12)
    #24     AppContext.run (package:flutter_tools/src/base/context.dart:152:18)
    <asynchronous suspension>
    #25     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:356:19)
    <asynchronous suspension>
    #26     CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:112:25)
    #27     new Future.sync (dart:async/future.dart:224:31)
    #28     CommandRunner.run (package:args/command_runner.dart:112:14)
    #29     FlutterCommandRunner.run (package:flutter_tools/src/runner/flutter_command_runner.dart:242:18)
    #30     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:62:22)
    <asynchronous suspension>
    #31     _rootRun (dart:async/zone.dart:1124:13)
    #32     _CustomZone.run (dart:async/zone.dart:1021:19)
    #33     _runZoned (dart:async/zone.dart:1516:10)
    #34     runZoned (dart:async/zone.dart:1500:12)
    #35     run.<anonymous closure> (package:flutter_tools/runner.dart:60:18)
    <asynchronous suspension>
    #36     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:29)
    <asynchronous suspension>
    #37     _rootRun (dart:async/zone.dart:1124:13)
    #38     _CustomZone.run (dart:async/zone.dart:1021:19)
    #39     _runZoned (dart:async/zone.dart:1516:10)
    #40     runZoned (dart:async/zone.dart:1463:12)
    #41     AppContext.run (package:flutter_tools/src/base/context.dart:152:18)
    <asynchronous suspension>
    #42     runInContext (package:flutter_tools/src/context_runner.dart:56:24)
    <asynchronous suspension>
    #43     run (package:flutter_tools/runner.dart:51:10)
    #44     main (package:flutter_tools/executable.dart:62:9)
    <asynchronous suspension>
    #45     main (file:///home/johan/dev/flutter/packages/flutter_tools/bin/flutter_tools.dart:8:3)
    #46     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
    #47     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
    

    Note that if I remove the applicationIDfrom the "qa" flavor, it builds. Also, the moment I add the applicationId to the dev flavor the same problem presents itself, and as I said this problem is not present with the standard flutter sample project (In fact results with the test with the simple project allowed me to set string resources and launcher icons on a per-flavor basis successfully)