我以前见过不同版本的dex错误,但这一个是新的。清洁/重新启动等不会有帮助。库项目似乎完好无损,依赖项似乎正确链接。

Unable to execute dex: method ID not in [0, 0xffff]: 65536
Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536

or

Cannot merge new index 65950 into a non-jumbo instruction

or

java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

tl;dr:谷歌的官方解决方案终于来了!

http://developer.android.com/tools/building/multidex.html

只有一个小提示,您可能需要这样做,以防止在进行索引时内存不足。

dexOptions {
        javaMaxHeapSize "4g"
}

还有一个超大模式可以以一种不太可靠的方式解决这个问题:

dexOptions {
        jumboMode true
}

更新:如果你的应用程序很胖,你有太多的方法在你的主应用程序,你可能需要重新组织你的应用程序

http://blog.osom.info/2014/12/too-many-methods-in-main-dex.html


当前回答

如果你使用Gradle,下面的代码会有所帮助。允许您轻松删除不需要的谷歌服务(假设您正在使用它们),以恢复到低于65k阈值。所有的功劳都来自这个帖子:https://gist.github.com/dmarcato/d7c91b94214acd936e42

关于上面提到的要点有很多有趣的讨论。TLDR吗?看看这个:https://gist.github.com/Takhion/10a37046b9e6d259bb31

将此代码粘贴到构建的底部。Gradle文件,调整你不需要的谷歌服务列表:

def toCamelCase(String string) {
    String result = ""
    string.findAll("[^\\W]+") { String word ->
        result += word.capitalize()
    }
    return result
}

afterEvaluate { project ->
    Configuration runtimeConfiguration = project.configurations.getByName('compile')
    ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
    // Forces resolve of configuration
    ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion

    String prepareTaskName = "prepare${toCamelCase("${module.group} ${module.name} ${module.version}")}Library"
    File playServiceRootFolder = project.tasks.find { it.name.equals(prepareTaskName) }.explodedDir

    Task stripPlayServices = project.tasks.create(name: 'stripPlayServices', group: "Strip") {
        inputs.files new File(playServiceRootFolder, "classes.jar")
        outputs.dir playServiceRootFolder
        description 'Strip useless packages from Google Play Services library to avoid reaching dex limit'

        doLast {
            copy {
                from(file(new File(playServiceRootFolder, "classes.jar")))
                into(file(playServiceRootFolder))
                rename { fileName ->
                    fileName = "classes_orig.jar"
                }
            }
            tasks.create(name: "stripPlayServices" + module.version, type: Jar) {
                destinationDir = playServiceRootFolder
                archiveName = "classes.jar"
                from(zipTree(new File(playServiceRootFolder, "classes_orig.jar"))) {
                    exclude "com/google/ads/**"
                    exclude "com/google/android/gms/analytics/**"
                    exclude "com/google/android/gms/games/**"
                    exclude "com/google/android/gms/plus/**"
                    exclude "com/google/android/gms/drive/**"
                    exclude "com/google/android/gms/ads/**"
                }
            }.execute()
            delete file(new File(playServiceRootFolder, "classes_orig.jar"))
        }
    }

    project.tasks.findAll { it.name.startsWith('prepare') && it.name.endsWith('Dependencies') }.each { Task task ->
        task.dependsOn stripPlayServices
    }
}

其他回答

如果你使用Gradle,下面的代码会有所帮助。允许您轻松删除不需要的谷歌服务(假设您正在使用它们),以恢复到低于65k阈值。所有的功劳都来自这个帖子:https://gist.github.com/dmarcato/d7c91b94214acd936e42

关于上面提到的要点有很多有趣的讨论。TLDR吗?看看这个:https://gist.github.com/Takhion/10a37046b9e6d259bb31

将此代码粘贴到构建的底部。Gradle文件,调整你不需要的谷歌服务列表:

def toCamelCase(String string) {
    String result = ""
    string.findAll("[^\\W]+") { String word ->
        result += word.capitalize()
    }
    return result
}

afterEvaluate { project ->
    Configuration runtimeConfiguration = project.configurations.getByName('compile')
    ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
    // Forces resolve of configuration
    ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion

    String prepareTaskName = "prepare${toCamelCase("${module.group} ${module.name} ${module.version}")}Library"
    File playServiceRootFolder = project.tasks.find { it.name.equals(prepareTaskName) }.explodedDir

    Task stripPlayServices = project.tasks.create(name: 'stripPlayServices', group: "Strip") {
        inputs.files new File(playServiceRootFolder, "classes.jar")
        outputs.dir playServiceRootFolder
        description 'Strip useless packages from Google Play Services library to avoid reaching dex limit'

        doLast {
            copy {
                from(file(new File(playServiceRootFolder, "classes.jar")))
                into(file(playServiceRootFolder))
                rename { fileName ->
                    fileName = "classes_orig.jar"
                }
            }
            tasks.create(name: "stripPlayServices" + module.version, type: Jar) {
                destinationDir = playServiceRootFolder
                archiveName = "classes.jar"
                from(zipTree(new File(playServiceRootFolder, "classes_orig.jar"))) {
                    exclude "com/google/ads/**"
                    exclude "com/google/android/gms/analytics/**"
                    exclude "com/google/android/gms/games/**"
                    exclude "com/google/android/gms/plus/**"
                    exclude "com/google/android/gms/drive/**"
                    exclude "com/google/android/gms/ads/**"
                }
            }.execute()
            delete file(new File(playServiceRootFolder, "classes_orig.jar"))
        }
    }

    project.tasks.findAll { it.name.startsWith('prepare') && it.name.endsWith('Dependencies') }.each { Task task ->
        task.dependsOn stripPlayServices
    }
}

完美的解决方案是与Proguard合作。正如aleb在评论中提到的。 它将使dex文件的大小减少一半。

尝试在构建中添加以下代码。Gradle,它对我很管用

compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
    multiDexEnabled true
}

我今天也遇到了同样的问题下面是有效的方法

为ANDROID工作室…启用立即运行

In File->Preferences->Build, Execution, Deployment->Instant Run-> Check Enable Instant Run for热插拔…

希望能有所帮助

我已经分享了一个使用custom_rules.xml构建脚本和几行代码解决这个问题的示例项目。

我在自己的项目中使用了它,它在100多万设备上运行完美无缺(从android-8到最新的android-19)。希望能有所帮助。

https://github.com/mmin18/Dex65536