我有Android Studio Beta版。我创建了一个新项目,编译我的旧模块,但当我尝试启动应用程序时,它没有启动消息:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

dexarchiivemergenerexception:无法合并dex

但是我不知道如何解决这个错误。我在谷歌上搜索了几个小时,但没有成功。

我的项目gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'
        classpath "io.realm:realm-gradle-plugin:3.7.1"
        classpath 'com.google.gms:google-services:3.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用程序gradle:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "parad0x.sk.onlyforyou"
        minSdkVersion 21
        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'
        }
        debug {
        }
    }
    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    lintOptions {
        checkReleaseBuilds false
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    //noinspection GradleCompatible
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile project(path: ':loginregisterview')


}

我的模块gradle:

    apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    testCompile 'junit:junit:4.12'
    compile project(path: ':parser')

}

我的第二个模块:

     apply plugin: 'com.android.library'
apply plugin: 'realm-android'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    realm {
        syncEnabled = true
    }
    useLibrary 'org.apache.http.legacy'

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'junit:junit:4.12'
    //  compile 'com.android.support:appcompat-v7:23.1.0'

    //   compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
 //   compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'com.google.code.gson:gson:2.6.2'
}

____________ finding_________

当我没有导入第二个模块(解析器)时,应用程序没有在dex上崩溃,但当模块没有导入时,应用程序无法工作。: D: D


当前回答

将此添加到app/build.gradle中。会有用的

实现“com.android.support:设计:27.1.0”

其他回答

我遇到了同样的问题,并找到了我的真正原因。 之前我也把之前的所有答案都试了一遍,但是没有解决问题。 我有两个模块在我的穿着应用程序项目,和构建。Gradle如下:

磨损模块的build.gradle:

implementation project(':common')
implementation files('libs/farmer-motion-1.0.jar')

通用模块的build.gradle:

implementation files('libs/farmer-motion-1.0.jar')

在升级到gradle 3之前。X, 'implementation'都是'compile'。

我用——stacktrace选项运行gradlew来获得堆栈跟踪,当出现这个问题时,你可以在gradle控制台窗口上单击这个。并发现依赖于jar包重复:

Caused by: com.android.dex.DexException: Multiple dex files define Lcom/farmer/motion/common/data/pojo/SportSummary$2;

类SportSummary在farmer-motion-1.0.jar包,在阅读官方迁移指南后,我改变了我的构建。Gradle如下:

磨损模块的build.gradle:

implementation project(':common')
// delete dependency implementation files('libs/farmer-motion-1.0.jar')

build.gradle for common module:

api files('libs/farmer-motion-1.0.jar') // change implementation to api

现在磨损模块将有依赖农民运动1.0.jar导出由公共模块。 如果运行时对jar包没有依赖,也可以将jar包的“implementation”依赖更改为“compileOnly”。

在我的项目中,我有两个以上的模块和sdk,我尝试了上面列出的所有建议或答案

在defaultConfig中添加multiDexEnabled true 清洁及重建计划 用实现代替编译 更新所有依赖项

所有这些工作暂时,但当我打开项目结构(cntrl + altr + shift+ s)我发现,在我的项目属性没有什么会选择像- -编译SDK版本,构建SDK版本 -口味相同

我更新所有这些,并执行清洁和重建项目,它为我工作。

我也有同样的问题。 我只是启用了即时运行(它被禁用),然后我的项目工作了。 你可以在……找到它

File->Settings-> Build,Execution,Deployment->Instant Run

在Android工作室3.5即时运行已删除。 请参考这里

在大多数情况下,这是可行的。 这对我很管用。 祝你好运,如果这对你也有用的话。

步骤1:构建中。gradle(模块:应用) 添加: defaultConfig { multiDexEnabled真实 }

步骤2:这样做:Build=>清洁项目

步骤3:这样做:Build=>重建项目

一种可能性是:存在相同的库,但依赖项中有不同的版本。

我有这个问题与以下行在gradle文件:

编译文件树(包括:['*.jar'], dir: 'libs') 编译的com.google.code.gson: gson: 2.8.2

gson库在我的libs目录中,但使用的是以前的版本。 我从libs目录中删除了gson-2.3.1.jar,一切都恢复正常了。