Generating a MembersInjector for [Class]. Prefer to run the dagger processor over that class instead

人走茶凉 提交于 2020-01-01 04:04:13

问题


I have these warnings and I don't understand what they mean. Can someone point me to something?

For classes I inject into (where there is a component.inject(this) statement)

Note: Generating a MembersInjector for [class] Prefer to run the dagger processor over that class instead.

For object I am injecting (constuctor annotated with @Inject)

Note: Generating a Provider for [class]. Prefer to run the dagger processor over that class instead.

回答1:


When Dagger's annotation processor runs, it generates two types of classes:

  1. Implementations of @Component interfaces
  2. Provider and MembersInjector implementations for each @Inject'd type.

While it's generating the @Component interface implementation, it connects each of the Provider and MembersInjector implementations according to how your modules were configured. If your component or any of the modules therein refer to an @Inject'd type that was compiled without the Dagger processor it will still generate the Provider or MembersInjector, but once for each component rather than once for the @Inject'd class.

This isn't really a problem (hence not a warning or error), but it does mean that can potentially have the Dagger processor generate the same classes many times for a single application. It might slow down compilation if and take up a bit more bytecode if it really gets out of hand.

The easy fix is just to make sure that you're running the Dagger annotation processor when you compile your @Inject'd types as well as your components.




回答2:


I was getting this "Generating a MembersInjector for..." Dagger 2 warning when I had a superclass and subclass as follows...

public abstract class BaseActivity extends Actvity {

    @Inject
    DependencyA dependencyA;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyApplication) getApplication()).getComponent().inject(this);
}

public class ConcreteActivity extends BaseActvity {

    @Inject
    DependencyB dependencyB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyApplication) getApplication()).getComponent().inject(this);
    }
}

... and my Component interface had two inject methods as follows...

void inject(BaseActivity activity);
void inject(ConcreteActivity activity);

I removed the inject(BaseActivity activity) method from my Component interface and I removed the Component.inject(this) method call from the BaseActivity class. The injection in BaseActivity still works as expected when the subclass (ConcreteActivity) calls Component.inject(this) and I do not see the "Generating a MembersInjector for..." Dagger 2 warning anymore when I build the application.



来源:https://stackoverflow.com/questions/37437873/generating-a-membersinjector-for-class-prefer-to-run-the-dagger-processor-ove

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!