Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of Dagger 2 warning "Generating a MembersInjector"

Given the following classes

abstract class AbstractClass {
    @Inject SomeDependency someDependency;
}

class SomeClass extends AbstractClass {
    @Inject AnotherDependency anotherDepenency;

    public void onCreate() {
        component = // Get component instance somehow
        component.inject(this);
    }
}

in Dagger 2 when injecting dependencies into a class which extends from an abstract base class which also contains dependencies, Dagger shows a warning of the kind Generating a MembersInjector for AbstractClass. Prefer to run the dagger processor over that class instead. during compilation.

However if I override/implement onCreate() in AbstractClass and call the dependency injection there, too, the dependency someDependency will be injected twice which might lead to unexpected behaviour. Once in onCreate() of AbstractClass and once in onCreate() of SomeClass.

What is the best solution to get rid of this warning while preventing duplicate injection of dependencies?

like image 814
Sven Jacobs Avatar asked Apr 04 '16 06:04

Sven Jacobs


2 Answers

As of Dagger 2.9 these warnings are off by default.

like image 132
Sven Jacobs Avatar answered Oct 07 '22 20:10

Sven Jacobs


Solution could be: define onCreate() in Abstract class only

like image 1
Konstantin Pribluda Avatar answered Oct 07 '22 22:10

Konstantin Pribluda