What is the purpose of the getter methods in Components in Dagger 2?

Usage in dependent components In the context of a hierarchy of dependent components, such as in this example, provision methods such as Foo foo() are for exposing bindings to a dependent component. “Expose” means “make available” or even “publish”. Note that the name of the method itself is actually irrelevant. Some programmers choose to name … Read more

What is the use case for @Binds vs @Provides annotation in Dagger2

@Binds can be perfectly equivalent to a @Provides-annotated method like this: @Provides public HomePresenter provideHomePresenter() { return new HomePresenterImp(); } …though you’d probably prefer a variant that takes HomePresenterImp as a method parameter, which lets Dagger instantiate HomePresenterImp (assuming it has an @Inject constructor) including passing any dependencies it needs. You can also make this … Read more

Set dynamic base url using Retrofit 2.0 and Dagger 2

Support for this use-case was removed in Retrofit2. The recommendation is to use an OkHttp interceptor instead. HostSelectionInterceptor made by swankjesse import java.io.IOException; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; /** An interceptor that allows runtime changes to the URL hostname. */ public final class HostSelectionInterceptor implements Interceptor { private volatile String host; public … Read more

Dagger 2 injecting Android Application Context

@Module public class MainActivityModule { private final Context context; public MainActivityModule (Context context) { this.context = context; } @Provides //scope is not necessary for parameters stored within the module public Context context() { return context; } } @Component(modules={MainActivityModule.class}) @Singleton public interface MainActivityComponent { Context context(); void inject(MainActivity mainActivity); } And then MainActivityComponent mainActivityComponent = DaggerMainActivityComponent.builder() … Read more

What determines the lifecycle of a component (object graph) in Dagger 2?

As for your question What determines the lifecycle of a component (object graph) in Dagger 2? The short answer is you determine it. Your components can be given a scope, such as @Scope @Retention(RetentionPolicy.RUNTIME) public @interface ApplicationScope { } @Scope @Retention(RetentionPolicy.RUNTIME) public @interface ActivityScope { } These are useful for you for two things: Validation … Read more

Implementing a simple Dagger2 sample

AppModule.kt: Provide the application context. No need to write @singleton @provides for your Test* classes (will see why) @Module class AppModule { @Provides @Singleton fun provideApplication(app: App): Context = app.applicationContext } AppComponent.kt: @Component.Builder is deprecated IIRC. Use @Component.Factory. And replace AndroidInjectionModule::class with AndroidSupportInjectionModule::class since we are using dagger-android-support and android’s *Compat* stuff. Refer a new … Read more