patrol icon indicating copy to clipboard operation
patrol copied to clipboard

"--flavor foo" value is not mapped to built in flutter "appFlavor" constant on Android

Open gair-mddv opened this issue 1 year ago • 4 comments

Steps to reproduce

on android emulator

  1. Run any test on app with providing existing flavor through "--flavor "
  2. try to access the flavor value that you pass in that test using appFlavor
  3. you will get null instead of value of flavor you passed

Actual results

When i run my test on android using command below flutter's default constant appFlavor is NULL but it should be initialised with value that we pass. On iOS works like it should. Only happening on android. here is my command to run test patrol test -t integration_test/main_screen_test.dart --flavor dev --dart-define-from-file=.env.dev.json -d "emulator-5554" -v

Logs

No response

Patrol version

patrol: 3.9.0

Patrol Doctor output

Patrol Doctor output
Patrol doctor:
Patrol CLI version: 2.8.1
Flutter command: flutter 
  Flutter 3.22.1 • channel stable
Android: 
• Program adb found in /Users/gair/Library/Android/sdk/platform-tools/adb
• Env var $ANDROID_HOME set to /Users/gair/Library/Android/sdk
iOS / macOS: 
• Program xcodebuild found in /usr/bin/xcodebuild
• Program ideviceinstaller found in /opt/homebrew/bin/ideviceinstaller

Flutter Doctor output

Flutter Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.1, on macOS 14.2.1 23C71 darwin-arm64, locale en-RU)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.5)
[✓] VS Code (version 1.91.0)
[✓] Connected device (6 available)
    ! Error: Browsing on the local area network for s’s Apple Watch. Ensure the device is unlocked and discoverable via Bluetooth. (code -27)
[✓] Network resources

• No issues found!

gair-mddv avatar Jul 09 '24 14:07 gair-mddv

Hello @gair-mddv, thank you taking time to report this bug, I added it to the task list.

zoskar avatar Jul 10 '24 09:07 zoskar

For some reason the --flavor fails to pass the flavor info appFlavor variable, you can use the following trick:

This is the definition of appFlavor:

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// The flavor this app was built with.
///
/// This is equivalent to the value argued to the `--flavor` option at build time.
/// This will be `null` if the `--flavor` option was not provided.
const String? appFlavor = String.fromEnvironment('FLUTTER_APP_FLAVOR') != '' ?
  String.fromEnvironment('FLUTTER_APP_FLAVOR') : null;

This means you can run patrol with: patrol test --dart-define "FLUTTER_APP_FLAVOR=your_app_flavor". Or you can use .patrol.env as described here.

Hope this helps you, it worked for me with multiple flavors.

dinko7 avatar Sep 17 '24 11:09 dinko7

Did this get fixed with the merge? patrol test --dart-define "FLUTTER_APP_FLAVOR=your_app_flavor" doesn't work.

benji-farquhar avatar Apr 26 '25 17:04 benji-farquhar

It appear that the flavor flag is passed to the first test. After that and second test does not have the flavor flag set. This happens whether you are running a batch of tests using 'patrol test' or 'patrol develop' on one test repeatedly. This is huge problem for CI. Trying to use dart-define with FLUTTER_APP_FLAVOR results in "FLUTTER_APP_FLAVOR is used by the framework and cannot be set using --dart-define or --dart-define-from-file".

This is especially problematic with a flavor and patrol develop. I keep getting Hot Restart: logs connected [WARN] Hot Restart: not attached to the app yet Hot Restart: attached to the app Patrol develop key commands: r Hot restart. q Quit (terminate the process and application on the device). Patrol DevTools extension is available at http://127.0.0.1:61150/patrol_ext?uri=http://127.0.0.1:60624/FmBf1f5b5Gk=/ ✓ App shut down on request (46.4s)

Inside the logs I get 07-16 15:40:15.515 1809 1908 W PackageManager: Not removing non-existent package com...... where com...... is not the flavor I am trying to run (not the default in my pubspec.yaml which this is).

If I manually remove the app from my android device then it all works again.

asaworld avatar Jul 16 '25 03:07 asaworld