错误:无法在单个dex文件中放入请求的类。请尝试提供main-dex列表。 #methods:72477> 65536

时间:2018-01-14 12:34:34

标签: android multidex

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

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'

30 个答案:

答案 0 :(得分:159)

他们给你的答案都不详尽。 问题出在Multidex上。 您必须在app gradle中添加库:

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

之后,添加app gradle的defaultConfig:

multiDexEnabled true

您的申请必须是Multidex类型.. 你必须在清单中写下它:

android:name=".MyApplication"

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

答案 1 :(得分:86)

我用下面的解决方案解决了我的问题:

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

  

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

然后在" defaultConfig"部分,添加:

  

multiDexEnabled true

答案 2 :(得分:49)

修改您的应用或模块的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在app上执行预编译   安装时间,扫描classesN.dex文件并将其编译成   单个.oat文件,供Android设备执行。因此,如果   你的minSdkVersion是21或更高, 你不需要multidex   支持库。

     

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

.not(':visible')

答案 3 :(得分:31)

您可以关注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
    }
    ...
}

答案 4 :(得分:18)

嗨问题在这里删除它

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

答案 5 :(得分:14)

这将完成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
}

最后,同步您的项目。尽情享受吧!

答案 6 :(得分:14)

对于扑朔迷离的项目,您也可以解决此问题

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

您拥有以下内容:

port

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

android {
    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 28

    }

}

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

并定义实现

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

    }

}

最后,您应该具有:

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

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

答案 7 :(得分:13)

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

debug {
            multiDexEnabled true
        }

buildTypes

如果您要构建 RELEASE APK ,请添加 发布块中的multiDexEnabled true为 -

release {
                ...
                multiDexEnabled true
                ...
        }

答案 8 :(得分:12)

我为我的项目找到了这个解决方案
我只是将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

的更多信息

答案 9 :(得分:9)

添加文件 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'
    }

答案 10 :(得分:8)

在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( );
}

}

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

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

答案 11 :(得分:8)

您计算的方法太多了。 Android dex文件限制为允许的65536种方法。

首先,如果您不需要所有Google Play服务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

答案 12 :(得分:8)

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

注意:请确保最小SDK超过14。

在您的build.gradle中:

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



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

用于androidx的使用:

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

有关更多信息,请访问原始链接:

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

答案 13 :(得分:7)

我以这种方式解决了同样的问题,您需要遵循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
  
}

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

答案 14 :(得分:7)

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

police stations discovered set

答案 15 :(得分:6)

  1. 转到app文件夹的build.gradle,
  2. 将 minSdkVersion 设置为 21

保存并运行您的应用程序,它现在应该不会出现 dex 错误

答案 16 :(得分:6)

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

答案 17 :(得分:6)

我不知道为什么,也许是因为我在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'

...

<application android:name="android.support.multidex.MultiDexApplication">

对我不起作用

答案 18 :(得分:5)

multiDexEnabled true

请尝试将其添加到您的应用程序defaultConfig {}中。这可以帮助我解决问题

答案 19 :(得分:5)

只需执行以下操作:

  

build.gradle

(module:app)

android {
        ....
        multiDexEnabled 'true' <== Enter code here
    }

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

答案 20 :(得分:5)

什么是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项目之后,并且在您运行该项目之前,请确保在运行之前先构建一个项目。 否则,您将获得一个例外。

答案 21 :(得分:4)

我两次遇到此错误,解决方法是;检查您的应用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

答案 22 :(得分:4)

对于 Flutter

解决方案 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'
}

答案 23 :(得分:4)

如果您将minifyEnabled与Proguard一起使用,则可能不需要启用multi-dex。我同意MG Developer的建议,如果可能,应尽量避免使用多dex。我的解决方案是仅对调试版本启用multi-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
    }
}

答案 24 :(得分:2)

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

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

defaultConfig {
    ...

    multiDexEnabled true
}

然后

dependencies {
    ...

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

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

dependencies {
    ...

    implementation 'androidx.multidex:multidex:2.0.1'
}

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

答案 25 :(得分:2)

在build:app

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'

答案 26 :(得分:1)

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

答案 27 :(得分:1)

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

使用命令行导航到Android项目根目录。 您可以如下获得编译依赖树。

gradlew app:dependencies --configuration debugCompileClasspath

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

gradlew app:dependencies

enter image description here

然后从您的应用程序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中选择构建>分析APK ... 选择发布/调试APK文件以查看内容。这将为您提供方法和引用计数,如下所示。

Analyze APK

答案 28 :(得分:0)

痛苦两个小时后找到了解决方法。

文件路径: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

答案 29 :(得分:0)

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
    }