Purpose of proxyProvide in Dagger 2 generated code

前端 未结 1 1573
借酒劲吻你
借酒劲吻你 2021-02-19 12:39

I have this Dagger module. I want to understand the generated code so I can verify that my Dagger configuration is optimal.

<         


        
1条回答
  •  渐次进展
    2021-02-19 13:19

    First off: Dagger generates this code ahead-of-time so that in a modularized build, you get better build performance. Because of that, we don't know which (or both, or neither) that you will need, so we generate both just in case, and assume that Proguard will be able to strip whatever is unused.

    So what are both actually doing?

    The first (the get() method) is invoked when the binding that this factory represents is requested as a Provider. That can happen either directly, or if the binding is scoped, or a few other scenarios.

    The second case is what we call inlining. Suppose you have a @Provides method in a module, and you have a method on your @Component that returns that type. The most ideal code to generate is something like:

    @Override
    public YourBinding y() {
      return YourModule.yourProvidesMethod();
    }
    

    The thing is, that provides method may not be accessible from the same package as your component, so we generate this "proxy" method which gives Dagger the right accessibility. It also makes accessible all of the parameters to that method, erasing them to Object if necessary. And if they do get erased (think of this like generic type erasure), we need to then insert the casts to the correct types inside the proxy method.

    The Provider.get() implementation doesn't need that, because there, all of the types should are accessible by the code that invokes it.

    So to sum up - we want to generate both versions, hopefully you should only use one, and Proguard should clean up the other.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题