I have the following app structure:
- Main app (Android native)
- Flutter module (aar)
- Flutter plugin
The main Android App uses a Flutter module that contains some Flutter plugins.
One of this plugins requires a set of dart-define variables.
I could add the --dart-define argument on Flutter module AAR build, but there's some internal factors that makes this option not so good.
So, my question is, there's a way to add this dart-define settings inside the Flutter plugin (in the gradle scripts)?
Thanks!
Yes, you can add Dart define variables to your Flutter plugin through Gradle scripts.
In
build.gradlefile of your Flutter plugin module Add the below code to the android section of the file:In your Flutter plugin Dart code, you can access the value of the build config field,
The
String.fromEnvironmentmethod will retrieve the value of theMY_DART_DEFINE_VARbuild config field and assign it to themyDartDefineVarconstant. If the build config field is not defined, it will use the default valuedefault_value.You can use the
myDartDefineVarconstant in your Flutter plugin code as needed.Remember that you will need to rebuild your Flutter plugin module and update the
AARfile in your main Android app to use the updated plugin with the new Dart define variables.