Dagger @Reusable scope vs @Singleton

Use @Singleton if you rely on singleton behavior and guarantees. Use @Reusable if an object would only be a @Singleton for performance reasons. @Reusable bindings have much more in common with unscoped bindings than @Singleton bindings: You’re telling Dagger that you’d be fine creating a brand-new object, but if there’s a convenient object already created … Read more

How do you override a module/dependency in a unit test with Dagger 2.0?

Probably this is more a workaround that proper support for test module overriding, but it allows to override production modules with test one. The code snippets below shows simple case when you have just one component and one module, but this should work for any scenario. It requires a lot of boilerplate and code repetition … 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

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

Error : Program type already present: android.support.design.widget.CoordinatorLayout$Behavior

It worked when I downgrade the support appcompat gradle dependency, like follwing : implementation ‘com.android.support:appcompat-v7:27.0.2’ previously it was implementation ‘com.android.support:appcompat-v7:27.1.0’ OR Also this can be fixed by just adding support design dependency of version 27.1.0 or above to your app level build.gradle as following : implementation ‘com.android.support:design:27.1.0’

How to set up DAGGER dependency injection from scratch in Android project?

Guide for Dagger 2.x (Revised Edition 6): The steps are the following: 1.) add Dagger to your build.gradle files: top level build.gradle: . // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.2.0’ classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ //added apt for source code generation … 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

tech