31

I am trying to set up Android flavors with flutter.

I have two entry points.

lib/main_prod.dart
lib/main_dev.dart

I have also added the following to my gradle file

flavorDimensions "version"
productFlavors {
    prod {
        dimension "version"
        applicationIdSuffix ".prod"
    }
    dev {
        dimension "version"
        applicationIdSuffix ".dev"
        versionNameSuffix " Dev"
    }
}

Now I am trying to generate two separate apk which I can use to publish to the Google Play Store.

enter image description here

I go on Android Studio and try generate these.

However I get this error.

enter image description here

Android Studio is still trying to look for the main.dart file, however, for my two flavors they are main_prod.dart and main_dev.dart. How do I tell Android Studio to look for the approperiate main file depending on the flavor im trying to get the apk file for.

Just_Ice
  • 523
  • 1
  • 6
  • 12

3 Answers3

105

To build apk or appbundle(to publish to the playstore) for each flavor you can use the commands(recommended) instead of using android studio.

For prod you can use:

APK

flutter build apk --flavor prod -t lib/main_prod.dart

App Bundle

flutter build appbundle --flavor prod -t lib/main_prod.dart

and for dev

APK

flutter build apk --flavor dev -t lib/main_dev.dart

App Bundle

flutter build appbundle --flavor dev -t lib/main_dev.dart
JideGuru
  • 7,102
  • 6
  • 26
  • 48
5

The above command will work for release builds, but for the debug builds, you may need to use this command:

flutter build apk -t lib/main_dev.dart --flavor=dev --debug

Otherwise, it may throw this error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:validateSigningDevRelease'.
> Keystore file not set for signing config release

* 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 5s
Running Gradle task 'assembleDevRelease'...                     6.6s
Gradle task assembleDevRelease failed with exit code 1
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
3

Simply for prod environment flutter build apk -t lib/main_prod.dart --release for release apk and flutter build apk -t lib/main_prod.dart --debug for debug apk should work.

Fahmida
  • 1,050
  • 8
  • 19