Android 无法合并索引

Android 无法合并索引,android,dex,android-studio-3.0,Android,Dex,Android Studio 3.0,我有Android Studio测试版。我用compile my old modules创建了一个新项目,但当我尝试启动应用程序时,它没有启动并显示以下消息: Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. com.android.builder.dexing.DexArchiveMergerException:无法合并dex 但我不知道如何解决这个错误。我

我有Android Studio测试版。我用compile my old modules创建了一个新项目,但当我尝试启动应用程序时,它没有启动并显示以下消息:

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
com.android.builder.dexing.DexArchiveMergerException:无法合并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'
}
____________发现_________

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

  • 删除
    .gradle
    目录

  • 再次运行你的应用程序

  • 注释

    • .gradle
      目录位于项目的根文件夹中。(您可能必须先显示隐藏的文件。)
    • 每次使用Android 3.0更新依赖模块时,我都必须这样做。(最近发布的Android Studio 3似乎已经解决了这个问题。)

      • 按照苏拉奇的建议删除
        .gradle
        对我来说是不够的。此外,我还必须执行一个
        Build>Clean项目

        请注意,要查看
        .gradle
        ,您需要切换到左上角导航器中的“项目”视图:


        当我从
        com.google.android.gms:play services:11.2.2
        更新到
        com.google.android.gms:play services:11.4.0
        时,我遇到了同样的问题。这为我解决了问题:

      • 干净的
      • 重建

      • 我同意Chris-Jr的观点。如果您使用Firebase嵌入AdMob广告(或者即使您没有),play services analytics包括play services广告,即使您没有将其作为依赖项添加。谷歌在推出11.4.0时显然犯了一个错误,因为分析包括10.0.1版的广告,而不是11.4.0版(渐变中的鼠标悬停提示显示了这一点)

        我在顶部手动添加了compile'com.google.android.gms:play services ads:11.4.0',但只有在我禁用Instant Run:

        因此,它要么回归到10.0.1,要么添加广告并禁用Instant Run。
        这是我发现的,如果有帮助。

        如果在包含kotlin支持后出现此错误,而其他解决方案均不起作用,请尝试将app module的
        build.gradle的kotlin依赖项更改为:

        implementation ("org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version") {
            exclude group: 'org.jetbrains', module: 'annotations'
        }
        

        这在Android Studio 3.0 Beta 6上对我很有效。请参阅答案以获得进一步解释。

        我尝试了上述所有方法,但没有一种有效。最后,我找到了适合我的工作:

        app/build.gradle:

        android {
            defaultConfig {
               multiDexEnabled true
            }
        }
        
        compile 'com.google.firebase:firebase-messaging:10.0.1'
        
        以下步骤对我有效:

      • 访问SdkManager-->Android Sdk-->Sdk工具并将Google play services更新至最新版本至46
      • 清理项目和重建项目
      • 在下面添加命令:

        android {
        ...
        
            dexOptions {
        
                jumboMode true
                javaMaxHeapSize "4g"
        
            }
        }
        

        对我来说,它是在app\build.gradle:

        android {
            defaultConfig {
               multiDexEnabled true
            }
        }
        
        compile 'com.google.firebase:firebase-messaging:10.0.1'
        


        不幸的是,就我而言,米歇尔和苏拉奇的解决方案对我都不起作用

        因此,我通过以下方法解决了这个问题:

        gradle:3.0中,编译配置现在已被弃用,应被实现api取代。欲了解更多信息,请访问官方文件

        编译配置仍然存在,但不应使用,因为它不能保证api和实现 配置提供

        最好使用实现api而不是编译

        只需将compile替换为implementationdebugCompile替换为debugImplementationtestCompile替换为testImplementationandroidtestcompile替换为androidTestImplementation

        例如:而不是这个

        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'
        
        像这样使用

        implementation 'com.android.support:appcompat-v7:26.0.2'
        implementation 'com.android.support:support-v4:26.1.0'
        implementation 'com.github.bumptech.glide:glide:4.0.0'
        
        之后

        • 删除项目中的.gradle文件夹(请注意,要查看.gradle,需要切换到左上角导航器中的“项目”视图)
        • 删除所有生成文件夹和gradle缓存
        • 从“生成”菜单中,按“清理项目”按钮
        • 任务完成后,按Build菜单中的重建项目按钮

        希望这会有帮助

        嗨,我几乎什么都试过了。所以,经过6个小时的努力,我终于通过逐行调试解决了这个问题

        classpath 'com.google.gms:google-services:3.0.0'
        
        Google services 3.0不支持firebase with Studio 3.0和playServiceVersion:11.6.0或更低版本

        implementation "com.google.firebase:firebase-messaging:$rootProject.ext.playServiceVersion"
        implementation "com.google.firebase:firebase-core:$rootProject.ext.playServiceVersion"
        implementation "com.firebase:firebase-jobdispatcher-with-gcm-dep:$rootProject.ext.jobdispatcherVersion"
        
        解决方案:

        我已将谷歌服务更改为

        classpath 'com.google.gms:google-services:3.1.1'
        
        它还支持firebase服务


        希望有人能节省他/她的时间。

        一个可能的根本原因是:重复的瞬态依赖项没有被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')
         }
        

        只需添加到上述解决方案中:

        确保没有指向的重复依赖项
        它们的不同版本,在多个地方(甚至在同一个文件中)。

        在我的案例中,问题是因为房间库:

        compile 'android.arch.persistence.room:runtime:1.0.0-alpha1'
        
        将其更改为:

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

        工作。

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

        当我取出它时,gson-2.8.1.jar项目编译时没有这个错误


        因此,尝试修改libs文件夹和依赖项

        安装Google play services(最新版本)+包括

        android {
            defaultConfig {
                multiDexEnabled true
                }
        }
        
        在build.gradle中为我解决了这个问题,确保
        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'
        
        implementation project(':common')
        implementation files('libs/farmer-motion-1.0.jar')
        
        implementation files('libs/farmer-motion-1.0.jar')
        
        Caused by: com.android.dex.DexException: Multiple dex files define Lcom/farmer/motion/common/data/pojo/SportSummary$2;
        
        implementation project(':common')
        // delete dependency implementation files('libs/farmer-motion-1.0.jar')
        
        api files('libs/farmer-motion-1.0.jar') // change implementation to api
        
        android {
                ...
                configurations.all {
                    resolutionStrategy {
                        // Force a particular version of the library 
                        // across all dependencies that have that dependency
                        force 'com.google.code.findbugs:jsr305:3.0.0'
                    }
                }
        }
        
        dependencies {
            ...
            implementation('com.google.auth:google-auth-library-oauth2-http:0.6.0') {
                // Exclude the library for this particular import
                exclude group: 'com.google.code.findbugs'
            }
        }
        
        android {
            ...
            configurations.all {
                resolutionStrategy {
                    // Completely exclude the library. Works for transitive
                    // dependencies.
                    exclude group: 'com.google.code.findbugs'
                }
            }
        }
        
        android {
            defaultConfig {
                ...
                minSdkVersion 15 
                targetSdkVersion 26
                multiDexEnabled true
            }
            ...
        }
        
        dependencies {
          compile 'com.android.support:multidex:1.0.1'
        }
        
        configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == 'com.android.support') {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion '26.1.0'
                }
            } else if (requested.group == "com.google.android.gms") {
                details.useVersion '11.8.0'
                } else if (requested.group == "com.google.firebase") {
                    details.useVersion '11.8.0'
                  }
              }
        }
        
        android.enableD8 = false
        
        android {
            compileSdkVersion 26
            defaultConfig {
              ...
                minSdkVersion 15
                targetSdkVersion 26
                multiDexEnabled true
             ...
            }
        }
        
        dependencies {
            compile 'com.android.support:multidex:1.0.1'}
        
        implementation 'com.jjoe64:graphview:4.2.2'
        
        compile 'com.jjoe64:graphview:4.2.1'