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)



원인



원인은 오류 속에 있는 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 이상의 메소드를 사용하는 앱 설정

이번에는 cloud_firestore를 추가하여 64K 참조 제한을 초과했기 때문에이 오류가 발생한 것 같습니다.

해결 방법



해결 방법은 간단하며 Android의 앱 수준 build.gradle에 ``를 추가하기 만하면됩니다.

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

만약을 위해 gradle clean 혹시 flutter run 를 실행하면 무사히 기동할 수 있었습니다!

요약



Flutter 재미있네요 MaterialDesign이면 간단하게 작성할 수 있으므로 하이브리드는 이것이 좋을 것 같은 생각이 듭니다.
단지, 역시라고 할까 이번과 같이 Android/iOS에 의존하는 문제에 걸렸을 경우에 어디가 문제인가 구분하는 것이 힘들기 때문에, kotlin과 swift도 슬슬 공부해야 할 생각이 되어 왔습니다. (한다면 옛날 자바도 했기 때문에 kotlin일까?)

참조



64K 이상의 메소드를 사용하는 앱 설정
Cloud Firestore : DexArchiveMergerException Unable to merge dex

좋은 웹페이지 즐겨찾기