Android 构建 APK 时出现 Dex 错误

Android Dex error while building APK

我在尝试构建 APK 时遇到以下错误。我已经阅读了几篇关于如何解决该问题的文章,但由于我开发的时间不长,所以我不确定自己在做什么。所以,希望有人能帮忙。

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: 
        java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

这是我的build.gradle(模块)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.dawnlp.mymap"
        minSdkVersion 22
        targetSdkVersion 25
        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(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.google.android.gms:play-services:10.0.1'
    testCompile 'junit:junit:4.12'
}

这是我的 build.gradle 项目

/ Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

如果有人能帮忙,我将不胜感激,同时请记住,我有点缺乏经验。

使用它:

android{
    defaultConfig {

            multiDexEnabled true
        }
}

也用它

   dependencies {
    compile 'com.android.support:multidex:1.0.1'
}

您需要启用 multidex,将此行添加到您的 defaultConfig :

multiDexEnabled true

并将此行添加到您的依赖项中:

compile 'com.android.support:multidex:1.0.1'

此外,由于您是 运行 单元测试,因此您需要创建一个 class 来扩展应用程序并安装 MultiDex :

public class YouApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

在您的模块级别使用此块 gradle: dex选项{ javaMaxHeapSize "4g" }

并在清单中将应用程序名称更改为 .multidex。 希望您的代码现在可以正常工作