Skip to content

--dart-define-from-file entries are passed as individual properties (-P) to gradle  #130599

@bartekpacia

Description

@bartekpacia

Is there an existing issue for this?

Steps to reproduce

  1. Create an empty project and cd into it

    flutter create example --platforms android --empty
    
  2. Create a defines.json file in the project root:

    echo '{"FIRST_KEY": "FIRST_VALUE"}' > defines.json
  3. Modify app/build.gradle to print the dart define during Gradle build. For example, add the following line inside the android block:

    android {
        ...
    
        println "dart-define: FIRST_KEY = ${getProperty('FIRST_KEY')}"
    
        defaultConfig {
            // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
            applicationId "com.example.example"
            // You can update the following values to match your application needs.
            // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
            minSdkVersion flutter.minSdkVersion
            targetSdkVersion flutter.targetSdkVersion
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
        }
    
        ...
        ```
    
  4. Run the build, passing the --dart-define-from-file option:

    flutter build apk --debug --dart-define-from-file defines.json
    

    It should pass - here's my output:

    $ flutter build apk --debug --dart-define-from-file defines.json
    
    dart-define: FIRST_KEY = FIRST_VALUE
    Running Gradle task 'assembleDebug'...                           1,313ms
    ✓  Built build/app/outputs/flutter-apk/app-debug.apk.
  5. Run the build, using the --dart-define option:

    flutter build apk --debug --dart-define 'FIRST_KEY=FIRST_VALUE'
    

    It fails with the following error:

    See logs

    $ flutter build apk --debug --dart-define 'FIRST_KEY=First value'
    
    
    FAILURE: Build failed with an exception.
    
    * Where:
    Build file '/Users/bartek/dev/random/example/android/app/build.gradle' line: 46
    
    * What went wrong:
    A problem occurred evaluating project ':app'.
    > Could not get unknown property 'FIRST_KEY' for extension 'android' of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
    
    * Try:
    > Run with --stacktrace option to get the stack trace.
    > Run with --info or --debug option to get more log output.
    > Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 436ms
    Running Gradle task 'assembleDebug'...                             937ms
    Gradle task assembleDebug failed with exit code 1
    

Expected results

I expect both ways of passing dart defines to work the same way. There's no information in the help text for flutter build (and friends) about this behavior:

flutter build apk --help

...
    --dart-define=<foo=bar>                             Additional key-value pairs that will be available as constants from the
                                                        String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and
                                                        double.fromEnvironment constructors.
                                                        Multiple defines can be passed by repeating "--dart-define" multiple times.
    --dart-define-from-file=<use-define-config.json>    The path of a json format file where flutter define a global constant pool. Json
                                                        entry will be available as constants from the String.fromEnvironment,
                                                        bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors;
                                                        the key and field are json values.
                                                        Multiple defines can be passed by repeating "--dart-define-from-file" multiple
                                                        times.
...

Actual results

The behavior differs.

Code sample

N/A / provided above

Screenshots or Video

N/A

Logs

N/A

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.5, on macOS 13.4.1 22F82 darwin-arm64, locale en-PL)
    • Flutter version 3.10.5 on channel stable at /Users/bartek/fvm/versions/3.10.5
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (4 weeks ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/bartek/androidsdk
    • Platform android-33, build-tools 33.0.1
    • ANDROID_HOME = /Users/bartek/androidsdk
    • Java binary at: /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/222.4459.24.2221.10121639/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/222.4459.24.2221.10121639/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.4)
    • IntelliJ at /Users/bartek/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 74.0.4
    • Dart plugin version 231.9161.14

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.4)
    • IntelliJ at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/231.9225.16/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.80.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.68.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.4.1 22F82 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 114.0.5735.198

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.10Found to occur in 3.10found in release: 3.12Found to occur in 3.12has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions