Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run custom command before flutter build?

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?

like image 405
RedHatter Avatar asked Jul 13 '19 19:07

RedHatter


People also ask

How do I run debug mode on flutter?

1- The first one is to use the debugShowCheckModeBanner property in your MaterialApp widget . And then do a hot reload. 2-The second possibility is to hide debug mode banner in Flutter Inspector if you use Android Studio or IntelliJ IDEA . 3- The third possibility is to use Dart DevTools .


3 Answers

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.

like image 133
Ben Winding Avatar answered Oct 10 '22 10:10

Ben Winding


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

like image 29
hysabone.com Avatar answered Oct 10 '22 09:10

hysabone.com


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.

like image 1
abianche Avatar answered Oct 10 '22 09:10

abianche