Dagger 2.2 component builder module method deprecated

半腔热情 提交于 2019-11-28 16:17:47

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.

The Javadoc reads:

@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.

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.

  • 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.

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.

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.
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.

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 :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!