“trouble writing output: Too many field references: 70185; max is 65536. You may try using --multi-dex option.” when building Android project

我们两清 提交于 2019-12-19 02:28:13

问题


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.

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:

trouble writing output: Too many field references: 70185; max is 65536.
You may try using --multi-dex option.
References by package:
<...long list of packages with field counts elided...>

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 ...>

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.

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.

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.

Is there any other solution?


回答1:


The solution was to change the package in the AndroidManifest to match the main application's package.

A manifest like this:

<manifest package="com.example.testlibrary" ...

resulted in 6k+ fields and build failure. Changing it to match the main application's package

<manifest package="com.example.mainapplication" ...

resulted in the project building successfully.

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).

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.

I also (later, grr), found this blog post which details the same problem and the eventual same solution.



来源:https://stackoverflow.com/questions/23749654/trouble-writing-output-too-many-field-references-70185-max-is-65536-you-may

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!