flutterflutter-build

flutter build_runner takes too long


I am using EasyLocalization, mobx, JsonSerializable etc. To generate *.g.dart files, I am using build_runner watch or the script in scripts/build.sh:

flutter packages pub run build_runner build

It was taking 2 seconds at most, but then suddenly it started to take at least 10 seconds or sometimes 30 seconds. Because of this situation, build_runner watch became useless, I mean it's not working properly since then.

The output on the console when I run my script:

$ sh scripts/build.sh 
[INFO] Generating build script...
[INFO] Generating build script completed, took 611ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 126ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 745ms

[INFO] Running build...
[INFO] 1.2s elapsed, 0/16 actions completed.
[INFO] 2.2s elapsed, 0/16 actions completed.
[INFO] 3.3s elapsed, 0/16 actions completed.
[INFO] 12.5s elapsed, 1/17 actions completed.
[INFO] 13.6s elapsed, 4/20 actions completed.
[INFO] Running build completed, took 14.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 57ms

[INFO] Succeeded after 14.6s with 2 outputs (47 actions)

Solution

  • I finally decided to try and solve this myself, I've improved it slightly by adding the following to my pubspec.yaml: E.g.

    targets:
      $default:
        builders:
          json_serializable:
            enabled: true
            generate_for:
              include:
                - lib/your_codegen_folder/**.dart
                - lib/**.gen.dart
    

    Hope this speeds it up a little for you. You'll have to add builder configs for your other two generators like the json_serializable one here. I suspect they might be referred to easy_localization and mobx? I don't know, I'm new to this.