Repository navigation
[Document] Usage of Dart-Define variables in gradle scripts #100292
Description
While creating integration tests for our Flutter application, we're facing a blocking issue:
Following the official flutter documentation for integration testing, in the section to automate the tests on Firebase Test Lab, its stated to build the app with gradle.
pushd android
# flutter build generates files in android/ for building the app
flutter build apk
./gradlew app:assembleAndroidTest
./gradlew app:assembleDebug -Ptarget=integration_test/<name>_test.dart
popdUnfortuantely, its not stated on how to include --dart-define variables into the script so that the created APK is build with the variable defined.
According to a stackoverflow thread, I've tried setting the variable in gradle.properties, with no effect.
As nothing in this matter is documented, I assume its not possible and therefor file this issue.
If it is possible, how can we pass the dart-define to the gradle build? Adjusting the documentation here might be helpful :)
Expected results:
Integration testable APK with a dart-define variable modified in its source code.
Actual results:
The APK only has default behavior, the variable can't be modified.
Logs
[✓] Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.22000.556], locale de-DE)
• Flutter version 2.10.3 at X:\Flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 7e9793dee1 (2 weeks ago), 2022-03-02 11:23:12 -0600
• Engine revision bd539267b4
• Dart version 2.16.1
• DevTools version 2.9.2
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at X:\SDK
• Platform android-31, build-tools 31.0.0
• Java binary at: X:\JetBrains\apps\AndroidStudio\ch-0\211.7628.21.2111.8193401\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
• All Android licenses accepted.
Activity
[-]Dart-Defines can not be set by gradle build[/-][+][Document] Usage of Dart-Define variables in gradle scripts[/+]Sese-Schneider commentedon Mar 17, 2022
@maheshmnj thank you for your triage! as per your investigation it looks like you think its already implemented, and just not documented.
Could you share the way to include the
dart-definein gradle with us?maheshj01 commentedon Mar 17, 2022
@Sese-Schneider, I am not sure about it, nor I could find a way to use the dart define constants in Gradle scripts, Lets wait for the secondary triage to take a look at it, They may have a better understanding of it.
cc: @blasten
onkarmonks commentedon Mar 18, 2022
@Sese-Schneider Did you find a way to do it?
I need to send data on basis of my Android grade flavor to main.dart to set appropriate .env file
Sese-Schneider commentedon Mar 20, 2022
@onkarmonks we also had this issue in the past, but failed to find a way to do this.
What we do is to supply
--flavorand a--dart-definein theflutter buildstep.However, this question is about injecting the
--dart-definein Androidsgradlew assembleDebugstep.roblframpton commentedon Mar 23, 2022
I found a solution to this. I inspected the verbose output of
flutter buildand found that the--dart-defineflags get base64 encoded, comma-separated, and passed to a Gradle via a flag called-Pdart-defines. (Note the s at the end of this flag, since it contains all the defines).So for example, the following dart defines passed to Flutter:
turn into the following flag passed to Gradle:
since,
myvar=123isbXl2YXI9MTIzin base64 andanothervar=testisYW5vdGhlcnZhcj10ZXN0in base64. Simply pass this flag to your Gradle command, e.g.:This solved the problem for me.
Sese-Schneider commentedon Mar 29, 2022
@roblframpton thank you very much, this solved our issues 🚀
/cc @onkarmonks
@maheshmnj given this is documented nowhere, I think the labels are correct as of missing documentation 👍🏼
dustin-graham commentedon Apr 6, 2022
For anyone else struggling to make the solution provided by @roblframpton , remember to base64 encode just the key=value portion of the --dart-define. This is probably obvious to everyone else but me, but I was encoding "--dart-define=key=value". That definitely does not work! @roblframpton explained it perfectly, just encode the key=value.
ivan-horchakov commentedon Apr 29, 2022
@roblframpton & @dustin-graham guys, inside my integration tests I have TEST_ENV var with the default value "develop". When I want to change it to "stage" for the local run via flutter driver - I'm using this:
flutter drive --driver=test_driver/integration_test.dart --target=integration_test/tests/messaging_test.dart --dart-define=TEST_ENV=stagingand it is working.But I need to figure out how to pass it during my Jenkins job, which builds and initiates test run in Firebase Test Lab. Here is my steps:
flutter build apk --debug./gradlew app:assembleAndroidTest./gradlew app:assembleDebug -Ptarget="pwd"/../integration_test/smoke_test.dartI tried the approach, mentioned by you in points 2 & 3 (base64 & usual) but it still uses the default value "develop". TIA!
krishnakumarcn commentedon Apr 29, 2022
Hey @ivan-horchakov , I had a similar use case and solved it like this:
Where
{base64String}is the encoded key=value pair, in your casebase64("TEST_ENV=staging")or directly from your previous code sample:
ivan-horchakov commentedon Apr 29, 2022
Thanks, @krishnakumarcn - you've made my day!
bartekpacia commentedon Dec 3, 2022
I'm having a problem with passing dart defines to the
app:connectedDebugAndroidTesttask, which is needed to run integration tests natively on Android.but it's spitting out really ugly error message:
error message
Update 1
Hahah, it was my stupid mistake. I used
echo, which adds a newline by default to the output.Bad:
$ echo 'CTS_DISABLE_MAP_PRELOADING=true' | base64Good:
$ printf 'CTS_DISABLE_MAP_PRELOADING=true' | base64Update 2
The above works for
app:assemble*family of tasks, but I have no luck withapp:connectedDebugAndroidTesttask :(It works as if only a single dart-define is accepted.
github-actions commentedon Apr 6, 2023
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.