本文介绍了摆脱Dagger 2警告“生成一个MembersInjector”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下类

 抽象类AbstractClass {
@Inject SomeDependency someDependency;
}

class SomeClass扩展AbstractClass {
@Inject AnotherDependency anotherDepenency;

public void onCreate(){
component = //以某种方式获取组件实例
component.inject(this);
}
}

在Dagger 2中将依赖项注入到扩展的类中在一个也包含依赖项的抽象基类中,Dagger显示了一种类型的警告为AbstractClass生成一个MembersInjector。更喜欢在该类上运行匕首处理器。在编译期间。



但是如果我覆盖/实现 onCreate( ) AbstractClass 中并在那里调用依赖注入,依赖 someDependency 将是注入两次可能导致意外行为。一旦进入 onCreate() AbstractClass ,一次进入 onCreate() of SomeClass



什么是在防止重复注入依赖项时摆脱此警告的最佳解决方案从这些警告默认是关闭的。


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?

解决方案

As of Dagger 2.9 these warnings are off by default.

这篇关于摆脱Dagger 2警告“生成一个MembersInjector”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:51