我有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


当前回答

启用 defaultConfig { multiDexEnabled真实 } 如果第一步不起作用 转到项目结构并找出正在使用的外部库 不同的版本。双击它并删除它的jar文件。关闭项目并再次打开android studio将重建项目。 问题应该已经解决了。

其他回答

添加如下命令:

android {
...

    dexOptions {

        jumboMode true
        javaMaxHeapSize "4g"

    }
}

在我的案例中,问题在于房间图书馆:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha1'

改为:

compile 'android.arch.persistence.room:runtime:1.0.0'

工作。

这个还没有在这里分享,所以希望它能帮助到一些人。

这个链接为我解决了同样的问题。

首先,我要指出,我不必将multiDexEnabled设置为true。

首先,我在pubspec中设置依赖项。yaml来

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.8.2 

并运行flutter包进入我的IDE终端。

此外,我还必须更改最小目标SDK版本:

android / app /构建开放。Gradle,然后找到那行说 minSdkVersion 16。 将这一行更改为minSdkVersion 21。 保存文件。

这本身就可以解决你的问题;然而,我还必须做以下工作,因为我的一些依赖版本不匹配。

我必须打开android/app/build。Gradle,然后添加下面的行作为文件的最后一行: 应用插件:'com.google.gms.google-services'

接下来,我必须打开android/build。Gradle,然后在buildscript标签中,添加一个新的依赖:

buildscript {
   repositories {
       // ...
   }

   dependencies {
       // ...
       classpath 'com.google.gms:google-services:3.2.1'   // new
   }
}

在此之后,我的应用程序终于在android模拟器上运行。

如果您遇到问题,该链接有一个更完整的演练。

另外,需要注意的是,我不必将multiDexEnabled设置为true。

在我的例子中,它是gson-2.8.1.jar,我已经添加到项目的libs文件夹中。但是SDK中已经有了参考。 所以没有必要将gson-2.8.1.jar添加到libs文件夹中。

当我把它拿出gson-2.8.1.jar项目编译没有这个wiered错误。

所以试着修改libs文件夹和依赖项。

在我的情况下,一个库使这个问题,库已成功添加到项目,但当我运行我的应用程序,它显示我这个错误。所以如果你也遇到了这种情况,你可以去github检查问题或提出新的问题。如果你没有找到任何解决方案关于图书馆,我建议你更换它。