Android应用构建错误。无法合并dex

时间:2018-01-12 05:28:50

标签: android android-studio gradle android-gradle android-build

  

错误:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'执行失败。   java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并dex

我使用的是Android Studio 3.0.1版。

我也尝试过清理项目'和'重建项目'。

* project build gradle

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

* module build gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
ext {
    grpcVersion = '1.4.0'
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.backkom.speechtest"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        multiDexEnabled  true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
    }
}

protobuf {
    protoc {
       artifact = 'com.google.protobuf:protoc:3.3.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                javalite {}
                grpc {
                // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    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 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    // gRPC
    compile "io.grpc:grpc-okhttp:$grpcVersion"
    compile "io.grpc:grpc-protobuf-lite:$grpcVersion"
    compile "io.grpc:grpc-stub:$grpcVersion"
    compile 'javax.annotation:javax.annotation-api:1.2'
    protobuf 'com.google.protobuf:protobuf-java:3.3.1'

    compile group: 'com.google.api.grpc', name: 'grpc-google-cloud-speech-v1', version: '0.1.13'

    // OAuth2 for Google API
    compile('com.google.auth:google-auth-library-oauth2-http:0.7.0') {
    exclude module: 'httpclient'
    }

    compile 'com.android.support:multidex:1.0.2'
}

当我进行重建项目时,我收到如下错误。

  

错误:任务':app:transformClassesWithMultidexlistForDebug'执行失败。 java.io.IOException:不能写[C:\ Users \ backkom \ AndroidStudioProjects \ SpeechTest \ app \ build \ intermediates \ multi-dex \ debug \ componentClasses.jar](不能读[C: \用户\ backkom.gradle \缓存\模块-2 \文件-2.1 \ com.google.protobuf \的protobuf-java的\ 3.3.1 \ e8964a2667e55d11a4505b329a4c34247663920b \的protobuf-java的3.3.1.jar(;;;;;; * * .class)](重复的zip条目[protobuf-java-3.3.1.jar:com / google / protobuf / ExperimentalApi.class]))

2 个答案:

答案 0 :(得分:-1)

从您的错误日志中,似乎您的目录 backkom 不适用于android studio。所以请检查你的目录

 C:\Users\backkom

是否可写。通过这样做 。转到该目录并检查其权限。它是否已经读取和写入权限。

答案 1 :(得分:-1)

根据我的理解,

试试这个:

Clean Project button.
Rebuild Project button from the Build menu.

使用此

更改local.properties
org.gradle.jvmargs=-XX\:MaxHeapSize\=512m -Xmx512m

以这种方式定义gradle

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.xxxx.xxxx"
minSdkVersion 21
targetSdkVersion 26
versionCode 7
versionName "0.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}

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

}

在项目中添加此multidex

compile 'com.android.support:multidex:1.0.1' 

并在Manifest中定义它