首页 > 解决方案 > DexArchiveMergerException 仅在发布时

问题描述

我开发了一个使用 swagger 生成的 Java 客户端的应用程序。客户端在一个名为“api”的项目中,而应用程序在一个名为“app”的项目中。

Build/Make Project当我通过一切正常构建应用程序时。此外,当我尝试在模拟或物理设备上运行应用程序时Run/Run 'app'。使用调试器运行也可以。即使我通过构建项目Buid/Generate Signed Bundle/APK并选择调试选项,它也可以工作。

现在。当我尝试创建签名版本构建时,构建失败。以下消息显示:

引起:java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:合并 dex 档案时出错:[...]\app\build\intermediates\transforms\dexBuilder\release\54,[...] \app\build\intermediates\transforms\externalLibsDexMerger\release\0, [...]\app\build\intermediates\transforms\dexBuilder\release\52.jar, [...]\app\build\intermediates\transforms \dexBuilder\release\53.jar


原因:com.android.builder.dexing.DexArchiveMergerException: 合并 dex 档案时出错:[...]\app\build\intermediates\transforms\dexBuilder\release\54, [...]\app\build\intermediates \transforms\externalLibsDexMerger\release\0, [...]\app\build\intermediates\transforms\dexBuilder\release\52.jar, [...]\app\build\intermediates\transforms\dexBuilder\release\53 。罐


引起:com.android.tools.r8.CompilationFailedException:编译未能完成


引起:com.android.tools.r8.utils.AbortException:错误:程序类型已经存在:io.swagger.client.ApiCallback

我对使用 Android Studio 和 Gradle 进行开发非常陌生。我在 Stack Overflow 上尝试了一些解决方案,这些解决方案已经建议添加一些库,但到目前为止,它们都没有解决我的问题。

对我来说,最后一条错误消息指向io.swagger.client.ApiCallback.

settings.gradle是否与“api”和“app”具有相同内容的事实有关?两者看起来都像这样: rootProject.name = "swagger-java-client"。这是文件中唯一的一行,但据我所知,settings.gradlefor“app”已经包含了该内容。我不记得改变它了,所以它显示“swagger-java-client”对我来说很奇怪。这正常吗?

更新 1

评论中提到了 ProGuard 可能是一个问题。这是我能找到的唯一事件,引用它。在我build.gradle的“app”项目中,有这部分:

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

但是,删除它并没有改变任何东西。

更新 2

搜索“io.swagger”时,AS 只发现:

只找到一个匹配项

当搜索“io.swagger.client.ApiCallback”时,AS 只发现:

只找到一个匹配项

更新 3

按照建议,我尝试添加

android {
    defaultConfig {
        multiDexEnabled true
    }
}

并添加android.enableD8 = false,但这也无济于事。

这是我的 build.gradle-Files (第一个导致问题的文件,到目前为止我没有尝试过建议的更正。)

build.gradle(模块:api)(由 Swagger 生成):

apply plugin: 'idea'
apply plugin: 'eclipse'

group = 'io.swagger'
version = '1.0.0'

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    }
}

repositories {
    jcenter()
}


if(hasProperty('target') && target == 'android') {

    apply plugin: 'com.android.library'
    apply plugin: 'com.github.dcendents.android-maven'

    android {
        compileSdkVersion 25
        buildToolsVersion '25.0.2'
        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 25
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }

        // Rename the aar correctly
        libraryVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.aar')) {
                    def fileName = "${project.name}-${variant.baseName}-${version}.aar"
                    output.outputFile = new File(outputFile.parent, fileName)
                }
            }
        }

        dependencies {
            provided 'javax.annotation:jsr250-api:1.0'
        }
    }

    afterEvaluate {
        android.libraryVariants.all { variant ->
            def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
            task.description = "Create jar artifact for ${variant.name}"
            task.dependsOn variant.javaCompile
            task.from variant.javaCompile.destinationDir
            task.destinationDir = project.file("${project.buildDir}/outputs/jar")
            task.archiveName = "${project.name}-${variant.baseName}-${version}.jar"
            artifacts.add('archives', task);
        }
    }

    task sourcesJar(type: Jar) {
        from android.sourceSets.main.java.srcDirs
        classifier = 'sources'
    }

    artifacts {
        archives sourcesJar
    }

} else {

    apply plugin: 'java'
    apply plugin: 'maven'

    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7

    install {
        repositories.mavenInstaller {
            pom.artifactId = 'swagger-java-client'
        }
    }

    task execute(type:JavaExec) {
       main = System.getProperty('mainClass')
       classpath = sourceSets.main.runtimeClasspath
    }
}

dependencies {
    compile 'io.swagger:swagger-annotations:1.5.21'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
    compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'org.threeten:threetenbp:1.3.5'
    testCompile 'junit:junit:4.12'
}

build.gradle(模块:app):

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "projectName"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "0.1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation fileTree(dir: '../api/build/libs', include: ['*.jar'])
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.okhttp:okhttp:2.7.5'
    api project(path: ':api')
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.8'
    implementation 'org.jetbrains:annotations-java5:15.0'
}

标签: androidandroid-studiogradlebuildswagger

解决方案


以下是一些解决方法:

  • 您能否确保您已multiDexEnabled在 gradle 设置中设置为 true,如下所示:

    android {
        defaultConfig {
            multiDexEnabled true
        }
    }
    
  • 试试grade.propertiesandroid.enableD8 = false _

推荐阅读