Dependency Injection (DI) is a design pattern used to enhance modularity and testability in software development. In Android, DI is crucial for managing dependencies and ensuring that components receive their required dependencies without creating them directly. This promotes a more maintainable and loosely coupled codebase.
Dependency Injection Frameworks in Android
Several DI frameworks are available for Android, and one of the popular ones is Dagger Hilt. Dagger Hilt is built on top of Dagger 2 and simplifies the setup and usage of dependency injection in Android apps. It is recommended by Google and provides annotations for defining and injecting dependencies effortlessly.
Setting up Dagger Hilt in an Android Project
To use Dagger Hilt in your Android project, you need to set up the necessary dependencies in your project’s build.gradle file:
dependencies {
...
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
}
Defining a Module and Component
In Dagger Hilt, you define modules that provide dependencies and components that specify the dependency injection scope. Here’s an example of a module:
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideApiService(): ApiService {
return ApiService()
}
}
And here’s an example of a component:
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(application: MyApplication)
}
Injecting Dependencies into Android Components
To inject dependencies into Android components like activities, fragments, or view models, you can use the @AndroidEntryPoint
annotation provided by Dagger Hilt. For example:
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var apiService: ApiService
...
}
Using Dagger Hilt for Constructor Injection
Dagger Hilt supports constructor injection, a recommended practice for injecting dependencies into classes. Here’s an example:
@AndroidEntryPoint
class MyViewModel @Inject constructor(private val repository: MyRepository) : ViewModel() {
// ViewModel logic
}
Example Code
Let’s demonstrate Dagger Hilt usage in a simple Android Kotlin application. We’ll define a module, component, and inject dependencies into an activity:
@InstallIn(SingletonComponent::class)
@Module
object AppModule {
@Provides
@Singleton
fun provideApiService(): ApiService {
return ApiService()
}
}
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(application: MyApplication)
}
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var apiService: ApiService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Use the injected ApiService
apiService.doSomething()
}
}
Conclusion
Dagger Hilt is a powerful and efficient dependency injection framework for Android apps built with Kotlin. By using Dagger Hilt, you can achieve better code organization, maintainability, and testability in your Android projects. This guide provides a basic overview and example of how to set up and use Dagger Hilt in your Kotlin Android applications.