@Binds methods must have only one parameter whose type is assignable to the return type

做~自己de王妃 提交于 2020-05-13 03:58:40

问题


I am migrating to the new dagger android 2.11

All set up based on the Google blueprint :MVP-Dagger.but I am having this error :

Error:(22, 57) error: @Binds methods must have only one parameter whose type is assignable to the return type

Here in this line :

   @ActivityScoped
    @Binds abstract PresenterFactory<MainContract.Presenter> providePresenterFactory(MainPresenter presenter);

The presenter :

@ActivityScoped
public class MainPresenter extends BasePresenterImpl<MainContract.View>
    implements MainContract.Presenter { public MainPresenter(String s) {..
} ... }

Someone have any idea on how to solve this? Thanks.


回答1:


The error message explains everything:

@Binds methods must have only one parameter whose type is assignable to the return type

Your @Binds method has a parameter of MainPresenter. This is not assignable to the return type PresenterFactory<MainContract.Presenter>. In other words, MainPresenter is not an instance of PresenterFactory<MainContract.Presenter>.

The correct syntax for @Binds methods is something like:

@Binds
abstract Abstraction bindAbstration(Concretion concretion)

where concretion is an instance of Abstraction.

Or, in Kotlin:

@Binds
abstract fun bindAbstraction(concretion: Concretion) : Abstraction

@Binds methods are not magic. They bind a type (for instance, an interface) and an implementation of that type that Dagger knows how to provide already.

update

You can convert your @Provides to @Binds with the following steps:

  1. Make sure MainPresenter has an explicit constructor annotated with @Inject and that Dagger 2 can provide the dependencies in the constructor.
  2. Write:

    @Binds
    abstract MainContract.Presenter bindPresenter(MainPresenter mainPresenter);
    



回答2:


I had the same exception but in a different situation.

The exception was misleading for me. After adding

kapt {
 correctErrorTypes true 
} 

to build.gradle I found the real problem:

error: [ComponentProcessor:MiscError] dagger.internal.codegen.ComponentProcessor was 
unable to process this class because not all of its dependencies could be resolved.
Check for compilation errors or a circular dependency with generated code.

and the hint not all of its dependencies could be resolved helped me to find out that I missed adding implementation project(path: ':repository') to build.gradle of my app module which handles DI stuff.

Hope this help others having same problem.




回答3:


Use @Provides instead of @Binds



来源:https://stackoverflow.com/questions/46656117/binds-methods-must-have-only-one-parameter-whose-type-is-assignable-to-the-retu

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