Flutterでcloud_firestoreをpubspec.ymlに追加するとビルドエラーが発生する場合の対処方法(.dex file cannot exceed 64K.)

# 遭遇した問題

最近Flutterも初めてわからないなりにFirebaseと合わせてFirestoreを使用しようとしたところ、下記のエラーが発生しました。

Flutterでcloud_firestoreを追加するとビルドエラーが発生する場合の対処方法

```
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/0.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/1.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/4.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/5.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/6.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/7.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/8.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/9.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/10.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/11.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/12.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/13.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/14.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/15.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/16.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/17.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/18.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/19.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/20.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/21.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/22.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/23.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/24.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/25.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/26.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/27.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/28.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/29.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/30.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/31.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/32.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/33.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/34.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/35.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/36.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/37.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/38.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/39.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/40.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/41.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/42.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/43.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/44.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/45.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/46.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/47.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/48.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/49.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/50.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/51.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/52.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/53.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/54.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/55.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/56.jar
 /Volumes/workspace/flutter_fcm/client/build/app/intermediates/transforms/dexBuilder/debug/57.jar
  The number of method references in a .dex file cannot exceed 64K.
  Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

* 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 18s
Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
```

**長い:innocent:**

# 原因

原因はエラーの中にある`The number of method references in a .dex file cannot exceed 64K.`がポイントでした。
Android アプリ（APK）ファイルには、Dalvik Executable（DEX）形式の実行可能なバイトコード ファイルが含まれており、その中にアプリの実行に使用されるコンパイル済みのコードがあります。
Dalvik Executable の仕様により、単一の DEX ファイル内で参照できるメソッドの総数が 65,536 に制限されます。
これらのメソッドには、Android フレームワーク メソッド、ライブラリ メソッド、独自のコードのメソッドが含まれます。コンピュータ サイエンスにおいては、Kilo、K は 1024（または、2^10）を表します。65,536 は 64 X 1024 に等しいため、この制限は「64K 参照制限」と呼ばれます。
引用：[64K を超えるメソッドを使用するアプリの設定](https://developer.android.com/studio/build/multidex)

今回はcloud_firestoreを追加することで、64K参照制限を超過してしまったため、このエラーが発生してしまったようです。

# 対処方法

対処方法は簡単で、Androidのアプリレベルのbuild.gradleに``を追加するのみです。

```gradle:/android/app/build.gradle
android {
  :
    defaultConfig {
         :
        multiDexEnabled true
    }
}
```

念の為`gradle clean`もしてから`flutter run`を実行すると無事に起動することができました！

# まとめ

Flutter楽しいですね:relaxed: MaterialDesignであれば簡単に作成出来るのでハイブリッドはこれが良さそうな気がします。
ただ、やはりといいますか今回の様にAndroid/iOSに依存する問題に引っかかった場合にどこが問題なのか切り分けるのが大変なので、kotlinとswiftもそろそろ勉強すべきな気がしてきました。(やるなら昔javaもしてたのでkotlinかな？)

## 参照

[64K を超えるメソッドを使用するアプリの設定](https://developer.android.com/studio/build/multidex)
[Cloud Firestore : DexArchiveMergerException Unable to merge dex
](https://stackoverflow.com/questions/47300681/cloud-firestore-dexarchivemergerexception-unable-to-merge-dex?newreg=53a3d681d1d2466291b779347b1509ca)