Android Studio 3.1 任务执行失败:合并 dex 存档时出错

Android Studio 3.1 Execution failed for task: Error while merging dex archives

android Studio 在使用以下

构建执行时出现构建错误

Execution failed for task ':app:transformClassesWithMultidexlistForDebug'. com.android.build.api.transform.TransformException: Error while generating the main dex list.

这是我的 gradle 文件,我在其中添加了一些 jar 依赖项。

App.gradle

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.demo"
        minSdkVersion 15
        targetSdkVersion 27
        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(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    implementation files('libs/commons-codec-1.4.jar')
    implementation files('libs/commons-codec-1.9.jar')
    implementation files('libs/opencsv-2.4.jar')
    implementation files('libs/ormlite-android-4.46.jar')
    implementation files('libs/ormlite-core-4.46.jar')
    implementation files('libs/Pushwoosh.jar')
    implementation files('libs/StartAppInApp-2.3.2.jar')
}

Project.gradle

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}
allprojects {
    repositories {
        jcenter()
        google()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

使用需要确保 multidex 在 build.gradle.

中为真
    android {
compileSdkVersion 22
buildToolsVersion "23.0.0"

     defaultConfig {
         minSdkVersion 14 //lower than 14 doesn't support multidex
         targetSdkVersion 22

         // Enabling multidex support.
         multiDexEnabled true
     }
      }

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

您还可以创建一个 class 来扩展 multidex 并且 class 应该在应用程序标签中提及。