Differences and Advantages of Hilt, Dagger2, Koin — 1

Burak iren
Huawei Developers
Published in
4 min readOct 11, 2020

Hello friends, in this article, we will introduce the subject of dependency injection for Android development, as well as discuss the advantages and differences.

What is DI(Dependency Injection)?

Dependency Injection, represented by the letter at the end of the principles abbreviated as SOLID, is a principle that greatly helps programmers. Dependency Injection is passing dependency to other objects or framework( dependency injector). It makes also testing easier.

This technique has many benefits here I’m just listing the most important ones:

  • decoupling the creation of object
  • ability to replace dependencies (eg: Wheel, Battery) without changing the class that uses it(Car)
  • promotes “Code to interface not to implementation” principle
  • ability to create and use mock dependency during test
  • makes our code reusable and clean

Now that we know about the basic concepts of dependency injection we will take a look at the different solutions and frameworks for dependency injection in Android.

DAGGER -2

Dagger 2 is an open source Dependency Injection framework. It was developed by the Square company and then forked by Google and is still being developed. It is called Dagger because it uses the Directed Acyclic Graph structure.

By finding the class that requests dependency in the code, it determines who provides the desired dependency and receives the required dependency from the provider and transmits it to the requester.

What are the main components of Dagger2?

Component : @Component annotation takes a list of modules as an input. The component is used to connect objects to their dependencies, typically by use of overridden inject() methods. In order to use the component, it must be accessible from the parts of the app that need injection.

Module : The @Module annotation tells Dagger that the EtcModule class will provide dependencies for a part of the application. It is normal to have multiple Dagger modules in a project, and it is typical for one of them to provide app-wide dependencies.

Provides : The @Provides annotation tells Dagger that the method provides a certain type of dependency

Singleton: It tells Dagger that there should only be a single instance of that dependency.

Inject : The @Inject annotation tells Dagger that you want it to do an injection of the dependency field.

What @inject needs comes from @ Module, via @Component

KOIN

Koin is a pragmatic lightweight DSL dependency injection framework for Kotlin. Koin is easy, simple & well documented. It is perfect for encapsulated feature/library modules.

Koin is based around its DSL which consists of these keywords:

startKoin() — call module to start it in application

module { } — create a Koin module or a submodule (inside a module)

factory { } — provide a factory bean definition

single { } — provide a bean definition

get() — resolve a component dependency (eager)

inject() — resolve a component dependency in Android components runtime

bind — additional Kotlin type binding for given bean definition

getProperty() — resolve a property

DAGGER HILT

The new Hilt library defines a standard way to do DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically for you.

Hilt is currently in alpha but it’s already quite powerful.

But in Hilt, we don’t need to create a component, include every module, and build for generating DaggerAppComponent class.

@HiltAndroidAppclass 
ExampleApp: Application(){
}

In Hilt, we don’t have to write the ViewModelFactory and ViewModelModule. Instead, Hilt provides @ViewModelInject annotation and @Assisted annotation for SavedStateHandle.

class MainViewModel @ViewModelInject constructor(  
private val mainRepository: MainRepository,
@Assisted private val saveState: SavedStateHandle) : ViewModel(){
}

In Hilt, we don’t need to write those annotations and ActivityModule. Instead, Hilt provides a @AndroidEntryPoint annotation.

@AndroidEntryPointclass 
MainActivity : AppCompatActivity() {
}

What are the main components of Hilt?

@HiltAndroidApp: We need to apply this annotation to our application class, which triggers the code generation and creates the base component.

@AndroidEntryPoint: Hilt creates a dependency container to the Android component that it was assigned so it can inject the dependencies.

@Module : This is used on the classes that create the objects of dependency classes (for the classes that you don’t own — for example, third-party library classes like OkHttp or Retrofit).

@InstallIn : The Hilt module class should also be annotated with @InstallIn to specify the scope of the module. For example, if we annotate the module with @InstallIn(ActivityComponent::class), then the module will bind to the activity lifecycle.

@Provides : This is used on the methods that are inside the module class and provides the dependency objects.

--

--