【问题标题】:Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex list. # methods: 72477 > 65536错误:无法在单个 dex 文件中容纳请求的类。请尝试提供 main-dex 列表。 # 方法:72477 > 65536
【发布时间】:2018-06-23 07:05:36
【问题描述】:

我想添加融合定位服务,但它显示了一些错误。 帮我。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.1"
    defaultConfig {
        applicationId "com.example.adil.bloodbankapplication"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-auth:11.8.0'
    compile 'com.google.firebase:firebase-database:11.8.0'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'junit:junit:4.12'
    compile 'com.android.support:design:26.1.0'
    compile 'com.github.joielechong:countrycodepicker:2.1.5'
    compile 'com.jaredrummler:material-spinner:1.2.4'
    compile 'hanks.xyz:htextview-library:0.1.5'
    compile 'com.firebaseui:firebase-ui-database:1.2.0'
    compile 'com.google.android.gms:play-services:11.8.0'
}


apply plugin: 'com.google.gms.google-services'

【问题讨论】:

    标签: android android-multidex


    【解决方案1】:

    他们给你的答案都不是详尽无遗的。 问题在于Multidex。 您必须在应用程序 gradle 中添加库:

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

    之后,添加app gradle的defaultConfig:

    multiDexEnabled true
    

    您的应用程序必须是 Multidex 类型。 你必须把它写在清单中:

    android:name=".MyApplication"
    

    “MyApplication”必须是 Multidex 类,或者它必须扩展它。

    【讨论】:

    • 那么MyApplication extends android.support.multidex.MultiDexApplication ?
    • 我已经尝试过这个解决方案,但我最终遇到了这个新问题:stackoverflow.com/questions/49518115/…
    • 这里是官方的 android 文档页面,提供了关于为什么会发生这种情况以及如何制作更好的发布版本 apk 的更多解释:developer.android.com/studio/build/multidex
    • 如果谷歌库没有 80000 个方法,那么没有 80000 个方法将是一个很好的解决方案(他们添加 multidex 的全部原因是因为他们希望每个人都使用他们自己的服务库有数以万计的方法)。
    • 如果您的 minSdkVersion 为 21 或更高,则不需要 multidex 支持库。
    【解决方案2】:

    我通过以下解决方案解决了我的问题:

    在 Gradle 构建文件中,添加依赖:

    实现'com.android.support:multidex:1.0.3'

    然后在“defaultConfig”部分,添加:

    multiDexEnabled 真

    【讨论】:

    • 为我工作。详细配置说明:developer.android.com/studio/build/multidex#mdex-gradle
    • 在大多数应用程序中不需要启用多索引支持。您应该考虑删除未使用的库,请参阅下面的答案。
    • 我想通过减少方法的数量来避免在我的 react-native 应用程序中使用 multidex,所以我使用github.com/mihaip/dex-method-counts 来测量它们。原来 facebook 的方法单独是 27k+,但我必须有 Fb 等几乎没有方法计数是我的代码,所以 multidex 是必须的
    【解决方案3】:

    修改您的应用或模块的build.gradle

    android {
        defaultConfig {
            ...
            minSdkVersion 21 <----- *here
            targetSdkVersion 26
            multiDexEnabled true <------ *here
        }
        ...
    }
    

    根据官方文档

    对 Android 5.0 及更高版本的 Multidex 支持

    Android 5.0(API 级别 21) 及更高版本使用称为 ART 的运行时,它本机支持加载 来自 APK 文件的多个 DEX 文件。 ART 在应用程序执行预编译 install time 扫描 classesN.dex 文件并将它们编译成 由 Android 设备执行的单个 .oat 文件。因此,如果 您的 minSdkVersion 为 21 或更高,您不需要 multidex 支持库。

    有关 Android 5.0 运行时的更多信息,请阅读 ART 和 Dalvik。

    https://developer.android.com/studio/build/multidex

    【讨论】:

    • 如果您将最低版本升级到 21 我认为没有必要添加行 multiDexEnabled true
    【解决方案4】:

    什么是 dex 文件: Android 应用 (APK) 文件包含 Dalvik 可执行 (DEX) 文件形式的可执行字节码文件,其中包含用于运行您的应用的编译代码。

    出现此异常的原因: DEX 规范将单个 DEX 文件中可以引用的方法总数限制为 65,536(64K 引用限制)——包括 Android 框架方法、库方法和您自己的代码中的方法。

    步骤01.添加如下依赖如下

    对于非 Androidx 用户,

    dependencies {
      implementation 'com.android.support:multidex:1.0.3'
    }
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true  //ADD THIS LINE
    }
    

    对于 Androidx 用户,

    dependencies {
      implementation 'androidx.multidex:multidex:2.0.1'
    }
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 28
            multiDexEnabled true  //ADD THIS LINE
        }
    

    步骤 02:

    在添加这些 Sync 项目之后和运行项目之前,请确保在运行之前构建项目。 否则,你会得到一个异常。

    【讨论】:

      【解决方案5】:

      你可以关注this

      Android 5.0(API 级别 21)之前的平台版本使用 Dalvik 运行时来执行应用代码。默认情况下,Dalvik 将应用程序限制为每个 APK 单个 classes.dex 字节码文件。为了绕过这个限制,您可以将 multidex 支持库添加到您的项目中:

      dependencies {
        implementation 'com.android.support:multidex:1.0.3'
      }
      

      如果您的 minSdkVersion 设置为 21 或更高,您只需在模块级 build.gradle 文件中将 multiDexEnabled 设置为 true,如下所示:

      android {
          defaultConfig {
              ...
              minSdkVersion 21 
              targetSdkVersion 28
              multiDexEnabled true
          }
          ...
      }
      

      【讨论】:

        【解决方案6】:

        当您使用依赖项时,您的方法会增加! 谷歌有一个解决方案。那个叫Multidex!

        注意:请确保 min SDK 超过 14。

        在您的 build.gradle 中:

        android {
            defaultConfig {
                ...
                minSdkVersion 15 
                targetSdkVersion 28
                multiDexEnabled true
            }
            ...
        }
        
        
        
          dependencies {
              implementation 'com.android.support:multidex:1.0.3'
            }
        

        适用于 androidX:

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

        欲了解更多信息,请访问原始链接:

        https://developer.android.com/studio/build/multidex

        【讨论】:

        • 很好的解决方案!解决了我的问题
        【解决方案7】:

        你好问题在这里删除它

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

        为什么?

        注意:不要使用组合播放服务目标。它引入了数十个库,使您的应用程序膨胀。相反,请仅指定您的应用使用的特定 Google Play 服务 API。

        然后使用你需要的东西。https://developers.google.com/android/guides/setup

        喜欢定位服务

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

        对于云消息传递

        com.google.android.gms:play-services-gcm:11.8.0
        

        【讨论】:

        • 是的,现在我遇到了我的运行按钮已禁用的问题。
        【解决方案8】:

        如果您正在构建 DEBUG APK,只需添加:

        debug {
                    multiDexEnabled true
                }
        

        buildTypes

        如果您正在构建 RELEASE APK,请添加 multiDexEnabled true 在发布块中作为-

        release {
                        ...
                        multiDexEnabled true
                        ...
                }
        

        【讨论】:

        • 打开 project/app/build.gradle 并添加以下行。 defaultConfig { ... multiDexEnabled true }
        【解决方案9】:

        对于flutter项目你也可以解决这个问题

        打开你的\android\app\build.gradle

        你应该有以下几点:

        android {
            defaultConfig {
                ...
                minSdkVersion 16
                targetSdkVersion 28
        
            }
        
        }
        

        如果您的 minSdkVersion 设置为 21 或更高,您只需在 defaultConfig 中将 multiDexEnabled 设置为 true。像这样

        android {
            defaultConfig {
                ...
                minSdkVersion 16
                targetSdkVersion 28
                multiDexEnabled true 
        
            }
        
        }
        

        如果您的 minSdkVersion 设置为 20 或更低,请将 multiDexEnabled true 添加到 defaultConfig

        并定义实现

        dependencies {
          implementation 'com.android.support:multidex:1.0.3'// or 2.0.1
        }
        

        最后你应该有:

        android {
            defaultConfig {
                ...
                minSdkVersion 16
                targetSdkVersion 28
                multiDexEnabled true  // added this
            }
        
        }
        
        dependencies {
          implementation 'com.android.support:multidex:1.0.3'
        }
        

        有关更多信息,请阅读以下内容: https://developer.android.com/studio/build/multidex

        【讨论】:

          【解决方案10】:

          我用这种方式解决了同样的问题,您需要遵循 2 个简单的步骤。我是 AndroidX 用户,所以对我来说,首先我在我的项目的依赖项部分的 build.gradle 文件中实现这个依赖项。

          implementation 'androidx.multidex:multidex:2.0.1'

          dependencies {
          //for multidex
          implementation 'androidx.multidex:multidex:2.0.1'
          

          }

          在您的 build.gradle 文件中添加此依赖项后,然后再次同步您的项目

          现在添加“defaultConfig”部分:

          multiDexEnabled true

          defaultConfig {
              applicationId "com.papel.helper"
              minSdkVersion 16
              targetSdkVersion 28
              versionCode 1
              versionName "1.0"
              testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
          
              multiDexEnabled true
            
          }
          

          现在再次重建您的项目并运行:)

          【讨论】:

          • 对我来说,implementation 'androidx.multidex:multidex:2.0.1' 不是必需的(Android Studio 4.1.1)。够了multiDexEnabled true
          • 诀窍在于更新的 multidex 库版本
          【解决方案11】:

          我在 Flutter 项目中使用了以下解决方案:

          打开yourproject/app/build.gradle并添加以下行:

          android {
              ...
          
              defaultConfig {
                  ...
                  
                  minSdkVersion 21
                  multiDexEnabled true
              }
          }
          

          然后

          dependencies {
              ...
          
              implementation 'com.android.support:multidex:1.0.3'
          }
          

          如果您已迁移到 AndroidX,您将需要这个:

          dependencies {
              ...
          
              implementation 'androidx.multidex:multidex:2.0.1'
          }
          

          这个解决方案对我有用。感谢您提出这个问题。

          【讨论】:

          • 谢谢。但即使不添加依赖项,它也对我有用
          【解决方案12】:

          只需执行以下操作:

          build.gradle

          (module:app)
          
          
          android {
                      ....
                defaultConfig {
                        multiDexEnabled true // enable mun
                }
          }
          

          并在您的 build.gradle 应用级文件中添加以下依赖项

          dependencies {
              implementation 'com.android.support:multidex:1.0.3'
          } 
          

          【讨论】:

            【解决方案13】:

            在 Gradle 构建文件中,添加依赖:

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

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

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

            然后在“defaultConfig”部分,添加:

            multiDexEnabled true
            

            在targetSdkVersion下

            minSdkVersion 17
            targetSdkVersion 27
            multiDexEnabled true
            

            现在如果你遇到这个错误

            错误:无法解决:multidex-instrumentation 打开文件

            您需要将 gradle 更改为 3.0.1

            classpath 'com.android.tools.build:gradle:3.0.1'
            

            并将以下代码放入文件 gradle-wrapper.properties

            distributionUrl=https://services.gradle.org/distributions/gradle-4.1-all.zip

            终于

            在项目中添加类Applition并引入AndroidManifest 它是从 MultiDexApplication 扩展而来的

            public class App extends MultiDexApplication {
            
                @Override
                public void onCreate( ) {
                    super.onCreate( );
                }
            }
            

            先运行项目创建错误,然后清理项目

            最后,重新启动项目并享受它

            【讨论】:

            • 我仍然有“无法解决:multidex-instrumentation”
            【解决方案14】:

            我为我的项目找到了这个解决方案
            我只是将最低 SDK 版本设置为 21 就解决了我的问题

            android {
                defaultConfig {
                    ...
                    minSdkVersion 21 //set the minimum sdk version 21
                    targetSdkVersion 29
                }
                ...
            }
            

            如果你的 minSdkVersion 为 21 或更高,则默认启用 multidex,并且不需要 multidex 支持库。 阅读更多关于 multidex https://developer.android.com/studio/build/multidex.html

            【讨论】:

              【解决方案15】:

              只需将此代码添加到您的应用或模块的 build.gradle 中

              android {
                  defaultConfig {
                      ...
                      
                      multiDexEnabled true <------ * here
                  }
                  ...
              }
              

              【讨论】:

                【解决方案16】:
                multiDexEnabled true
                

                请尝试将其添加到您的应用 defaultConfig {}。这有助于我解决问题

                【讨论】:

                  【解决方案17】:

                  这将为 Kotlin 或 Java 项目完成工作。

                  第 1 步 - 在 Gradle 脚本下找到 build.gradle(Module:app)

                  第 2 步 - 添加 multiDexEnabled true 见下文:

                      compileSdkVersion 29
                  
                      defaultConfig {
                          applicationId "com.example.appname"
                          minSdkVersion 19
                          targetSdkVersion 29
                          versionCode 1
                          versionName "1.0"
                          multiDexEnabled true //addded
                          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                      }
                  

                  第 3 步 - 添加 multidex 依赖项

                  dependencies {
                  implementation 'com.android.support:multidex:2.0.0' //added
                  }
                  

                  最后,同步您的项目.. 享受吧!

                  【讨论】:

                    【解决方案18】:

                    如果您将 minifyEnabled 与 Proguard 一起使用,您可能不需要启用多 dex。我同意 MG Developer 的观点,即您应该尽可能避免使用多 dex。我的解决方案是只为调试构建启用多 dex。 minifyEnabled 解决了发布版本的问题

                    buildTypes {
                        release {
                            minifyEnabled true
                            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                        }
                        debug {
                            // getting error: Cannot fit requested classes in a single dex file.  # methods: 65537 > 65536
                            // https://developer.android.com/studio/build/multidex
                            // minifyEnabled true (used with release) will fix this by getting rid of unused method calls, but this will hide debugging info on crash
                            multiDexEnabled true
                        }
                    }
                    

                    【讨论】:

                    【解决方案19】:

                    你的方法太多了。 Android dex 文件有 65536 个方法的限制。

                    对于初学者,如果您不需要所有 google play services API 并且只需要位置,请替换

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

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

                    您可以参考此页面以了解避免它的方法,或者如果需要允许更多: https://developer.android.com/studio/build/multidex.html

                    【讨论】:

                    • 是的,但这不是真正的问题。我想我正在添加相同的依赖项。一个用于 Firebase,另一个用于位置检查下面两行。
                    【解决方案20】:

                    我不知道为什么,可能是因为我在 Kotlin 中开发但要修复此错误 我终于必须创建一个像这样扩展MultiDexApplication 的类:

                    class MyApplication : MultiDexApplication() {
                    }
                    

                    在我的Manifest.xml 中我必须设置

                    <application
                            ...
                            android:name=".MyApplication">
                    

                    为了不让任何人感到困惑,我也这样做:

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

                    对于 androidx,这也适用于我:

                    implementation 'androidx.multidex:multidex:2.0.0'
                    

                    ...

                    &lt;application android:name="android.support.multidex.MultiDexApplication"&gt;

                    不适合我

                    【讨论】:

                    • 记得在将 multiDexEnabled 设置为 true 后进行项目清理和重建。这为我解决了问题。
                    • 更改清单中的应用程序名称属性对我也不起作用,但在 gradle 构建文件中启用 multiDex 并添加实现确实有效。看来现在改manifest name tag是可选的?
                    【解决方案21】:

                    对于颤振

                    解决方案 1

                    要解决,只需将最小目标 SDK minSdkVersion 16 更改为 minSdkVersion 21

                    打开project/android/app/build.gradle并更改

                    defaultConfig {
                        ...
                        minSdkVersion 21
                     }
                    

                    解决方案 2

                    启用multidex:修改project/android/app/build.gradle文件以启用multidex并添加multidex库作为依赖,如下所示:

                    android {
                        defaultConfig {
                            ...
                            multiDexEnabled true
                        }
                        ...
                    }
                    
                    dependencies {
                      implementation 'com.android.support:multidex:1.0.3'
                    }
                    

                    【讨论】:

                    • minSdkVersion 21 运行良好
                    【解决方案22】:

                    添加您的文件android/app/build.gradle

                     defaultConfig {
                            applicationId "com.ubicacion"
                            minSdkVersion rootProject.ext.minSdkVersion
                            targetSdkVersion rootProject.ext.targetSdkVersion
                            versionCode 1
                            versionName "1.0"
                            multiDexEnabled true // ✔
                            missingDimensionStrategy 'react-native-camera', 'general'
                        }
                    

                    【讨论】:

                      【解决方案23】:
                      1. 转到 app 文件夹的 build.gradle,
                      2. 将 minSdkVersion 设置为 21

                      保存并运行您的应用,它现在应该可以在没有 dex 错误的情况下运行了

                      【讨论】:

                        【解决方案24】:

                        我所要做的就是在文件 > 项目结构 > 应用 > 风味中将最低 SDK 版本设置为 21

                        【讨论】:

                          【解决方案25】:

                          我遇到过两次这个错误,解决方法是;检查您的应用程序 gradle 文件以查看您的目标 SDK,如果它是 20 或更高,只需在您的 defaultconfig { multiDexEnabled true } 中添加一行

                          如果您的 targetSDK 小于 20,则将该行添加到您的 defaultConfig 并添加一个依赖项

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

                          查看此链接了解更多信息。

                          https://developer.android.com/studio/build/multidex#mdex-gradle

                          【讨论】:

                            【解决方案26】:

                            使用 multidex 支持应该是最后的手段。默认情况下,gradle build 会为你的 APK 收集大量的传递依赖。按照Google Developers Docs 中的建议,首先尝试从项目中删除不必要的依赖项。

                            使用命令行导航到 Android Projects Root。 您可以按如下方式获取编译依赖树。

                            gradlew app:dependencies --configuration debugCompileClasspath
                            

                            你可以获得完整的依赖树列表

                            gradlew app:dependencies
                            

                            然后从您的应用 build.gradle 中删除不必要的或传递的依赖项。例如,如果您的应用使用名为“com.google.api-client”的依赖项,您可以排除不需要的库/模块。

                            implementation ('com.google.api-client:google-api-client-android:1.28.0'){
                               exclude group: 'org.apache.httpcomponents'
                               exclude group: 'com.google.guava'
                               exclude group: 'com.fasterxml.jackson.core'
                               }
                            

                            然后在 Android Studio 中选择 Build > Analyze APK... 选择发布/调试 APK 文件以查看内容。这将为您提供方法和引用计数,如下所示。

                            【讨论】:

                            • 如果必须使用 multidex 有哪些缺点
                            【解决方案27】:

                            在构建:应用程序中

                            apply plugin: 'com.android.application'
                            android {
                            compileSdkVersion 28
                            defaultConfig {
                                applicationId "com.proyecto.marketdillo"
                                minSdkVersion 14
                                targetSdkVersion 28
                                versionCode 1
                                versionName "1.0"
                                testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
                                multiDexEnabled true
                            }
                            buildTypes {
                                release {
                                    minifyEnabled false
                                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                                }
                              }
                            }
                            dependencies {
                            implementation fileTree(dir: 'libs', include: ['*.jar'])
                            implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
                            implementation 'com.google.firebase:firebase-core:16.0.7'
                            implementation 'com.android.support.constraint:constraint-layout:1.1.3'
                            implementation 'com.android.support:support-v4:28.0.0-rc02'
                            implementation 'com.android.support:design:28.0.0-rc02'
                            implementation 'com.google.firebase:firebase-auth:16.1.0'
                            implementation 'com.squareup.picasso:picasso:2.5.2'
                            testImplementation 'junit:junit:4.12'
                            androidTestImplementation 'com.android.support.test:runner:1.0.2'
                            androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
                            implementation 'com.google.firebase:firebase-firestore:17.1.5'
                            implementation 'com.android.support:multidex:1.0.3'
                            }
                            apply plugin: 'com.google.gms.google-services'
                            

                            【讨论】:

                              【解决方案28】:
                              android {
                                  compileSdkVersion 28
                                  defaultConfig {
                                      applicationId "com.dev.khamsat"
                                      minSdkVersion 16
                                      targetSdkVersion 28
                                      versionCode 1
                                      versionName "2.0"
                                      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
                                      multiDexEnabled true
                                  }
                              

                              【讨论】:

                              • 这对我来说也足够了。
                              【解决方案29】:

                              该错误是由您的应用 Gradle 文件中的类依赖项数量引起的。如果类超过 72,400,您可能希望使用 multidex 作为更好地处理文件索引的解决方案。从这里了解更多信息:https://developer.android.com/studio/build/multidex

                              【讨论】:

                                【解决方案30】:

                                苦了两个小时找到了解决办法。

                                文件路径:android/build.gradle

                                为我工作

                                defaultConfig {
                                    applicationId "com.something"
                                    minSdkVersion rootProject.ext.minSdkVersion
                                    targetSdkVersion rootProject.ext.targetSdkVersion
                                    versionCode 1
                                    versionName "1.0"
                                    multiDexEnabled true --> This works for me
                                }
                                

                                如果您的 minSdkVersion 设置为 21 或更高,则默认启用 multidex,您不需要 multidex 支持库。 - 参考:https://developer.android.com/studio/build/multidex#mdex-gradle

                                【讨论】:

                                  猜你喜欢
                                  • 1970-01-01
                                  • 2020-02-03
                                  • 2018-09-06
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多