我开始使用 dagger 2.2,组件构建器中的模块方法已被弃用。
这是我的应用程序组件:
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(Application application);
}
以及应用程序模块:
@Module
public class ApplicationModule {
Application application;
public ApplicationModule(Application application) {
this.application = application;
}
@Provides
@Singleton
Application providesApplication() {
return application;
}
}
这是生成的类:
@Generated(
value = "dagger.internal.codegen.ComponentProcessor",
comments = "https://google.github.io/dagger"
)
public final class DaggerApplicationComponent implements ApplicationComponent {
private DaggerApplicationComponent(Builder builder) {
assert builder != null;
}
public static Builder builder() {
return new Builder();
}
public static ApplicationComponent create() {
return builder().build();
}
@Override
public void inject(Application application) {
MembersInjectors.<Application>noOp().injectMembers(application);
}
public static final class Builder {
private Builder() {}
public ApplicationComponent build() {
return new DaggerApplicationComponent(this);
}
/**
* @deprecated This module is declared, but an instance is not used in the component. This method is a no-op. For more, see https://google.github.io/dagger/unused-modules.
*/
@Deprecated
public Builder applicationModule(ApplicationModule applicationModule) {
Preconditions.checkNotNull(applicationModule);
return this;
}
}
}
如果不使用 ComponentBuilder,如何初始化组件?
最佳答案
您应该阅读为什么不推荐使用它的描述。 如果您使用的是 IntelliJ 或 Android Studio 等 IDE,您只需选择方法并点击Control + Q 在 Windows 上阅读 Javadoc,包括弃用通知。
Javadoc 内容如下:
@deprecated This module is declared, but an instance is not used in the component. This method is a no-op. For more, see https://google.github.io/dagger/unused-modules.
从这个链接你可以看到:
When the Dagger processor generates components, it only requires instances of modules and component dependencies that are explicitly needed to supply requests for a binding.
- If all of a module’s methods that are used in the component are static, Dagger does not need an instance of that module at all. Dagger can invoke the static methods directly without a module.
- If a module provides no bindings for a Component, no instance of that module is necessary to construct the graph.
可以肯定地说,您可以忽略弃用。它旨在通知您未使用的方法和模块。只要您在子图中的某处实际需要/使用 Application
,就会需要该模块,并且弃用警告将消失。
关于android - 不推荐使用 Dagger 2.2 组件构建器模块方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36521302/