Junior — Middle
Can you name the key elements of the Dagger structure used for dependency management?
sobes.tech AI
Answer from AI
Key elements of Dagger's dependency management structure:
- @Module — a class that provides dependencies through methods annotated with @Provides.
- @Provides — an annotation for methods within a module that create and return dependency objects.
- @Component — an interface that connects modules and dependency injection points, responsible for generating code for injection.
- @Inject — an annotation used for constructors, fields, or methods to indicate where and what dependencies should be injected.
Example:
@Module
class NetworkModule {
@Provides
OkHttpClient provideOkHttpClient() {
return new OkHttpClient();
}
}
@Component(modules = NetworkModule.class)
interface AppComponent {
void inject(MainActivity activity);
}