How do I build different versions of my Flutter app for qa/dev/prod?

后端 未结 9 1580
清歌不尽
清歌不尽 2020-12-01 00:14

I am building a Flutter app, and I have variables with different values for different environments (QA, dev, prod, etc). What\'s a good way to organize my app so I can easil

相关标签:
9条回答
  • 2020-12-01 00:58

    The cleaner way to do this is through Build Flavors.

    As a quick example, if you want to have a different app id for your app for your "dev" build, you could have this in the gradle file:

    flavorDimensions "version"
    productFlavors {
        dev {
            applicationIdSuffix ".dev"
            versionNameSuffix "-dev"
        }
    }
    

    Read more about gradle build variant configuration here.


    Now you can run with this build variant with command line:

    flutter run --flavor dev
    

    If you are using Android Studio, you can set the build variant in the Run configuration too:

    Read more about iOS configuration on this blog. And official flutter documentation about build flavours.

    0 讨论(0)
  • 2020-12-01 01:06

    One way to do it: create different main_<environment>.dart files in the lib/ directory of your project.

    Each main_<environment>.dart contains the environment-specific configurations/values (such as the different database names, etc). Each main_<environment>.dart then imports the actual application library and runs the application, passing in the environment's values/configurations.

    Then, choose which .dart file to build: flutter run -t lib/main_debug.dart

    0 讨论(0)
  • 2020-12-01 01:06
    import 'package:flutter/foundation.dart';
    

    And use following const values:

    if (kReleaseMode) {
      // App is running in release mode. 
    } else if (kProfileMode) {
      // App is running in profile mode.
    } else if (kDebugMode) {
      // App is running in debug mode.
    } else if (kIsWeb) {
      // App is running on the web.
    }
    
    0 讨论(0)
提交回复
热议问题