Android 应用太大?无法执行 dex:无法将新索引合并到非巨型指令中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23527218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:14:15  来源:igfitidea点击:

Application too big? Unable to execute dex: Cannot merge new index into a non-jumbo instruction

androidlinker-errorsdex

提问by rupps

I am getting the following error when I compile my app:

编译应用程序时出现以下错误:

[2014-05-07 21:48:42 - Dex Loader] Unable to execute dex: Cannot merge new index 65536 into a non-jumbo instruction!

I am at the point that if I declare a new method anywhere in my package, I get this error. If I don't, the app compiles.

我在那个点,如果我宣布我的包中的新方法在任何地方,我得到这个错误。如果我不这样做,应用程序会编译。

I would like to know what exactly (and accurately) does this error mean. My app is big, but I don't think its that big! So:

我想知道这个错误究竟(准确地)是什么意思。我的应用程序很大,但我认为它没有那么大!所以:

  • Does the error mean I have too many methods? public? static? package? members?
  • Is it related to the methods/members of my root package, or also to the included JAR libraries?
  • Is there a way to get more debug information about this?
  • 错误是否意味着我有太多方法?民众?静止的?包裹?会员?
  • 它与我的根包的方法/成员有关,还是与包含的 JAR 库有关?
  • 有没有办法获得更多关于此的调试信息?

I already know about that "jumbo" enabling flag addressed in the similar questions here in SO, however, I think jumbo mode is not available on the API level I'm targeting (ICS).

我已经知道 SO 中类似问题中解决的“巨型”启用标志,但是,我认为巨型模式在我的目标 API 级别 (ICS) 上不可用。

采纳答案by Mattia Franchetto

It's related to the number of methods of libraries included in the project. For example if you have tracking in your app, just Google Analytics is ~7000 methods. In one of my projects using Lombok (2MB of JAR) gave me these problem. Solved getting rid of this library.

它与项目中包含的库方法的数量有关。例如,如果您的应用程序中有跟踪功能,那么仅 Google Analytics(分析)就有大约 7000 种方法。在我使用 Lombok(2MB 的 JAR)的项目之一中,我遇到了这些问题。解决了摆脱这个库的问题。

回答by fpanizza

Your error is for the amount of strings (methods, members, etc) in a single dex file.

您的错误是针对单个 dex 文件中字符串(方法、成员等)的数量。

You need to compile you app using jumbo in dex with:

您需要使用 jumbo in dex 编译您的应用程序:

dex.force.jumbo=true

in project.properties

project.properties

This increment the limit for strings in a dex files. And your project will probably compile.

这会增加dex 文件中字符串限制。你的项目可能会编译。

Also with jumbo set, the is another limit of 64K only for methodsin an single dex. If you get this limit in the future , you will need to remove some dependencies.

同样使用 jumbo set,另一个64K限制仅适用于单个 dex 中的方法。如果您将来获得此限制,则需要删除一些依赖项。

UPDATE: for build with Gradle: In Gradle you can enable jumboMode also in the build.gradle file with:

更新:对于使用Gradle构建:在 Gradle 中,您还可以在 build.gradle 文件中启用 jumboMode :

dexOptions {
    jumboMode = true
}

Check: Android Build: Dex Jumbo Mode in Gradle

检查: Android 构建:Gradle 中的 Dex Jumbo 模式

Also with Gradle you can avoid the 64K limit for methodsusing multidex build, tutorial here: https://developer.android.com/tools/building/multidex.html

同样使用 Gradle,您可以避免使用 multidex 构建的方法64K 限制,教程在这里:https: //developer.android.com/tools/building/multidex.html

回答by gary

For gradle build, just add the dexOptions into build.gradle to enable jumbo mode:

对于 gradle 构建,只需将 dexOptions 添加到 build.gradle 以启用巨型模式:

android {
    dexOptions {
        jumboMode = true
    }
}

Remember to run "gradle clean" before your new building.

请记住在新建筑物之前运行“gradle clean”。

回答by omahena

It looks like the problem occurs because all the class files from your project and JAR files are packed together before DEXing. This may not be completely true but any way of controlling this in our project has proven to be quite difficult. Even removing stuff that initially caused this problem, cleaning and rebuilding didn't fix the issue for us in a consistent way.

看起来问题的发生是因为您的项目中的所有类文件和 JAR 文件在 DEXing 之前都打包在一起。这可能不完全正确,但事实证明,在我们的项目中控制这一点的任何方式都非常困难。即使删除最初导致此问题的东西,清理和重建也不能以一致的方式为我们解决问题。

So we took this opportunity to switch our project to Android Studio and managed to solve the problem by turning on ProGuard for debug builds as well. More precisely we only use the shrink phase of the ProGuard's processing chain.

因此,我们借此机会将我们的项目切换到 Android Studio,并通过为调试构建打开 ProGuard 来设法解决问题。更准确地说,我们只使用 ProGuard 处理链的收缩阶段。

Gradle makes it very easy to turn on ProGuard for debug builds:

Gradle 使为调试构建打开 ProGuard 变得非常容易:

buildTypes {
    debug {
        runProguard true
        proguardFile 'proguard-project-debug.txt'
    }
}

And here is the debug ProGuard config we use:

这是我们使用的调试 ProGuard 配置:

-keep class com.your.code.**
# Use -keep to explicitly keep any other classes shrinking would remove
-dontoptimize
-dontobfuscate
-ignorewarnings

This does increase the build time of the project but the good side is that the debugger still works.

这确实增加了项目的构建时间,但好的一面是调试器仍然可以工作。

The only faster alternative I can think of is that any JAR files are manually stripped of the unused class files. But this is not only difficult to do it is also inconvenient when you want to use a slightly larger part of a library at a later time.

我能想到的唯一更快的替代方法是手动删除任何 JAR 文件中未使用的类文件。但这不仅很难做到,而且当您以后想使用库中稍大的部分时也很不方便。

I hope this helps other developers struggling with this issue. And perhaps in the future Google can improve the compiler that does this pruning by default. Our APK DEX file went from 8MB to 2.9MB.

我希望这可以帮助其他开发人员在这个问题上苦苦挣扎。也许在未来,谷歌可以改进默认情况下进行这种修剪的编译器。我们的 APK DEX 文件从 8MB 变成了 2.9MB。

Newer gradle (1.0.0+) versions

较新的 gradle (1.0.0+) 版本

In newer Versions of Android studio (1.0+) the bundled Gradle got updated. There were some changes on how the build mechanism works so your project Gradle file can now take advantage of the minifyEnabledand shrinkResourcesparameters. Current version is 1.1.0.

在较新版本的 Android Studio (1.0+) 中,捆绑的 Gradle 得到了更新。构建机制的工作方式发生了一些变化,因此您的项目 Gradle 文件现在可以利用minifyEnabledshrinkResources参数。当前版本是 1.1.0。

Keeping up with changes on a fast moving platform like Android takes effort but it is often rewarded with new features, tools and faster build times. So updating Android Studio and (carefully) updating your projects is worth the time you invest.

在像 Android 这样快速发展的平台上跟上变化需要付出努力,但它通常会获得新功能、工具和更快的构建时间的回报。因此,更新 Android Studio 和(小心地)更新您的项目是值得您投入时间的。

buildTypes {
    debug {
        proguardFile 'proguard-project-debug.txt'
        minifyEnabled true
        shrinkResources true
    }
}

回答by yuliskov

Some interesting observations. Same error may appear if you have multi-flavor project. It's confusing. Turned out that I attempted run app with generic command: gradlew installDebug. When I've changed command line to look like this problem is gone. Don't forget to replace Flavor part with your actual one.

一些有趣的观察。如果您有多种口味的项目,可能会出现同样的错误。这很混乱。原来,我尝试使用通用命令运行应用程序:gradlew installDebug。当我将命令行更改为看起来这个问题就消失了。不要忘记用您的实际部分替换 Flavor 部分。

gradlew installFlavorDebug