问题描述
我遇到了这个错误,但没有发现该错误消息的命中率,所以我想我会分享我想出的解决方案,以拯救面临问题的其他人重复我的工作.
I hit this error and found no hits for the error message, so I thought I'd share the solution I came up with to save anyone else facing the problem repeating my work.
在编写用于(大型)应用程序的新 Android 库 (apklib) 时,将新项目添加为依赖项时,在 dexing 期间出现以下错误:
When writing a new Android library (apklib) for use in a (large) application, I'm getting the following error during dexing when I add my new project as a dependency:
输出写入问题:字段引用过多:70185;最大值为 65536.
您可以尝试使用 --multi-dex 选项.
按包引用:
<...省略了字段计数的长包列表...>
它失败的特定构建步骤是:
The particular build step it fails on is:
java -jar $ANDROID_SDK/build-tools/19.0.3/lib/dx.jar --dex \
--output=$PROJECT_HOME/target/classes.dex \
<... long list of apklib and jar dependencies elided ...>
使用错误消息推荐的 --multi-dex
可能是一个解决方案,但我不是应用程序项目的所有者,它已经有一个大型复杂的构建过程,我会犹豫要不要改变.
Using --multi-dex
as recommended by the error message might be a solution, but I'm not the owner of the application project and it already has a large complex build process that I would hesitate to change regardless.
我可以用一个几乎没有字段的无操作测试库项目重现这个问题,但在错误输出中它被列为有 6000 多个字段.在错误输出中列出的包中,有少数具有类似的 6k+ 字段计数,但绝大多数具有更合理的
I can reproduce this problem with a no-op test library project that has literally no fields, but in the error output it's listed as having 6000+ fields. Of the packages listed in the error output, there are a handful with similar 6k+ field counts, but then the vast majority have more plausible <1k field counts.
这个问题类似于 Facebook 以黑客方式而闻名.FB 解决方案似乎很疯狂,我发现的唯一其他解决方案(例如,这个 Android 错误票、或这个a>、这个 SO 答案、this other SO answer)都涉及更改主应用程序的代码,这远远超出了我想要做的范围.
This problem is similar to the "Too many methods" problem that Facebook famously hacked their way around. The FB solution seems insane, and the only other solutions I've found (e.g., this Android bug ticket, or this one, this SO answer, this other SO answer) all involve changing the main app's code which is well beyond the scope of what I want to do.
还有其他解决办法吗?
推荐答案
解决方案是更改 package 在 AndroidManifest 中以匹配主应用程序的包.
The solution was to change the package in the AndroidManifest to match the main application's package.
这样的清单:
<manifest package="com.example.testlibrary" ...
导致 6k+ 字段和构建失败.更改它以匹配主应用程序的包
resulted in 6k+ fields and build failure. Changing it to match the main application's package
<manifest package="com.example.mainapplication" ...
导致项目构建成功.
请注意,只有清单中的包发生了变化,我没有对库的 Java 源代码或其布局进行任何更改(Java 包仍然是 com.example.testlibrary,目录结构与之匹配).
Note that only the package in the manifest is changing, I did not make any changes to the library's Java source or its layout (the Java package was still com.example.testlibrary with directory structure to match).
我假设不同的包名称导致所有 Android 字段再次包含在该包下.错误列表中包含 6k+ 字段的所有包的包名都与主应用程序不同.
I hypothesize that the different package name is causing all the Android fields to be included again under that package. All the packages in the error listing with 6k+ fields had a different package name than the main application.
我也(后来,grr)发现 这篇博文 详细介绍了相同的问题和最终相同的解决方案.
I also (later, grr), found this blog post which details the same problem and the eventual same solution.
这篇关于"写入输出时遇到问题:字段引用过多:70185;最大值为 65536.您可以尝试使用 --multi-dex 选项."构建Android项目时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!