Sobes.tech
Junior — Middle

What components are included in the Dagger dependency injection framework?

sobes.tech AI

Answer from AI

The main components of Dagger are:

  • @Module — a class that provides dependencies through methods annotated with @Provides.
  • @Component — an interface that connects modules and dependency injection sites. It is responsible for generating code for injection.
  • @Inject — an annotation used to mark constructors, fields, or methods where dependencies should be injected.
  • @Scope — an annotation to define the scope of dependencies (e.g., Singleton).

Example:

@Module
class NetworkModule {
    @Provides
    Retrofit provideRetrofit() {
        return new Retrofit.Builder()...build();
    }
}

@Component(modules = NetworkModule.class)
interface AppComponent {
    void inject(MainActivity activity);
}

class MainActivity {
    @Inject Retrofit retrofit;

    void onCreate() {
        DaggerAppComponent.create().inject(this);
    }
}
What components are included in the Dagger dependency… - sobes.tech