LoginSignup
22
14

More than 1 year has passed since last update.

dart-defineでFlutterアプリのFirebase開発環境と本番環境を使い分ける Android編

Last updated at Posted at 2020-08-04

dart-defineでFlutterアプリのFirebase開発環境と本番環境を使い分ける iOS編のAndroid版です。dart-define自体の説明やflavorなどとの比較に関してはこちらの記事に書きましたので、こちらも参照していただければと思います。

本記事では、dart-defineで環境変数を設定することでFirebaseの開発用と本番用プロジェクトを使い分ける方法をご紹介します。また、dart-defineを使った方法は、flavorと同様、3種類以上のプロジェクトを使い分けるように拡張することも可能です。

Firebaseプロジェクトの作成、ファイル配置

まず、Firebaseプロジェクトは以下のように作成しましょう。

  • 開発用: com.example.test.dev
  • 本番用: com.example.test

次に、以下のようにFirebaseファイルを配置します。
- android/app/src/dev/google-services.json (開発環境用Firebase設定ファイル)
- android/app/src/prod/google-services.json (本番環境用Firebase設定ファイル)

ビルド設定

続いて、android/app/build.gradle を以下のように編集します。このように設定することで、dart-defineを使って環境変数に応じて以下の2つのことができるようになります。

  • Firebaseプロジェクトの使い分け
  • アプリ名の使い分け

Flutter 1.17系の場合

/*
略
*/
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

// 環境変数の読み込み
def dartEnvironmentVariables = [
        APP_SUFFIX: null,
        BUILD_ENV: ''
];
if (project.hasProperty('dart-defines')) {
    dartEnvironmentVariables = dartEnvironmentVariables + project.property('dart-defines')
            .split(',')
            .collectEntries { entry ->
                def pair = entry.split('=')
                [(pair.first()): pair.last()]
            }
}

// firebaseファイルをコピーして android/app ディレクトリに移動
task copyGoogleServicesJson(type: Copy) {
    from "src/${dartEnvironmentVariables.BUILD_ENV}/google-services.json"
    into './'
}

// firebaseファイルのコピータスクを待ってからファイルが必要な処理を行うよう設定
tasks.whenTaskAdded { task ->
    if (task.name == 'processDebugGoogleServices') {
        task.dependsOn copyGoogleServicesJson
    }
    if (task.name == 'processReleaseGoogleServices') {
        task.dependsOn copyGoogleServicesJson
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'

// 環境変数でアプリidを変更
android {
    defaultConfig {
        applicationId "com.example.test"
        applicationIdSuffix dartEnvironmentVariables.APP_SUFFIX
        minSdkVersion 23
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
/*
略
*/

Flutter 1.20系の場合

/*
略
*/
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

// 環境変数の読み込み
def dartEnvironmentVariables = [
        APP_SUFFIX: null,
        BUILD_ENV: ''
];
if (project.hasProperty('dart-defines')) {
    dartEnvironmentVariables = dartEnvironmentVariables + project.property('dart-defines')
            .split(',')
            .collectEntries { entry ->
                def pair = URLDecoder.decode(entry).split('=')
                [(pair.first()): pair.last()]
            }
}

// firebaseファイルをコピーして android/app ディレクトリに移動
task copyGoogleServicesJson(type: Copy) {
    from "src/${dartEnvironmentVariables.BUILD_ENV}/google-services.json"
    into './'
}

// firebaseファイルのコピータスクを待ってからファイルが必要な処理を行うよう設定
tasks.whenTaskAdded { task ->
    if (task.name == 'processDebugGoogleServices') {
        task.dependsOn copyGoogleServicesJson
    }
    if (task.name == 'processReleaseGoogleServices') {
        task.dependsOn copyGoogleServicesJson
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'

// 環境変数でアプリidを変更
android {
    defaultConfig {
        applicationId "com.example.test"
        applicationIdSuffix dartEnvironmentVariables.APP_SUFFIX
        minSdkVersion 23
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

/*
略
*/

Flutter 2.2系の場合

「Flutter 1.20系の場合」の

                def pair = URLDecoder.decode(entry).split('=')

                def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')

に置き換えてください。

ビルド

$ flutter build apk --debug --dart-define=BUNDLE_ID_SUFFIX=Dev --dart-define=BUILD_ENV=dev --dart-define=APP_SUFFIX=dev

本番環境では、 --dart-define=BUILD_ENV=prod--dart-define=APP_SUFFIXはパラメータごと消去してください。

参考

22
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
14