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


当前回答

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

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

首先,我要指出,我不必将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。

其他回答

一个可能的根本原因是:Android Studio导入多模块项目时没有正确处理重复的瞬态依赖。检查你的清单并删除它们。对我来说,解决办法是这样的:

--- a/project/module/build.gradle
+++ b/project/module/build.gradle
@@ -21,5 +21,4 @@ android {
 dependencies {
     implementation project(':upstream-dependency-project')
     implementation 'com.android.support:support-v4:18.0.0'
-    implementation files('libs/slf4j-android-1.6.1-RC1.jar')
 }

只需在app/build.gradle中添加这一行

defaultConfig {
       multiDexEnabled true 
    }

试试这个,对我很管用。

在app / build.gradle:

dependencies {
    ....

    compile project(':grow-notification')

    /** Firebase Start **/
    compile 'com.google.firebase:firebase-messaging:11.4.0'
    compile 'com.firebase:firebase-jobdispatcher:0.6.0'
    /** Firebase End **/
}

apply plugin: 'com.google.gms.google-services'

在模块中的growth -notification/build.gradle:

dependencies {
    ....

    /** Firebase Start **/
    provided 'com.google.firebase:firebase-messaging:11.4.0'
    provided 'com.firebase:firebase-jobdispatcher:0.6.0'
    /** Firebase End **/
}

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

注意警告!

有时你只需要消除警告,错误就会自动消失。特殊情况见下文:


在我的模块级构建中有这两个依赖项。gradle文件:

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

和工作室已经警告(除了dex合并问题):

所有com.android.support库必须使用完全相同的版本规范(混合版本会导致运行时崩溃)。找到27.0.2、21.0.3版本。例如com.android.support:animated-vector-drawable:27.0.2和com.android.support:support-v4:21.0.3

所以我明确地确定了com.android的版本。支持:Support -v4(详情请参阅这里),两个问题(警告和与dex合并相关的问题)都解决了:

implementation 'com.android.support:support-v4:27.0.2'  // Added this line (according to above warning message)
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'

其他类似情况请参见下面的评论。