Fixing Dagger 2 Error: "... cannot be provided [...]"
This error occurs when Dagger 2 cannot provide a dependency without an annotated constructor or a method annotated with @Provides. To resolve it:
1. Add an @Inject Constructor
Add an @Inject annotated constructor to the class that is not provided:
class MyDependency { @Inject MyDependency() { /**/ } }
Dagger will then use this constructor to create the instance.
2. Create a @Provides Method in a Module
Alternatively, create a method annotated with @Provides in a module that returns the dependency:
@Module class MyModule { @Provides MyDependency provideMyDependency() { return new MyDependency(); } }
Dagger will use this method to create and provide the dependency.
Additional Considerations
The above is the detailed content of How to Fix Dagger 2's 'cannot be provided' Error?. For more information, please follow other related articles on the PHP Chinese website!