【问题标题】:Android Dex error while building APK [duplicate]构建APK时出现Android Dex错误[重复]
【发布时间】:2023-03-27 00:02:01
【问题描述】:

我在尝试构建 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()
    }
}

如果有人可以提供帮助,将不胜感激,同时我也有点缺乏经验。

【问题讨论】:

  • 您必须启用 multidex,因为您的方法超过 65536,或者您可以简单地将播放服务库替换为广告等所需的内容
  • 启用multidex

标签: android gradle apk dex


【解决方案1】:

使用它:

android{
    defaultConfig {

            multiDexEnabled true
        }
}

也可以使用它

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

【讨论】:

  • 谢谢。您是说将 defaultConfig { multiDexEnabled true } 放入依赖项 { compile 'com.android.support:multidex:1.0.1' }
  • 像这样的依赖项 { defaultConfig { multiDexEnabled true } 编译 fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso -core:2.2.2',{排除组:'com.android.support',模块:'support-annotations'}) 编译'com.android.support:appcompat-v7:25.1.0' 编译'com.google .android.gms:play-services:10.0.1' testCompile 'junit:junit:4.12' }
【解决方案2】:

您需要启用 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);
    }
}

【讨论】:

  • 谢谢。默认配置在哪里?
  • 在 build.gradle(模块)文件中。
【解决方案3】:

在你的模块级 gradle 中使用这个块: dexOptions { javaMaxHeapSize "4g" }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2018-09-28
    • 2017-05-10
    • 1970-01-01
    相关资源
    最近更新 更多