Run custom command before flutter build?
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
2. Add a new Shell Script configuration from (+) Add New Configuration > Shell Script
3. Setup your shell script execution. Make sure to click on Apply
after you're done. But don't close it yet.
4. Select Flutter > Run/main.dart > Before Launch > (+)Add > Run Another Configuration
5. Choose your Shell Script configuration
6. Click Apply
then click OK
. Your configured Shell Script will now run.
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.
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, 2022Comments
-
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?