Flutter Flavors and .env Files: Use Cases and How to Set Them Up with CI/CD for Multiple Stages

When building mobile apps, managing multiple environments like development, staging, and production is crucial for a smooth workflow. Flutter provides a flexible way to handle this through Flavors and files. This article explains what flavors and files are, their use cases, and guides you step-by-step on how to configure both in your Flutter project. Finally, it shows how to set up CI/CD for all three stages and automate APK builds with a full workflow example.

What is a Flutter Flavor?

Flutter flavoring allows you to create multiple build variants of the same app from a single codebase. Each flavor represents a different version of the app with its own configurations like app name, icon, API endpoints, Firebase project, and build settings.

For example, you might want:

  • A development flavor for testing new features connected to a development backend.

  • A staging flavor for QA testing using a staging environment.

  • A production flavor that is the release version for end users.

These flavors can have different package names, enabling you to install multiple flavors on the same device simultaneously. Flavors are especially useful for separating concerns, testing, and releasing apps with confidence.


What is a .env File?

A file is a simple plain-text file used to store environment variables such as API keys, URLs, feature flags, and other configuration values. The key idea is to keep sensitive information or environment-specific configurations out of your source code.

By using files, you can:

  • Keep secrets and sensitive data safe by excluding them from version control.

  • Easily switch between configurations for development, staging, and production without modifying your app code.

  • Maintain cleaner code by referencing environment variables instead of hardcoding values.

In Flutter, you can use the package to load files at runtime.


Why Use Both Flavors and .env Files?

While flavors control the build variant and app identity, files control the configuration loaded at runtime. Together, they provide a powerful way to:

  • Build multiple versions of your app with different identities.

  • Load environment-specific data securely and efficiently.

  • Manage multiple environments without duplicating code.

Your flavor’s entry point (for example, ) will load the corresponding file dynamically so each build has its own configuration.


How to Add Flavors and .env Files in a Flutter Project

Step 1: Create Flavor-Specific Entry Points

In your folder, create three Dart files:

Each file should load the corresponding file using :

Repeat for each flavor, loading and respectively.


Step 2: Prepare .env Files

Create , , and files in the root folder with environment-specific variables, for example:

Make sure to add to your if you want to keep secrets out of version control.

Step 3: Configure Flutter for Flavors

Modify your Android and iOS native project to support flavors.

Android: Edit to add product flavors:

  • iOS: In Xcode, create multiple schemes for development, staging, and production. Assign different bundle identifiers for each.

This step enables you to build separate APKs/IPAs for each flavor.


Setting Up CI/CD for Multiple Flavors with .env Files

Managing flavors and files manually can be error-prone, especially when deploying. Automate the process with CI/CD pipelines.

Key Points in CI/CD:

  • Build each flavor in parallel.

  • Generate files dynamically from CI secrets (avoid committing secrets).

  • Run unit tests before build.

  • Build APK and IPA for each flavor.

  • Upload build artifacts for download or deploy to app stores.

  • Schedule the pipeline to run daily or on demand.


Sample Full GitHub Actions Workflow

Explanation of Workflow Steps

  1. Checkout Code: Pulls your source from GitHub.

  2. Set up Flutter: Installs Flutter SDK on the runner.

  3. Install Dependencies: Runs to install packages.

  4. Create file dynamically: Generates the environment file per flavor from GitHub secrets (like ).

  5. Run Unit Tests: Ensures code quality before building.

  6. Build APK: Uses the flavor-specific main file and passes environment info via .

  7. Upload APK: Saves the APK as an artifact for download or deployment.

  8. Build IPA: Similarly builds iOS app for each flavor.

  9. Upload IPA: Saves the iOS build artifact.


Suggestions and Best Practices

  • Keep secrets secure: Store all API keys and sensitive variables in GitHub Secrets or a secrets manager, never commit them to Git.

  • Use flavor-specific Firebase projects if your app uses Firebase to separate analytics and push notifications.

  • Test all flavors thoroughly locally before running CI/CD to avoid build failures.

  • Consider automating app store uploads with Fastlane for full deployment automation.

  • Use semantic versioning and flavor-specific version names to easily track builds.

  • Keep your flavor configuration consistent across Android, iOS, and Flutter code.

  • Document your flavor and environment setup for your team to ensure smooth collaboration.


By combining Flutter flavors with files and automating your builds with CI/CD, you gain powerful control over multiple app versions, reduce human errors, and accelerate your release cycle.

#flutter #flutterdev #flutterflavors #dotenv #envfiles #mobiledevelopment #crossplatform #cleanarchitecture #cicd #ci #cd #devops #githubactions #buildautomation #mobileci #appdevelopment #apks #ipa #softwareengineering #codequality

Ahad J.

Flutter Software Engineer & Flutter Enthusiast 🎯 Helping Businesses Scale with Mobile Apps using Flutter 💙

1mo

I was looking to read something similar and your post comes up as a blessing. Thanks for this informative blog Shahanaj Parvin 🙌🏻

Like
Reply
Md. Al-Amin

Mobile App Developer (Flutter & Dart) | SWE L-II at Vivasoft Limited | Ex Jr. SWE at Rokomari | Spring Boot Expert | Instructor at Instructory |

1mo

Super helpful for teams managing multiple environments in Flutter. One thing to watch out for storing .env files directly in the app (even with flutter_dotenv) can expose sensitive data like API keys, since they get bundled into the binary. For production, it’s safer to offload secrets to a secure backend or inject them at runtime via remote config or CI/CD secrets.

Like
Reply
Ahmad Yusuf

Mobile App Engineer (Flutter) | Building Performant, Scalable Apps | Productivity

1mo

Thanks for sharing, Shahanaj Right on time 😅👍🏽

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics