In object-oriented programming (OOP), especially in Android Development, to make our program code cleaner and maintainable, one of the principles we are used to apply is SOLID. one of the points in the SOLID principle is Dependency Inversion.
One should depend upon abstractions, not concretions.
Inversion of Control (IoC) is a design pattern used to represent Dependency Inversion in real software.
That makes Dependency Injection popular.
What is Dependency Injection?
Dependency injection (DI) is a software engineering technique that involves making the interactions between objects as thin as possible through specific dependencies. This allows for loosely coupled code — or code that only depends on the required portion of a separate class — to run. And it reduces hard-coded dependencies and allows for cleaner, more flexible code.
some of most popular Dependency Injection frameworks are Koin and Hilt.
Then what's different?
Let’s start by talking about Koin first.
Koin stands out for its simplicity and flexibility. It is a simple DI framework that does not require annotations or code generation. Instead, it makes use of the Domain Specific Language (DSL) provided by Kotlin to specify dependencies and modules.
Koin’s ease of use is one of its benefits. Its minimum setup requirements and its ability to create dependencies with a clear, readable syntax are the aspects that make it stand out. Therefore, Koin is easy for developers who are new to DI.
On the other hand, this features of Koin might come with trade-offs in certain scenarios. Its runtime resolution might lead to potential issues during compile-time if dependencies are not properly configured. Additionally, Koin might not offer the same level of performance optimizations that some other DI frameworks, like Hilt, provide.
How to use Koin?
First, add dependencies to our project’s build.gradle file,
dependencies {
// Koin core features
implementation 'org.koin:koin-core:3.2.0'
// Koin Android features
implementation 'org.koin:koin-android:3.2.0'
implementation 'org.koin:koin-androidx-viewmodel:3.2.0'
}
Then, create Koin modules,
import org.koin.dsl.module
val myKoinModule = module {
single { UserRepository() }
factory { UserViewModel(get()) }
// Define other dependencies...
}
single: create an object that persistent with the entire container lifetime. It does not create a new instance with each call.
factory: create a new object each time. Short live. No persistence in the container
Then in Application class, initialize Koin by loading the modules,
class MyKoinApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyKoinApplication )
modules(myKoinModule )
}
}
}
This is how we call at Activity
class HomeActivity : AppCompatActivity() {
private val viewModel by viewModel<UserViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Use viewModel or other dependencies...
}
}
how about Hilt?
Hilt, developed by Google and built on top of Dagger, is a powerful dependency injection (DI) framework tailored for Android. It simplifies DI by leveraging annotations and compile-time checks to ensure correctness and performance. Additionally, it integrates seamlessly with Jetpack libraries like ViewModel and WorkManager, making it easier to manage dependencies across different Android components.
One of Hilt’s biggest advantages is its strong alignment with the Android development ecosystem, making it a preferred choice for larger projects following architectural patterns like MVVM or Clean Architecture. However, its learning curve can be steep due to its reliance on Dagger and the specific annotations required. Compared to Koin, Hilt may also involve a more complex setup, but it offers better compile-time safety and performance benefits.
How to use Hilt
Add Hilt dependency at our project’s build.gradle file,
dependencies {
// Other dependencies...
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_lifecycle_version"
kapt "androidx.hilt:hilt-compiler:$hilt_lifecycle_version"
}
Enable Hilt to our Application
@HiltAndroidApp
class MyHiltApplication : Application()
Creating Hilt modules
@Module
@InstallIn(ApplicationComponent::class)
object MyModule {
@Provides
fun provideMyRepository(): MyRepository {
return MyRepository()
}
@Provides
fun provideMyViewModel(repository: MyRepository): MyViewModel {
return MyViewModel(repository)
}
}
Using dependencies in Activity/Fragment,
@AndroidEntryPoint
class MyActivity : AppCompatActivity() {
private val viewModel: MyViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
There is 3 different characteristics between Koin and Hilt:
Syntax and Configuration: Koin offers a simple and readable syntax without relying on annotations or code generation, while Hilt heavily depends on annotations like @Inject and @Module for dependency injection.
Learning Curve and Complexity: Koin is beginner-friendly with minimal setup, making it easier to adopt. In contrast, Hilt has a steeper learning curve due to its integration with Dagger and required annotations.
Performance and Optimization: Koin has faster build times and a smaller APK size since it doesn’t generate code, but it is slightly slower at runtime due to resolving dependencies dynamically. Hilt, using code generation, ensures better runtime performance but can lead to longer build times and a larger APK.
So when to use Koin and Hilt?
_Koin _is ideal for smaller projects due to its simplicity and flexibility, making code more modular and maintainable. However, for larger projects requiring deep integration with Android components, _Hilt _is preferable for its performance optimizations and alignment with Android architecture.
references:
https://medium.com/ibtech/hilt-vs-koin-f3532b5796eb
https://en.wikipedia.org/wiki/Dependency_injection
https://simple.wikipedia.org/wiki/SOLID(object-oriented_design)_
https://medium.com/@srimanikandan92/hilt-vs-koin-a-simple-guide-to-android-dependency-injection-ef77d00e7cf6
Top comments (0)