Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

docs.flutter.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more

Flutter 3.41 is live! Check out theblog post!

Set up Flutter flavors for Android

How to create build flavors specific to different release types or development environments.

This guide shows you how to create Flutter flavors for an Android app.

Overview

#

A Flutter flavor when used with Android represents a unified term for various platform-specific features. For example, a flavor could determine which icon, app name, API key, feature flag, and logging level is associated with a specific version of your app.

If you want to create Flutter flavors for an Android app, you can do this in Flutter. In Android, a Flutter flavor is referred to as aproduct flavor.

The following illustrates an example of the Androidbuild variants that are created when an Android app has two product flavors (staging,production) and two build types (debug,release):

Product flavorsBuild typesResulting build variants
stagingdebug stagingDebug stagingRelease
productionrelease productionDebug productionRelease

Configure your product flavors

#

Complete the following steps to add two Android product flavors calledstaging andproduction to a new Flutter project calledflavors_example, and then test your project to make sure that the flavors work as expected.

  1. Create a new Flutter project calledflavors_example with Kotlin as the preferred Android language. By default, the project includes thedebug andrelease Android build types.

    console
    flutter create --android-language kotlin flavors_example
  2. Add the product flavors calledstaging andproduction to theflavors_example project.

    • In theflavors_example project, navigate to theandroid/app/ directory and openbuild.gradle.kts.

    • Add theflavorsDimension property and theproductFlavors properties inside of theandroid {} block. Make sure that theandroid {} block also contains the defaultdebug andrelease build types:

      build.gradle.kts
      kotlin
      android{...buildTypes{getByName("debug"){...}getByName("release"){...}}...flavorDimensions+="default"productFlavors{create("staging"){dimension="default"applicationIdSuffix=".staging"}create("production"){dimension="default"applicationIdSuffix=".production"}}}
  3. To make sure that you've set up everything correctly, run your app on the Android product flavors. You won't see any differences because the configuration settings haven't changed, but you do want to make sure that the app can run.

    • Start an Android emulator or connect a physical device with developer options enabled.

    • In the console, navigate to theflavors_example directory and enter the following command to test thestaging flavor:

      console
      flutter run --flavor staging
    • Repeat the previous step for theproduction flavor.

  4. If everything runs, you're ready to customize your configurations. For more information, seeCustomize configurations.

Launch a flavor

#

After you've created the product flavors for an Android app, you can launch a specific product flavor through Flutter.

You can launch a product flavor with the Flutter CLI using the following steps:

  1. Start an Android emulator or connect a physical device with developer options enabled.

  2. In the console, navigate to theflavors_example directory and enter the following command:

console
flutter (run | build <subcommand>) --flavor <flavor_name>
  • (run | build <subcommand>): Replace this with one of the following:

    • run: Runs the app in debug mode.
    • build: Builds either an APK or an appbundle.
      • <subcommand>: Eitherapk orappbundle.
  • <flavor_name>: Replace this with the name of your Android product flavor (for example:staging,production).

Example:

console
flutter build apk --flavor staging

Use flavors in Flutter code

#

After you've configured your product flavors, you can change your app's behavior—such as pointing to different API endpoints or changing the theme—based on the active flavor.

The Flutter framework provides theappFlavor constant, which retrieves the name of the current flavor as aString. This value matches the flavor name passed to the--flavor flag during theflutter run orflutter build process.

Access the current flavor

#
  1. Import the services library: To access theappFlavor constant, add the following import to your Dart file:

    dart
    import'package:flutter/services.dart';
  2. Check the flavor value: Use theappFlavor constant in your application logic (often inmain()) to handle flavor-specific configurations:

    dart
    voidmain(){// appFlavor will match the flavor name from build.gradle.ktsif(appFlavor=='production'){// Logic for production environmentConfig.apiUrl='https://api.flavors_example.com';}elseif(appFlavor=='staging'){// Logic for staging environmentConfig.apiUrl='https://staging.api.flavors_example.com';}runApp(constMyApp());}
    Note

    The value ofappFlavor matches the name of the product flavor you defined in yourbuild.gradle.kts file (for example,staging orproduction). If no flavor is specified during the build,appFlavor returnsnull.

After you've added product flavors, you can customize them for your Android app.

Create a distinct app display name

#

If you have multiple product flavors, a distinct app name can quickly identify which flavor your deployed app is using.

Distinct app names in menu

The following steps show how to add distinct app display names for two product flavors calledstaging andproduction in a project calledflavors_example.

  1. Updatebuild.gradle.kts in your IDE:

    • In theflavors_example project, navigate to theandroid/app/ directory and openbuild.gradle.kts.

    • In theflavorsDimension block, add aresValue() property calledapp_name to thestaging andproduction flavors:

      build.gradle.kts
      kotlin
      android{...flavorDimensions+="default"productFlavors{create("staging"){dimension="default"resValue(type="string",name="app_name",value="Flavors staging")applicationIdSuffix=".staging"}create("production"){dimension="default"resValue(type="string",name="app_name",value="Flavors production")applicationIdSuffix=".production"}}
  2. UpdateAndroidManifest.xml in your IDE:

    • In theflavors_example project, navigate toandroid/app/src/main and openAndroidManifest.xml.

    • Replace the value forandroid:label with@string/app_name.

      AndroidManifest.xml
      xml
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"><applicationandroid:label="@string/app_name".../>/>
  3. Launch the app for each product flavor (staging,production) and check to make sure that the app display name has changed for each.

    • To launch a product flavor, see the steps inLaunch a flavor.

    • In the Android App Emulator, go to the list of apps. You should see one forFlavors p... andFlavors s....

    • To see more information forFlavors p... orFlavors s..., long-press the icon for one of them and and selectApp info.

Create distinct icons

#

If you have multiple product flavors, a distinct icon for each configuration can help you quickly identify which flavor your deployed app is using.

Distinct icons

The following steps show how to add a distinct icon for two product flavors calledstaging andproduction in a project calledflavors_example.

  1. Prepare your icons:

    • Design yourstaging icon andproduction icon in the design tool of your choice.

    • Generate versions of thestaging icon andproduction icon in the following sizes and them inPNG format:

      • mipmap-mdpi (48x48 pixels)
      • mipmap-hdpi (72x72 pixels)
      • mipmap-xhdpi (96x96 pixels)
      • mipmap-xxhdpi (144x144 pixels)
      • mipmap-xxxhdpi (192x192 pixels)
    Note

    You can use a tool likeApp Icon Generator to generate the versions of your icons.

  2. Create flavor-specific resource directories:

    • Navigate to theandroid/app/src directory.

    • Create a directory calledstaging/res.

    • Navigate to thestaging/res directory.

    • Create the followingmipmap directories and move the versions of thestaging icon into them:

      • mipmap-mdpi/48x48_staging.png
      • mipmap-hdpi/72x72_staging.png
      • mipmap-xhdpi/96x96_staging.png
      • mipmap-xxhdpi/144x144_staging.png
      • mipmap-xxxhdpi/192x192_staging.png
    • Repeat the previous steps for theproduction flavor directories and icons.

    • Rename all of the icons toic_launcher.png.

  3. Double-check the configurations inAndroidManifest.xml in your IDE:

    • In theflavors_example project, navigate toandroid/app/src/main and openAndroidManifest.xml.

    • Make sure that the value forandroid:icon is@mipmap/ic_launcher.

  4. Launch the app for each product flavor (staging,production) and check to make sure that the app icon has changed for each. To launch a product flavor, see the steps inLaunch a flavor.

Bundle assets

#

If you have assets that are only used in a specific flavor in your app, you can configure them to only be bundled into your app when launching that flavor. This prevents your app bundle size from being bloated by unused assets. To bundle assets for each flavor, add theflavors subfield to theassets field in your project's pubspec. To learn more, see theassets field inFlutter pubspec options.

Set a default flavor

#

You can have your app use a specific flavor when you launch your app without specifying a flavor. To do this, you need to add thedefault-flavor field to your project's pubspec. To learn more, see thedefault-flavor field inFlutter pubspec options.

Add unique build settings

#

If you have additional build settings that you would like to configure for a specific Android product flavor, see Android'sConfigure build variants.

While it is possible to setabiFilters in product flavors, it is not recommended. Instead, favorabiFilters in build types. When settingabiFilters in product flavors, one must use the-Pdisable-abi-filtering flag when runningflutter build orflutter run.

More information

#

For more information on creating and using flavors, check out the following resources:

Was this page's content helpful?

Unless stated otherwise, the documentation on this site reflects Flutter 3.41.2. Page last updated on 2026-02-06.View source orreport an issue.


[8]ページ先頭

©2009-2026 Movatter.jp