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.
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 passdart-define
variablesConsider that your dart-define flag is
environment=staging
Then on normal flutter run, you will be using it like this.
To pass this via
gradlew
command, convert it into base64 encoded string and pass it like below.Base64 encoded string of
environment=staging
isZW52aXJvbm1lbnQ9c3RhZ2luZw==
So use
If you have multiple dart defines, add them separated by comma. (note that the flag is -Pdart-defines - (the define
s
clarifies it) )