Android 给出错误 "Cannot fit requested classes in a single dex file"

Android gives error "Cannot fit requested classes in a single dex file"

我不知道为什么,但今天早上无法在我的手机上启动我的应用程序。我收到此错误消息:

Cannot fit requested classes in a single dex file. Try supplying a main-dex list.

# methods: 68061 > 65536 Message{kind=ERROR, text=Cannot fit requested classes in a single dex file. Try supplying a main-dex list.

# methods: 68061 > 65536, sources=[Unknown source file], tool

我真的是 Android 的新手,我不明白这个问题以及我需要做什么?为什么我现在遇到这个问题而不是以前?

在根 build.gradle 文件中执行如下操作:

dependencies {

    // ...

    implementation 'androidx.multidex:multidex:2.0.1'
}

android {
    defaultConfig {

        // ...

        multiDexEnabled true
    }
}

此处有更多详细信息:Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536

Multidex 并不总是解决问题的方法,它确实会生成更多的 dex 文件以适应您的方法数量,但请确保不要导入您需要的更多方法,因为从长远来看这将使您的构建比以前慢了。

例如,如果您只需要使用播放服务中的位置库,则有两种选择

第一个是实施位置随附的整个播放服务库

implementation 'com.google.android.gms:play-services:11.8.0'

这些整个库可能有超过 40.000 种方法(只是一个估计,我真的不知道总数),接近达到 65536 种方法的限制。

相反,您应该只针对您将使用的库而不是整个库包

在这种情况下

implementation 'com.google.android.gms:play-services-location:11.8.0'

可能只有 50 - 100 个方法可以使用,这在构建时比从整个库包中加载一大堆你永远不会使用的方法要好。

这只是避免获得

的提示

Cannot fit requested classes in a single dex file.

for minSdkVersion 以上 android 5.0 API 20 +

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.

如果您的目标是较低的设备 (Android 4.1 API 16) 或 Android 5 (API 20)

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.

在最后一种情况下您将需要使用 multidex

在 build.gradle(app) 文件中: 在依赖项中添加以下内容:

 implementation 'com.android.support:multidex:1.0.3'

并在defaultConfig中添加如下内容,

multiDexEnabled true

希望您能找到解决方案。

运行 RN 0.62,这对我来说很成功。

更新build.gradle

classpath('com.android.support:multidex:1.0.3')

更新默认配置

multiDexEnabled true

完成这两项更改后,重新启动 gradle 构建。

android x

的推荐解决方案

为什么会出错

当您的应用及其引用的库超过 65,536 个方法时,您会遇到构建错误,表明您的应用已达到 Android 构建架构的限制 more information here

解决方案

只需将多 dex 支持库添加到 buid.gradle (Module: app) 文件中的依赖项

dependencies {
    def multidex_version = "2.0.1"
    implementation 'androidx.multidex:multidex:$multidex_version'
}
   

当您出于相同目的使用 deprecated npmcorrect npm 时会出现此错误

更新 2021 Android-X 和 Android Studio 4.XX:

在应用级别的默认配置中添加 multiDexEnabled Build.Gradle 就足够了

multiDexEnabled 真

参考图片

您可以按照以下步骤解决此问题。

步骤 1:迁移到 AndroidX

重构 > 迁移到 AndroidX

步骤 2: 在 build.gradle 文件中添加此依赖项:

implementation 'androidx.multidex:multidex:2.0.1'

步骤 3: 在 build.gradle 的 defaultConfig 部分添加:

multiDexEnabled true