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.

Subcomponents create a lot of synthetic accessor methods.

See original GitHub issue

As I’m sure you’re aware, Android is sensitive to method count and accessing private members across nested class boundaries causes javac to create synthetic accessor methods. Dagger subcomponents are implemented using nested classes in the generated source. These subcomponents access providers from the enclosing component which are stored in private fields. As a result, lots of synthetic accessor methods are created when subcomponents are used.

A simple fix here is to make all fields package-private.

While this does increase the visibility, user code never references subcomponent implementation instances directly where they could potentially access them. We only reference the static builder() and the nested Builder class. The Builder’s build() method returns the interface type, not the implementation type. While you could in theory cast back to the implementation type, doing so is highly unlikely and isn’t really different than your ability to use reflection to get at the fields currently.

For some numbers, a small app using Dagger with a large component and two medium-sized subcomponents receives ~100 of these synthetic accessor methods. Square Register, a non-trivial large app, receives ~1000 of these synthetic accessor methods which itself is larger than some of the third-party libraries it uses and nearly 2% of the 65k single dex limit.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
JakeWhartoncommented, Jan 30, 2019

Oh yeah thanks I forgot to follow up.

I did a bunch of experiments with R8 and it absolutely destroys Dagger (in the best way). I managed to write a super contrived example with two @Components, a component dependency between them, multiple @Modules, @Binds usage, static and instance @Provides, @BindsInstance usage, and members injection which all combined to create a single string and in some cases Dagger disappeared completely and all that was left was some StringBuilder usage (which is why I also filed https://issuetracker.google.com/issues/113859361). Varying the sample would cause certain things to be retained which led to those issues being filed. In general, though, I was amazed at what R8 could do.

But in every case all of the synthetic accessors were all eliminated!

0reactions
ronshapirocommented, Jan 31, 2019

Great! In that case I’m going to close this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Synthetic Accessors in Kotlin - Medium
As you may know, accessing private methods or fields in Java from nested or anonymous inner classes results in the creation of synthetic...
Read more >
Synthetic accessor method warning - java - Stack Overflow
When using private , javac will instead generate a synthetic accessor, which itself is just a getter method with Java's default visibility ...
Read more >
Using Dagger in multi-module apps - Android Developers
The Using Dagger in Android apps doc page covers how to create and use subcomponents. However, you cannot use the same code because...
Read more >
1926.502 - Fall protection systems criteria and practices. - OSHA
Guardrail systems used on ramps and runways shall be erected along each unprotected side or edge. 1926.502(b)(15). Manila, plastic or synthetic rope being...
Read more >
49 CFR Part 173 -- Shippers - General Requirements ... - eCFR
Methods of manufacture, packing, and storage of hazardous materials, ... subparts P and Q of this subchapter at the Packing Group II or...
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