DexException多个dex文件在Android

时间:2018-02-08 08:56:49

标签: android android-studio jaxb dex android-multidex

在我的android项目中,我使用了两个JAXB库jaxp-ri(用于XMLGregorianCalendarImpl类)和jaxb-api(用于对象到xml转换)。我在build.gradle文件中添加了这两个库。

但是当我构建我的项目时,我得到了下面提到的错误

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Ljavax/xml/stream/events/Namespace;
Error:com.android.dex.DexException: Multiple dex files define Ljavax/xml/stream/events/Namespace;
Error:  at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:661)
Error:  at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:616)
Error:  at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:598)
Error:  at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
Error:  at com.android.dx.merge.DexMerger.merge(DexMerger.java:198)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:61)
Error:  at com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:36)
Error:  at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
Error:  at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
Error:  at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
Error:  at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
Error:  at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Error:Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Ljavax/xml/stream/events/Namespace;

Build.gradle文件

dependencies {

    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.android.support:appcompat-v7:26.0.0-beta1'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'com.android.support.test:runner:1.0.1'

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    compile group: 'com.sun.org.apache', name: 'jaxp-ri', version: '1.4'

    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1'

}

任何人都可以帮忙吗?我该如何解决这个问题?

修改

完成build.gradle文件

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "in.cdac.xmlparsing"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile group: 'com.sun.org.apache', name: 'jaxp-ri', version: '1.4'
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1'
}

2 个答案:

答案 0 :(得分:4)

只需在模块级build.gradle中声明 multiDexEnabled true ,仅当你的minSdkVersion设置为21或更高时才有效。

如果你的minSdkVersion低于21,除了上面的步骤,你需要将multidex支持库作为依赖

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

您可以在此处阅读详细信息 - https://developer.android.com/studio/build/multidex.html

答案 1 :(得分:2)

在build.gradle(Module:app)下使用。您可以添加所需的依赖项。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.administrator.firstproject"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled = true
        dexOptions {
            javaMaxHeapSize "4g"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.+'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    // viewslider dependancies
    compile 'com.android.support:support-annotations:26.+'
    compile 'com.android.support:support-compat:26.+'
    compile 'com.android.support:support-core-ui:26.+'
    compile 'com.android.support:multidex:1.0.1'
    implementation files('libs/poi-3.7.jar')


}