我以前见过不同版本的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
}
}