Flutter_firebase 連携時にAndroidでビルドエラーが発生したメモ


```yaml:pubspec.yml
dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  firebase_core: ^0.4.0+9
  cloud_firestore: ^0.12.9+5
  provider: ^4.1.2
```


FlutterチュートリアルをもとにFirebaseとの連携を行なっていたところ
Androidでビルド時に以下のエラーが発生した

```console:エラー内容
①
Plugin project :firebase_core_web not found. Please update settings.gradle.

FAILURE: Build failed with an exception.

②
* What went wrong:
Execution failed for task ':app:processDebugGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it. 
   Searched Location: 
  /Users/key/AndroidStudioProjects/test_firebase/android/app/src/debug/google-services.json
  /Users/key/AndroidStudioProjects/test_firebase/android/app/src/google-services.json
  /Users/key/AndroidStudioProjects/test_firebase/android/app/src/Debug/google-services.json
  /Users/key/AndroidStudioProjects/test_firebase/android/app/google-services.json


* 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 2s
Exception: Gradle task assembleDebug failed with exit code 1

```


<h1>エラー①</h1>

```console:エラー内容①
Plugin project :firebase_core_web not found. Please update settings.gradle.
```

[flutter appのルート] / android / settings.gradle に以下の内容を追加し解消

```gradle:settings.gradle
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()

def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
    pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}

plugins.each { name, path ->
    def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
    include ":$name"
    project(":$name").projectDir = pluginDirectory
}
```

参考：
https://github.com/FirebaseExtended/flutterfire/issues/2599

<h1>エラー②</h1>

```console:エラー内容②
* What went wrong:
Execution failed for task ':app:processDebugGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it. 
   Searched Location: 
  /Users/key/AndroidStudioProjects/test_firebase/android/app/src/debug/google-services.json
  /Users/key/AndroidStudioProjects/test_firebase/android/app/src/google-services.json
  /Users/key/AndroidStudioProjects/test_firebase/android/app/src/Debug/google-services.json
  /Users/key/AndroidStudioProjects/test_firebase/android/app/google-services.json
```

(1)https://console.firebase.google.com/ にアクセスして対象のプロジェクトを開く
(2)左のリストの歯車アイコンをクリックし「プロジェクトを設定」を選択する
(3)マイアプリ→Androidアプリ→google.service.jsonをダウンロードする
(4)ダウンロードしたファイルを以下のディレクトリに配置
   [flutter appのルート] / android / app / google.service.json ←

参考：
https://stackoverflow.com/questions/33866061/error-file-google-services-json-is-missing-from-module-root-folder-the-google

上記２つのエラーは解消したが新しいエラーが発生！

<h1>エラー③</h1>

```console:エラー内容③
D8: Cannot fit requested classes in a single dex file (# methods: 84767 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
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
	at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:132)
	at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:119)
	at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:102)
	at com.android.build.gradle.internal.tasks.DexMergingTaskRunnable.run(DexMergingTask.kt:432)
	at com.android.build.gradle.internal.tasks.Workers$ActionFacade.run(Workers.kt:242)
	at org.gradle.workers.internal.AdapterWorkAction.execute(AdapterWorkAction.java:57)
	at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
	at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:67)
	at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:63)
	at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)
	at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:63)
	at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
	at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:200)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:195)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:75)
	at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:68)
	at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:153)
	at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:68)
	at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:62)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.lambda$call$2(DefaultBuildOperationExecutor.java:76)
	at org.gradle.internal.operations.UnmanagedBuildOperationWrapper.callWithUnmanagedSupport(UnmanagedBuildOperationWrapper.java:54)
	at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:76)
	at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
	at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:60)
	at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:200)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:215)
	at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164)
	at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:131)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
	at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
	at java.lang.Thread.run(Thread.java:748)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, position: null
	at Version.fakeStackEntry(Version_2.1.62.java:0)
	at com.android.tools.r8.utils.Y.a(SourceFile:78)
	at com.android.tools.r8.D8.run(D8.java:11)
	at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:117)
	... 36 more
Caused by: com.android.tools.r8.utils.b: Cannot fit requested classes in a single dex file (# methods: 84767 > 65536)
	at com.android.tools.r8.utils.T0.error(SourceFile:1)
	at com.android.tools.r8.utils.T0.a(SourceFile:2)
	at com.android.tools.r8.dex.P.a(SourceFile:740)
	at com.android.tools.r8.dex.P$h.a(SourceFile:7)
	at com.android.tools.r8.dex.b.a(SourceFile:14)
	at com.android.tools.r8.dex.b.b(SourceFile:25)
	at com.android.tools.r8.D8.d(D8.java:133)
	at com.android.tools.r8.D8.b(D8.java:1)
	at com.android.tools.r8.utils.Y.a(SourceFile:36)
	... 38 more


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDexDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
     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 4s
[!] The shrinker may have failed to optimize the Java bytecode.
    To disable the shrinker, pass the `--no-shrink` flag to this command.
    To learn more, see: https://developer.android.com/studio/build/shrink-code
Exception: Gradle task assembleDebug failed with exit code 1
```

モジュールレベルのbuild.gradleの設定で、minSdkVersion が16になっていたのでこれを21に変更
[flutter appのルート] / android / app / build.gradle

```gradle:build.gradle
   defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.test_firebase"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }
```

参考：
https://developer.android.com/studio/build/multidex


無事エラーなくビルドが完了しました。