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.
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:
MainPresenter
has an explicit constructor annotated with @Inject
and that Dagger 2 can provide the dependencies in the constructor.Write:
@Binds
abstract MainContract.Presenter bindPresenter(MainPresenter mainPresenter);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With