I'm Embedding a Flutter app as a module within an existing Android app ( https://flutter.dev/docs/development/add-to-app/android/project-setup ) and would like to know if/how I can use "--dart-define" to define compile-time constants. Tried using ./gradlew -Ddart-define=myVal=Value
without any luck.
When building a typical Flutter app I would use the flutter
command. In my case I'm continuing to use gradlew
to build my app and it's unclear how to pass in --dart-define constants.
If your project depends on the Android Archive (AAR).
You can pass the dart-defines
in the command line.
flutter build aar --dart-define=myVal=Value
If your project depends on the module's source code.
You can set dart-define
s in gradle.properties
in your android host project directory (or in yourHostProject/yourFlutter/.android/Flutter/
directory).
--dart-define=myVal=Value
Explanation
.android/Flutter/build.gradle
script executes "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
script. When the flutter.gradle
is executed, your dart files are compiled into naitive code.
flutter.gradle
loads dart-defines from the .android/Flutter
project instance ( flutter.gradle#731-L734 ) and then, use it ( flutter.gradle#L1091-L1093 ).
To set the project instance value, you add --dart-define=myVal=Value
into gradle.properties
. (If you have multiple gradle.properties
files, consider configuration order( Gradle properties in Gradle Docs ).
In android studio you may edit running configuration (press drop-down near "run" button -> "Edit configurations...") and define variables there ("Additional run args:" row):
--dart-define="http_serv=http://10.0.2.2:42627/" --dart-define="websocket=ws://10.0.2.2:42627/websocket"
Getting variable in code:
final checkArgs = String.fromEnvironment('http_serv', defaultValue: '');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.