Subcomponents create a lot of synthetic accessor methods.
See original GitHub issueAs 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:
- Created 7 years ago
- Reactions:12
- Comments:11 (1 by maintainers)
Top GitHub Comments
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
@Component
s, a component dependency between them, multiple@Module
s,@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!
Great! In that case I’m going to close this.