【 Flutter 】エラー: The number of method references in a .dex file cannot exceed 64K.

Flutterでぶちあったメモです。今回は、apkファイルビルド時に The number of method references in a .dex file cannot exceed 64K とか言われてビルド失敗しました。

現象


FAILURE: Build failed with an exception.                                
                                                                        
* What went wrong:                                                      
  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 15s   

解決法

モデュールレベルのbuild.gradle(android/app/build.gradle)に↓を追記。


android {
    ....

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        //これを追記↓
        multiDexEnabled true 
    }
    ....
}

原因

どうやら、Androidアプリが参照しているメソッドが64K(65536)を超えてしまうと、ビルドエラーになってしまうらしい。↑の設定をするとそれを回避できる。

参考: https://developer.android.com/studio/build/multidex?hl=ja