Android Dex错误,同时构建APK

问题描述:

我在尝试构建APK时遇到下面的错误。我已经阅读了几篇关于如何解决这个问题的文章,但由于我没有长时间的发展,我不确定自己在做什么。所以,希望有人能帮忙。Android Dex错误,同时构建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() 
    } 
} 

将不胜感激,如果有人可以帮帮忙,还裸介意我有点缺乏经验。

+0

你必须启用multidex,因为你的方法超过65536,或者你可以用广告等简单地替换播放服务库 – Redman

+0

启用[multidex](http:// stackoverflow.com/questions/15209831/unable-to-execute-dex-method-id-not-in-0-0xffff-65536) – azizbekian

使用它:

android{ 
    defaultConfig { 

      multiDexEnabled true 
     } 
} 

而且使用它太

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

谢谢。你的意思是地方 defaultConfig { multiDexEnabled真正 } 到 依赖{ 编译 'com.android.support:multidex:1.0.1' } –

+0

像这样 依赖{ defaultConfig { multiDexEnabled编译文件树(dir:'libs',包括:['* .jar']) androidTestCompile de组:'com.android.support',模块:'support-annotations' }) compile'c​​om.android.support:appcompat-v7:25.1.0' compile'c​​om.google.android.gms:play -services:10.0.1' testCompile'junit:junit:4.12' } –

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

multiDexEnabled true 

而此行到您的依赖关系:

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

而且,当你正在运行单元测试,你需要创建一个扩展应用的一类,并安装MultiDex:

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

谢谢。 defaultConfig在哪里? –

+0

在build.gradle(模块)文件中。 –

使用该块在模块级的gradle产出: dexOptions { javaMaxHeapSize“ 4G” }

而且在清单改变应用程序的名称,以.multidex。 希望你的代码现在可以正常工作