Flutter: How to use dart-define variables in gradle scripts

2k views Asked by At

While creating integration tests for a flutter app, how to pass a dart-define variables via gradle scripts.

From Flutter documentation, in order to build an instrumentation test apk (and later to upload it to Firebase test lab and test), we need to directly use gradlew commands like this:

pushd android
flutter build apk
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=integration_test/<name>_test.dart
popd

Here its not specified how to pass --dart-define flag's content that we are using from flutter side flutter run to the gradlew command.

1

There are 1 answers

0
krishnakumarcn On BEST ANSWER

Flutter dart-define flag is encoded to base64, and passed to gradle as comma separated fields with an argument name -Pdart-defines. So we can utilise this to manually pass dart-define variables

Consider that your dart-define flag is environment=staging

Then on normal flutter run, you will be using it like this.

flutter run --dart-define environment=staging

To pass this via gradlew command, convert it into base64 encoded string and pass it like below.

Base64 encoded string of environment=staging is ZW52aXJvbm1lbnQ9c3RhZ2luZw==

So use

./gradlew app:assembleDebug -Ptarget=integration_test/<name>_test.dart -Pdart-defines="ZW52aXJvbm1lbnQ9c3RhZ2luZw=="

If you have multiple dart defines, add them separated by comma. (note that the flag is -Pdart-defines - (the defines clarifies it) )

./gradlew app:assembleDebug -Ptarget=integration_test/<name>_test.dart -Pdart-defines="ZW52aXJvbm1lbnQ9c3RhZ2luZw==,ZW1haWw9bXllbWFpbEBlbWFpbC5jb20="