Run custom command before flutter build?

3,670

Solution 1

Flutter doesn't have this capability yet, it's kind of better to have a pre-build script.

Simple Pre-build Script

my-pre-build.sh

# my-pre-build.sh (run some commands)

Usage

./my-pre-build.sh && flutter build apk

Using Make to Abstract the Build

You could also abstract the build using something like a Makefile, which makes things simpler in my opinion.

Makefile (in root of your project)

# Build for Android (runs build-proto first)
build-apk: build-proto
    flutter test
    flutter build apk

# Build for iOS (runs build-proto first)
build-ios: build-proto
    flutter test
    flutter build ios

# Proto generation (calls terminal proto commands)
build-proto:
    proto-native-lib compile -f *.proto

Usage

make build-android
make build-ios

This is a useful way to bundle repetitive commands and reuse scripts between different build types.

Solution 2

I believe you can do this in Android Studio.

1. Go to Run > Edit Configurations

enter image description here

2. Add a new Shell Script configuration from (+) Add New Configuration > Shell Script

enter image description here

3. Setup your shell script execution. Make sure to click on Apply after you're done. But don't close it yet.

enter image description here

4. Select Flutter > Run/main.dart > Before Launch > (+)Add > Run Another Configuration

enter image description here

5. Choose your Shell Script configuration

enter image description here

6. Click Apply then click OK. Your configured Shell Script will now run.

enter image description here

Solution 3

In case you'd like to consider something different, I accomplish exactly what you are requesting by using an external build tools. In my case I chose https://fastlane.tools/, which neatly integrates with Flutter, works for Android and iOS and it's open source.

Share:
3,670
RedHatter
Author by

RedHatter

Hi! I'm Timothy. I have been messing around with computers for the past 10 years and have become quite proficient. I mainly work with the GUI side of software design and occasionally the more interesting parts of web design.

Updated on December 06, 2022

Comments

  • RedHatter
    RedHatter over 1 year

    I'm building a flutter app that uses protocal buffers and I would like to compile the .proto file during the build process. How can I run a custom shell command before the dart compilation of a flutter build?