Exclude assets from debug build on Flutter


I have 8000+ files named (1.txt, 2.txt, 3.txt, etc) on my /assets/files folder. However I don’t need all of them for my debug build. I only need the first 30 of them. Indeed, if I leave all those files on the assets folder the build time is huge.

I’ve learned there is a source set concept but it applies only to Java and Kotlin on Android Studio.

How can I exclude those exceeding files from debug build on Flutter?


This ‘problem’ don’t have any built-in solution yet (because of Dart and Flutter’s design)

It was discussed on the following issue :

A bit hacky (but working) solution is to script the edition of your pubspec.yaml to update the assets to import before each build.
Since there is no ‘pre-build’ method yet with the Flutter Framework, you can use a Makefile to execute your script.

The following would work :

Step 1 – Update your pubspec.yaml to ‘tag’ the part of your assets you want to update.

    - assets/icons/icon.png
    - assets/translations/
    - assets/images/

The tags here are obviously #DYN_ASSETS_START and #DYN_ASSETS_END

Step 2 – Create a Script to update your assets.
The following is just a working example, it can be strongly improved since I’m far away from being an expert in Bash.
The point of the script is to find the two tags which were set in your pubspec.yaml and update everything which is between those two tags.



if [ "$BUILD_MODE" = "DEBUG" ]; then
    #DYN_ASSETS_START'"\\${nl}"'    - assets/images/image1.png'"\\${nl}"'    - assets/images/image2.png'"\\${nl}"'    - assets/images/image3.png'"\\${nl}"'    - assets/images/image4.png'"\\${nl}"'    - assets/images/image5.png'"\\${nl}"'    #DYN_ASSETS_END'"\\${nl}" pubspec.yaml > temp.yaml
    mv temp.yaml pubspec.yaml
elif [ "$BUILD_MODE" = "RELEASE" ]; then
    #DYN_ASSETS_START'"\\${nl}"'    - assets/images/'"\\${nl}"'    #DYN_ASSETS_END'"\\${nl}" pubspec.yaml > temp.yaml
    mv temp.yaml pubspec.yaml

Disclaimer : this script is not sexy at all.
sed may not be the best tool to achieve this, the code is hard to maintain read and evolve, and I only tested it on MacOS (I doubt it’d work on another environment).
The goal of this script here is to give you an idea of a workaround fulfilling the author’s request.

Step 4 – Create a Makefile.

    ./your_script.sh DEBUG
    flutter pub get
    flutter build apk

    ./your_script.sh RELEASE
    flutter pub get
    flutter build apk --release

Step 5 – Trigger the Makefile Flow

make build-android-debug

Answered By – FDuhen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published