为什么添加谷歌的火力点引起Dex错误?

添加

实现“com.google.firebase:firebase-core:11.8.0”

到我的模块gradle文件给我错误提示我有超过64K方法在我的项目。

我的猜测是,我以某种方式实现了整个谷歌播放服务,虽然增加

实现’com.google.android.gms:play-services-maps:11.8.0′

要么

实现’com.google.android.gms:play-services-location:11.8.0′

给我没有错误。

格拉德尔:

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "se.workshop.collect" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation 'uk.co.chrisjenx:calligraphy:2.3.0' implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation 'com.android.support:support-v4:26.1.0' implementation 'com.android.support:recyclerview-v7:26.1.0' implementation 'uk.co.chrisjenx:calligraphy:2.3.0' implementation 'com.google.android.gms:play-services-maps:11.8.0' implementation 'com.google.android.gms:play-services-location:11.8.0' implementation 'com.google.firebase:firebase-core:11.8.0' implementation "com.android.support:recyclerview-v7:26.1.0" //Recycler View Support implementation "com.android.support:cardview-v7:26.1.0" //Recycler View Support implementation 'com.squareup.okhttp3:okhttp:3.9.0' implementation 'com.google.code.gson:gson:2.8.2' implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0" implementation 'com.karumi:dexter:4.2.0' implementation 'com.android.support:design:26.1.0' implementation 'com.android.support:support-vector-drawable:26.1.0' implementation group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version:"$kotlin_version" testImplementation 'junit:junit:4.12' androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', { exclude group: 'com.android.support', module: 'support-annotations' }) } 

和错误:

 Error: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 Error:com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 Error: at com.android.dx.merge.DexMerger$8.updateIndex(DexMerger.java:565) Error: at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:276) Error: at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:574) Error: at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:166) Error: at com.android.dx.merge.DexMerger.merge(DexMerger.java:198) Error: at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61) Error: at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36) Error: at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424) Error: at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) Error: at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) Error: at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) Error: at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) Error:Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'. > com.android.build.api.transform.TransformException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 Information:BUILD FAILED in 13s 

在这个主题中提到的问题的简单答案是,您添加的依赖关系只会将方法的总数推到64K以上,就像您在错误消息中看到的一样。 这听起来像你认为它不应该,但显然是这样。 请记住,firebase-core还引入了firebase-analyics和一些其他库,所以当你添加它时,你实际上不仅仅是firebase-core。

无论如何,你应该在你的版本中启用multidex来防止这个错误,因为你添加了更多的库。