简体   繁体   English

Dagger 2.2 组件构建器模块方法已弃用

[英]Dagger 2.2 component builder module method deprecated

I started using dagger 2.2 and the module methods in the Component builder are deprecated.我开始使用 dagger 2.2,不推荐使用组件构建器中的模块方法。

This is my Application component :这是我的应用程序组件:

@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
    void inject(Application application);
}

And the Application module:和应用程序模块:

@Module
public class ApplicationModule {
    Application application;

    public ApplicationModule(Application application) {
        this.application = application;
    }

    @Provides
    @Singleton
    Application providesApplication() {
        return application;
    }
}

Here is the generated 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;
    }
  }
}

How do I initialize the component if not with the ComponentBuilder?如果不使用 ComponentBuilder,如何初始化组件?

You should read the description of why it is deprecated.你应该阅读它被弃用的原因的描述。 If you are using an IDE like IntelliJ or Android Studio you can just select the method and hit Control + Q on Windows to read the Javadoc including the deprecation notice.如果您使用的是 IntelliJ 或 Android Studio 等 IDE,您只需选择该方法并在 Windows 上点击Control + Q即可阅读包括弃用通知在内的 Javadoc。

The Javadoc reads: Javadoc 写道:

@deprecated This module is declared, but an instance is not used in the component. @deprecated 该模块已声明,但组件中未使用实例。 This method is a no-op.这种方法是无操作的。 For more, see https://google.github.io/dagger/unused-modules .有关更多信息,请参阅https://google.github.io/dagger/unused-modules

And from this link you can see:从这个链接你可以看到:

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.当 Dagger 处理器生成组件时,它只需要提供明确需要的模块和组件依赖项的实例来提供绑定请求。

  • 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 根本不需要该模块的实例。 Dagger can invoke the static methods directly without a module. Dagger 可以在没有模块的情况下直接调用静态方法。
  • If a module provides no bindings for a Component, no instance of that module is necessary to construct the graph.如果模块没有为组件提供绑定,则构建图不需要该模块的实例。

It is safe to say that you can just ignore the deprecation.可以肯定地说,您可以忽略弃用。 It is intended to notify you of unused methods and modules.它旨在通知您未使用的方法和模块。 As soon as you actually require / use Application somewhere in your subgraph the module is going to be needed, and the deprecation warning will go away.一旦您在子图中的某处实际需要/使用Application ,就会需要该模块,并且弃用警告将消失。

It show deprecated because you are not using Component and module in your application by它显示已弃用,因为您没有在应用程序中使用组件和模块

@Inject
SomeObjectFromModule mSomeObject

if you are not injecting dependencies in your applications there is no use of initialising your component so dagger look for at least one usage如果你没有在你的应用程序中注入依赖项,那么初始化你的组件是没有用的,所以匕首寻找至少一种用法

once you add these lines in any classes you want to inject views and then clean build and rebuild the project and your deprecation will be solved一旦您将这些行添加到您想要注入视图的任何类中,然后清理构建和重建项目,您的弃用将得到解决

It showing error when my Module have no @Provides method or the object that provide by Dagger is not used in app.当我的Module没有@Provides方法或应用程序中未使用Dagger提供的对象时,它会显示错误。
Example to remove deprecated module删除不推荐使用的模块的示例

Module模块

@Module
public class SecondActivityModule {
    @Provides
    Book provideBookTest() {
        return new Book();
    }
}

Activity活动

public class SecondActivity extends AppCompatActivity {
    @Inject
    Book test;
    ...
}

OR in Component在组件中

@Component(modules = SecondModule.class)
public interface SecondComponent {

    void inject(SecondActivity activity);

    Book getBookTest();
}

I have the same problem with host and I just want everyone has deprecated issue on Generated component builder class should check two things to save time:我对主机有同样的问题,我只是希望每个人都已弃用生成的组件构建器类的问题,应该检查两件事以节省时间:

1/ Correct dagger syntax for module, component also check carefully where you inject. 1/ 更正模块的 dagger 语法,组件还仔细检查注入的位置。

2/ Must have injection object (inject annotation and its object) in place you want to inject or else the dagger compiler cannot see where to use your module so some method will be deprecated.Just inject at least one module's provides to your injection place and re-compile the code, you won't have that issue anymore :) 2/ 必须有你想要注入的注入对象(注入注解及其对象),否则 dagger 编译器无法看到在哪里使用你的模块,所以一些方法将被弃用。只需将至少一个模块的提供注入你的注入位置和重新编译代码,你就不会再有这个问题了:)

you will get module method deprecated if you declare void inject(AppCompactActivity activity);如果您声明void inject(AppCompactActivity activity);您将弃用模块方法void inject(AppCompactActivity activity); in component class.在组件类中。 instead of you have to use tight coupling like following void inject(MainActivity activity);而不是您必须使用紧耦合,如以下void inject(MainActivity activity); and rebuild your project you will see, there is no deprecate method in module class并重建您的项目,您将看到,模块类中没有弃用方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM