Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutterでstaging/production環境を切替える

 Flutterでstaging/production環境を切替える

Flutter Meetup Tokyo #1 のLT資料です

FURUKI Eiji

April 18, 2018
Tweet

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • @fullfool / FURUKI Eiji • iOS / Android

    ΤϯδχΞ • ϞΠגࣜձࣾ • Flutterྺɹࡢ೥8݄த०͔Β • ࠷ۙFlutter৮ͬͯͳ͍
  2. FlavorΛࢦఆͯ͠ΞϓϦΛ࣮ߦ $ flutter run --flavor staging $ flutter run --flavor

    production `--flavor` ΦϓγϣϯͰFlavorΛࢦఆ͢Δ flavorʹࢦఆ͢Δ஋͸Platformଆ(Android/iOS)ͰઃఆࡁΈͷ஋͕ࢦఆͰ ͖·͢
 ͦΕͧΕͷPlatformͰͷFlavorͷఆٛ͸εϥΠυ຤ඌʹ
  3. DartͰFlavorΛऔಘ void main() { const String flavor = const String.fromEnvironment('flavor');

    configure(flavor); runApp(new MyApp()); } const bool isReleaseMode = const bool.fromEnvironment('dart.vm.product'); ىಈ࣌ʹFlavorΛऔಘ͍ͨ͠ releaseϞʔυΛऔಘ͢Δ࣌
  4. DartͰFlavorΛऔಘ void main() { const String flavor = const String.fromEnvironment('flavor');

    configure(flavor); runApp(new MyApp()); } const bool isReleaseMode = const bool.fromEnvironment('dart.vm.product'); ىಈ࣌ʹFlavorΛऔಘ͍ͨ͠ releaseϞʔυΛऔಘ͢Δ࣌
  5. MethodChannelͷొ࿥(Android) new MethodChannel(getFlutterView(), "flavor") .setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void

    onMethodCall(MethodCall methodCall, MethodChannel.Result result) { result.success(BuildConfig.FLAVOR); } }); ΞϓϦىಈ࣌ʹMethodChannelΛొ࿥͢Δ ʢ͜ͷchannel͸Flavorऔಘઐ༻ͷͨΊMethodCall͸ࢀর͠ͳ͍ʣ
  6. MethodChannelͷొ࿥(Android) new MethodChannel(getFlutterView(), "flavor") .setMethodCallHandler(new MethodChannel.MethodCallHandler() { @Override public void

    onMethodCall(MethodCall methodCall, MethodChannel.Result result) { result.success(BuildConfig.FLAVOR); } }); ProductFlavor(BuildConfig.FLAVOR)Λฦ͢
  7. MethodChannelͷొ࿥(iOS) FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController; FlutterMethodChannel* flavorChannel = [FlutterMethodChannel methodChannelWithName:@"flavor"

    binaryMessenger:controller]; [flavorChannel setMethodCallHandler:^(FlutterMethodCall *call, FlutterResult result) { NSString* flavor = (NSString*)[[NSBundle mainBundle] .infoDictionary valueForKey:@"Flavor"]; result(flavor); }]; Bundleʹఆٛ͢ΔSchemeʹؔ࿈෇͚ͨFlavorΛฦ͢
  8. MethodChannelͰFlavorऔಘ void main() { _run(); } Future _run() async {

    var flavor = await MethodChannel('flavor').invokeMethod('getFlavor'); configure(flavor); runApp(new MyApp()); } main.dartͰFlavorΛऔಘͯ͠ɺઃఆΛॳظԽ
  9. FlavorຖͷEntry-Point • main.dart͸ "production"ͰઃఆॳظԽ void main() { configure("staging"); runApp(new MyApp());

    } void main() { configure("production"); runApp(new MyApp()); } • main_staging.dart͸ "staging"ͰઃఆॳظԽ
  10. PlatformͰͷFlavorఆٛ(Android) android { ... defaultConfig {...} buildTypes {...} flavorDimensions "api"

    productFlavors { staging { dimension "api" applicationIdSuffix ".staging" manifestPlaceholders = [appName: "Casmo dev"] } production { dimension "api" manifestPlaceholders = [appName: "Casmo"] } } } ௨ৗͷAndroidΞϓϦͷProductFlavorsͱಉ༷
  11. Firebaseͷ੾ସ(iOS) GoogleService-Info-$flavor.plist Λىಈ࣌ʹಡ Έସ͑Δ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSString *flavor = (NSString*)[[NSBundle mainBundle] .infoDictionary valueForKey:@"Flavor"]; NSString *firebaseInfo = [NSString stringWithFormat: @"GoogleService-Info-%@", flavor]; NSString *path = [[NSBundle mainBundle] pathForResource:firebaseInfo ofType:@"plist"]; FIROptions* firOptions = [[FIROptions alloc] initWithContentsOfFile:path]; if (firOptions == nil) { assert("Couldn't load config file"); } [FIRApp configureWithOptions:firOptions];
  12. Fastlane+Beta(Android) lane :beta do |options| flavor = (options[:prod] ? "production"

    : "staging") entry_dart = (options[:prod] ? "lib/main.dart" : "lib/main_staging.dart") sh("cd ../.. && flutter build apk --flavor #{flavor} -t #{entry_dart} --release && cd -") changelog_from_git_commits # upload to Beta crashlytics( ... ) slack( ... ) end flutter buildͰ؀ڥΛ੾Γସ͑ͯϏϧυ͢Δ
  13. Fastlane+Beta(iOS) lane :beta do |options| scheme = (options[:prod] ? "Production"

    : "Staging") flavor = (options[:prod] ? "production" : "staging") entry_dart = (options[:prod] ? "lib/main.dart" : "lib/main_staging.dart") sh("cd ../.. && flutter build ios --flavor #{flavor} -t #{entry_dart} --release && cd -") gym( scheme: scheme, configuration: "Release #{scheme}", export_method: "ad-hoc" ) changelog_from_git_commits # upload to Beta crashlytics( ... ) slack( ... ) end flutter buildͰ؀ڥΛ੾Γସ͑ͯϏϧυ͢Δ