Android Studio 运行时异常:dex 写入线程中出现意外异常

21

我今天一整天都在遇到这个奇怪的错误 - 有人知道这里出了什么问题吗?

据我所知,我已经正确地使用了multidex库(以下是从app.gradle文件中获取的):

defaultConfig {
    applicationId "com.example.simon"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    /*Enabling multidex*/
    multiDexEnabled true
}

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

我的顶层 gradle 文件非常基础:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.google.gms:google-services:1.3.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

错误:

    Executing tasks: [:app:generateDebugSources,
:app:generateDebugAndroidTestSources]

Parallel execution with configuration on demand is an incubating
feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex101Library UP-TO-DATE
:app:prepareComAndroidSupportPaletteV72301Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComDigitsSdkAndroidDigits162Library UP-TO-DATE
:app:prepareComFacebookAndroidFacebookAndroidSdk440Library UP-TO-DATE
:app:prepareComFacebookConcealConceal101Library UP-TO-DATE
:app:prepareComGithubCurioustechizenAndroidAgoLibrary130Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGcm780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMaps780Library UP-TO-DATE
:app:prepareComTheartofdevEdmodoAndroidImageCropper104Library UP-TO-DATE
:app:prepareComTwitterSdkAndroidTweetComposer080Library UP-TO-DATE
:app:prepareComTwitterSdkAndroidTweetUi131Library UP-TO-DATE
:app:prepareComTwitterSdkAndroidTwitter161Library UP-TO-DATE
:app:prepareComTwitterSdkAndroidTwitterCore141Library UP-TO-DATE
:app:prepareIoFabricSdkAndroidFabric134Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:processDebugGoogleServices
:app:generateDebugResources
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesDebug
:app:processDebugResources
:app:generateDebugSources
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources UP-TO-DATE
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE

BUILD SUCCESSFUL

Total time: 3.913 secs Executing tasks: [:app:assembleDebug]

Parallel execution with configuration on demand is an incubating
feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex101Library UP-TO-DATE
:app:prepareComAndroidSupportPaletteV72301Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComDigitsSdkAndroidDigits162Library UP-TO-DATE
:app:prepareComFacebookAndroidFacebookAndroidSdk440Library UP-TO-DATE
:app:prepareComGithubCurioustechizenAndroidAgoLibrary130Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGcm780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMaps780Library UP-TO-DATE
:app:prepareComTheartofdevEdmodoAndroidImageCropper104Library
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:processDebugGoogleServices
:app:generateDebugResources
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesDebug
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:collectDebugMultiDexComponents UP-TO-DATE
:app:packageAllDebugClassesForMultiDex UP-TO-DATE
:app:shrinkDebugMultiDexComponents UP-TO-DATE
:app:createDebugMainDexClassList UP-TO-DATE
:app:dexDebugExecuting
tasks: [:app:assembleDebug]

Parallel execution with configuration on demand is an incubating
feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex101Library UP-TO-DATE
:app:prepareComAndroidSupportPaletteV72301Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComDigitsSdkAndroidDigits162Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:processDebugGoogleServices
:app:generateDebugResources
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesDebug
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:collectDebugMultiDexComponents UP-TO-DATE
:app:packageAllDebugClassesForMultiDex UP-TO-DATE
:app:shrinkDebugMultiDexComponents UP-TO-DATE
:app:createDebugMainDexClassList UP-TO-DATE
:app:dexDebug AGPBI:
{"kind":"simple","text":"UNEXPECTED TOP-LEVEL
EXCEPTION:","sources":[{}]} AGPBI:
{"kind":"simple","text":"java.lang.RuntimeException: Unexpected
exception in dex writer thread","sources":[{}]} AGPBI:
{"kind":"simple","text":"\tat
com.android.dx.command.dexer.Main.runMultiDex(Main.java:397)","sources":[{}]}
AGPBI: {"kind":"simple","text":"\tat
com.android.dx.command.dexer.Main.run(Main.java:275)","sources":[{}]}
AGPBI: {"kind":"simple","text":"\tat
com.android.dx.command.dexer.Main.main(Main.java:245)","sources":[{}]}
AGPBI: {"kind":"simple","text":"\tat
com.android.dx.command.Main.main(Main.java:106)","sources":[{}]}


 FAILED

FAILURE: Build failed with an exception.

* What went wrong: Execution failed for task ':app:dexDebug'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command
'C:\Program Files\Java\jdk1.7.0_76\bin\java.exe'' finished with
non-zero exit value 2

* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1 mins 5.426 secs

你尝试过使用--info或--debug运行以获取更多信息吗?如果没有其他办法,你可以尝试找到并手动运行gradle正在运行的确切命令,以查看其输出和失败原因。 - JesusFreke
嗨,我今天稍后会运行它并回复你 - 抱歉回复慢了。 - Simon
3个回答

71

在谷歌搜索一段时间后,我发现问题是dex写入器分配的堆空间不够。

我通过在我的应用gradle.build中添加以下内容来解决它:

android {
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }
}

这个选项也成功地显著加速了我的 gradle 编译。

使用 Gradle(Android Studio)编译时间极长


20
细节:dexOptions {} 应该放在 build.gradle 的 android {} 块内。 - tarkeshwar
3
“incremental true”已被弃用,但“javaMaxHeapSize "4g"”非常好用!谢谢! - sagus_helgy

2

将以下内容添加到我的项目级gradle.properties中有所帮助 -

org.gradle.jvmargs=-Xmx1536m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

1
将您的清单更改为以下内容。
defaultConfig 
{
    applicationId "com.example.simon"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    /*Enabling multidex*/
    multiDexEnabled true
}
android {
    dexOptions {
        incremental true
        javaMaxHeapSize "4g"
    }
}

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

这个问题在添加“:”时出现。
/*Enabling multidex*/
    multiDexEnabled true

大多数情况下。如需更多信息,请告诉我您不理解的确切内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接