Android — Dependency Injection — Dagger2

Dependency Injection framework

Hari Prasad
2 min readAug 30, 2023

Dependency Injection — Giving object to its instance variables.

  • Dependency Injection (DI) is a software design pattern where components and their dependencies are decoupled by providing the required dependencies from external sources, such as constructors, methods, or properties, instead of the components creating their dependencies internally.
  • Dependency Injection (DI) is a pattern that separates the creation and management of object instances from the classes that use those instances.
  • This pattern improves modularity, testability, and maintainability by reducing tight coupling between components and enabling easier substitution of implementations.

Benefits

  • Modularity: Components are decoupled from their dependencies, making it easier to replace, update, or extend them.
  • Testability: Components can be easily tested in isolation by providing mock or fake dependencies during testing.
  • Maintainability: Changes to dependencies don’t affect the internals of components, reducing the risk of bugs and making maintenance simpler.
  • Flexibility: Different implementations of dependencies can be swapped in without changing the code of the dependent class.
  • Decoupling: Classes become less dependent on specific implementations of their dependencies, promoting modularity and easier maintenance.
  • Flexibility: Different implementations of a dependency can be easily swapped in without affecting the consuming classes.
  • Readability: Code becomes more readable and clear, as the creation logic is moved out of the main business logic.

Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android.

  • Dagger handles the creation, injection, and lifetime of objects.

Types of Dependency Injection:

  • Constructor Injection: Dependencies are provided through the constructor of a class.
  • Method Injection: Dependencies are provided as arguments to methods.
  • Property Injection: Dependencies are assigned to properties of a class.

How it works

  • Consumer — @Inject
  • Producer —@Module @Provides
  • Connector — @Component

Example

  • Create Modules
@Module
class AppModule {
@Provides
fun provideApiService(): ApiService {
return ApiService()
}
}
  • Create Components
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(activity: MainActivity)
}
  • Inject Dependencies:
class MainActivity : AppCompatActivity() {
@Inject
lateinit var apiService: ApiService

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val appComponent = DaggerAppComponent.create()
appComponent.inject(this)

// Now apiService is ready to use
}
}
  • Application Class
class MyApplication : Application() {
val appComponent: AppComponent by lazy {
DaggerAppComponent.create()
}
}

Scope

  • Scope is one of annotations that you can add in a component interface or in provide method in a module.
  • It’s used for controlling dependencies lifetime
  • Scope is a way to tell Dagger of dependencies lifetime.
  • @Singleton a predefined scope by Dagger

--

--