android – Dagger 2.2元件構建器模組方法已棄用
我開始使用匕首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/">Android Studio這樣的IDE,您只需選擇該方法並在Windows上點選Control Q即可閱讀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 07000.
從這個連結可以看到:
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.
可以肯定地說,您可以忽略棄用.它旨在通知您未使用的方法和模組.一旦您在子圖中實際需要/使用應用程式,就需要使用模組,棄用警告將會消失.
http://stackoverflow.com/questions/36521302/dagger-2-2-component-builder-module-method-deprecated