Simplify Your Code with Dagger @BindsInstance and @Component.Builder

Murat AYDIN
4 min readSep 28, 2022

In this blog post, I’ll explain how to use Dagger @BindsInstance and how it simplifies implementation. Dagger is the most used dependency injection framework in Android app development.

Dagger creates the dependency graph for you and decrease the boilerplate code. You can easily get your dependencies with Dagger.

Dagger has a @BindsInstance annotation. It is used for binding instances. It is useful when you need to inject a dependency in runtime. We will see how it simplifies the implementation.

In order to do that, I created an application with and without using @BindsInstance annotation. Source code used in this blog post is here. main branch shows Dagger injection without @BindsInstance and binds branch shows Dagger injection with @BindsInstance.

Now, we will go over the setup created without @BindsInstance which is in main branch in here. There are two feature modules in this application. feature module is a module that provides a Printer class.

@Module
class FeatureModule {
@Provides
@Singleton
fun providesPrinter(context: Context) = Printer(context)
}

logger module is a module that provides a Logger class.

@Module
class LoggerModule {
@Provides…

--

--