"Problem with writing output: Too many references to the field: 70185; max - 65536. You can try using the -multi-dex option." when creating an Android project

I hit this error and did not find any hits for the error message, so I decided to share with the solution that I came up with to save anyone else who was faced with a problem repeating my work.

When writing a new Android library (apklib) for use in a (large) application, I get the following error during dexing when I add my new project to the dependency:

write problem: too many field references: 70185; max - 65536.
You can try using the -multi-dex option.
Packaging links:
<... long list of packets with field count rejected ...>

The specific build step with which it comes out 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 in the error message, may be a solution, but I am not the owner of the application project and it already has a big complex build process that I would hesitate to change independently.

I can reproduce this problem with a project without a test library that has literally no fields, but it contains 6000+ fields in the error output. Of the packages listed in the error output, there are a handful with similar 6k + field counts, but then the vast majority have more likely field counts <1k.

This problem is similar to the Too Many Methods problem, which Facebook famously hacked into . The FB solution seems insane, and the only other solutions that I found (for example, this big Android code , or this , this SO answer , this other SO answer ) are all related to changing the main application code, which goes beyond what I I want to make.

Is there any other solution?

+6
source share
1 answer

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

And it looks like this:

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

led to 6k + fields and build failure. Change it according to the main application package

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

successful creation of the project was completed.

Please note that only the package in the manifest changes, I did not make any changes to the Java library source or its layout (the Java package was still com.example.testlibrary with the directory structure to match).

I assume that the name of the other package causes all Android fields to be included in this package again. All packages in the error list with 6k + fields had a different package name than the main application.

I also (later grr) found this blog post which presents the same problem and possible solution.

+2
source

Source: https://habr.com/ru/post/969555/


All Articles