question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Too many field references in Uber R.java jar to fit in a single dex

See original GitHub issue

In 4cf0eb7a744c9583cc3f45f7952ef3aa6ae8afae , the list of fields to be included in R.java was changed from computing from the resource xml references and just using the R.txt from prebuilt aars instead. This means whatever is in R.txt gets treated as if it was declared in the package that is in the aar’s manifest.

This becomes a problem in large apps that consume many prebuilt aars. If these aars depend on libraries like the android support library, gms etc, the field references of used resources coming from such dependencies will be counted against each prebuilt aar.

For example, if gms had a color resource

public static class color {
    public static final int common_google_signin_btn_text_dark=0x7f0700b0;
}

referenced in an app and aarX and aarY (both depended on by app) depend on gms, the Uber R.java jar will contain both com.aarX.R.color.common_google_signin_btn_text_dark and com.aarY.R.color.common_google_signin_btn_text_dark even if the app only refers to com.aarX.R.color.common_google_signin_btn_text_dark in its code.

This leads to a situation where all the R.java files cannot fit in a single jar to be dexed/predex because the number of filed references is > 65535.

com.facebook.buck.step.StepFailedException: Failed on step dx_&&_write_file with an exception:
field ID not in [0, 0xffff]: 65536
com.android.dex.DexIndexOverflowException: field ID not in [0, 0xffff]: 65536

trim_resource_ids does not help much because the field reference count in the custom dx packaged with buck only looks at field names and not the fully qualified name of field references for simplicity.

Had a long conversation regarding this with @dreiss yesterday. I seem to see two possible solutions to this

  • Make the referenced resource calculation in dx more fine grained by including the package name + resource name instead of just the resource name
  • Provide an option to list a primary_dex_resource_patterns and split the Uber R.java jar into multiple jars if linear_alloc_splt_dex is enabled so resources needed in the primary dex can still be kept like today.

If there are any other workarounds or other solutions, would love to know so we can try the best alternative

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
zongwu233commented, Aug 18, 2016

I solve this problem in this way: split Uber R.java into two parts ,one is the ${packagename}.R.java and the rest R.java. ${packagename}.R.java as UberRDotJava is trimed, compiled and dexed as before, and the rest R.java is compiled, dexed and add into preDexedLibraries . then primer dex’s fields is much less than before .

0reactions
ChaitanyaPramodcommented, Feb 24, 2017

Hi, some us continue to see a similar issue over at #926 which appears to be strongly related to this issue. Can we get some help with #926?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"trouble writing output: Too many field references: 70185; max ...
The solution was to change the package in the AndroidManifest to match the main application's package. A manifest like this:
Read more >
Getting "Too many field references to fit in one dex file" after ...
I reinstalled my SDKs to fix an Emulator problem. Then I got Androidx related issues because I am using b4A 9.01 and the...
Read more >
Shrink, obfuscate, and optimize your app - Android Developers
Code shrinking (or tree-shaking): detects and safely removes unused classes, fields, methods, and attributes from your app and its library dependencies (making ...
Read more >
SDK 8 : Manual Ant Compile failed with : lib_rules.xml:121 ...
I have a related problem where classes.dex wouldn't get the classes from external libraries included in my app. The fix... ... It's ugly...
Read more >
Diff - 8befd6fd051e3c5d7c8e4c804664b8c69f91335a^2 ...
RemoteConnection); field public static final java.lang. ... + case 6: return ".so mmap"; + case 7: return ".jar mmap"; + case 8: return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found