Dagger 2.2 组件生成器模块方法已弃用
Dagger 2.2 component builder module method deprecated
我开始使用 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;
}
}
这里是生成的class:
@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,如何初始化组件?
您应该阅读 为什么 它被弃用的描述。 如果您使用 IDE 像 IntelliJ 或 Android Studio 你可以 select 方法并点击 Control + Q on 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.
从这个link你可以看出:
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
,就会需要该模块,并且弃用警告将消失。
我和主机有同样的问题,我只是希望每个人都有关于生成的组件构建器的弃用问题class应该检查两件事以节省时间:
1/ 更正模块的匕首语法,组件也仔细检查你注入的位置。
2/ 必须在要注入的地方有注入对象(注入注解及其对象),否则 dagger 编译器看不到在哪里使用你的模块,所以某些方法将 deprecated.Just 注入至少一个模块提供给你的注入位置并重新编译代码,你不会再有那个问题了:)
它显示已弃用,因为您没有在应用程序中使用组件和模块
@Inject
SomeObjectFromModule mSomeObject
如果你没有在你的应用程序中注入依赖项,那么初始化你的组件是没有用的,所以 dagger 至少寻找一种用法
在任何 类 中添加这些行后,您想要注入视图,然后清理构建并重建项目,您的弃用问题将得到解决
当我的 Module
没有 @Provides
方法或 Dagger
提供的对象未在应用程序中使用时显示错误。
删除弃用模块的示例
模块
@Module
public class SecondActivityModule {
@Provides
Book provideBookTest() {
return new Book();
}
}
Activity
public class SecondActivity extends AppCompatActivity {
@Inject
Book test;
...
}
OR 组件
@Component(modules = SecondModule.class)
public interface SecondComponent {
void inject(SecondActivity activity);
Book getBookTest();
}
如果在组件 class 中声明 void inject(AppCompactActivity activity);
,模块方法将被弃用。而不是你必须像下面那样使用紧耦合 void inject(MainActivity activity);
并重建你的项目你会看到,模块 class
中没有弃用方法
我开始使用 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;
}
}
这里是生成的class:
@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,如何初始化组件?
您应该阅读 为什么 它被弃用的描述。 如果您使用 IDE 像 IntelliJ 或 Android Studio 你可以 select 方法并点击 Control + Q on 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.
从这个link你可以看出:
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
,就会需要该模块,并且弃用警告将消失。
我和主机有同样的问题,我只是希望每个人都有关于生成的组件构建器的弃用问题class应该检查两件事以节省时间:
1/ 更正模块的匕首语法,组件也仔细检查你注入的位置。
2/ 必须在要注入的地方有注入对象(注入注解及其对象),否则 dagger 编译器看不到在哪里使用你的模块,所以某些方法将 deprecated.Just 注入至少一个模块提供给你的注入位置并重新编译代码,你不会再有那个问题了:)
它显示已弃用,因为您没有在应用程序中使用组件和模块
@Inject
SomeObjectFromModule mSomeObject
如果你没有在你的应用程序中注入依赖项,那么初始化你的组件是没有用的,所以 dagger 至少寻找一种用法
在任何 类 中添加这些行后,您想要注入视图,然后清理构建并重建项目,您的弃用问题将得到解决
当我的 Module
没有 @Provides
方法或 Dagger
提供的对象未在应用程序中使用时显示错误。
删除弃用模块的示例
模块
@Module
public class SecondActivityModule {
@Provides
Book provideBookTest() {
return new Book();
}
}
Activity
public class SecondActivity extends AppCompatActivity {
@Inject
Book test;
...
}
OR 组件
@Component(modules = SecondModule.class)
public interface SecondComponent {
void inject(SecondActivity activity);
Book getBookTest();
}
如果在组件 class 中声明 void inject(AppCompactActivity activity);
,模块方法将被弃用。而不是你必须像下面那样使用紧耦合 void inject(MainActivity activity);
并重建你的项目你会看到,模块 class