Junior — Middle
What is the purpose of the Provides annotation in Dagger and how is it used to provide dependencies?
sobes.tech AI
Answer from AI
The @Provides annotation in Dagger is used inside a module to indicate a method that creates and supplies a dependency. Such a method returns an object that will be injected into other components.
Example of usage:
@Module
class NetworkModule {
@Provides
OkHttpClient provideOkHttpClient() {
return new OkHttpClient.Builder().build();
}
}
Here, the provideOkHttpClient method creates and returns an instance of OkHttpClient. Dagger uses this method to supply the object when needed for injection into other classes. This allows centralized management of dependency creation and configuration.