What is Hilt?
The Hilt is a Dependency injection library. It helps to reduce boilerplate code. In Dagger Hilt, we have containers for every class in android. Hilt is built on dagger which means it has advantages of Dagger like compile-time correctness, runtime performance, scalability, and studio support. The hilt is a pure DI, it’s not a Service locator like Koin. Don’t know the difference between DI and Service locator? So here we go.
Difference Between service Locator and DI
DI Container | Service locator |
---|---|
We have to use a container to create all of objects | Here the client code request the dependencies (Objects) |
DI Container is located somewhere outside and it takes services from some storage and pushes them to the consumer | Computation fees on the Ethereum network. |
Getting Started
Firstly we need to add dependency for adding hilt in our android project. So for that just add this in project level build.gradle.
buildscript { dependencies { classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha' } }
Now add this in your app-level build.gradle
apply plugin: 'kotlin-kapt' apply plugin: 'dagger.hilt.android.plugin' dependencies { implementation "com.google.dagger:hilt-android:2.28-alpha" kapt "com.google.dagger:hilt-android-compiler:2.28-alpha" }
And the last thing, don’t forget to enable Java 8 in your project ashilt use java 8 features
android { ... compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }
Scoping Instance to Container
Sometimes the scope of an instance matters a lot, so hlit provides scoping annotation to manage the scope of instance. Hlit provides scoping based on its components. Hlit components define the scope for any instance. So here we have a component hierarchy.
So here we can see if we define any instances as singleton scope, then it creates a single object for the whole application, if we define activityScope then it creates a single object per activity and the same conditions apply to fragment and viewscope.
Hilt and Jetpack Support
Yes, you heard it right, Hilt can be used with jetpack components and currently Viewmodel and Workmanger is supported by Hilt. We can use Hilt’s extensions for using Viewmodel and Workmanger with hilt.
dependencies { // When using ViewModel. implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01' // When using workmanger. implementation 'androidx.hilt:hilt-work:1.0.0-alpha01' // When using Kotlin. kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01' // When using Java. annotationProcessor 'androidx.hilt:hilt-compiler:1.0.0-alpha01' }
Yes! We have successfully completed the setup.
Let’s jump into code
Simple Class Injection
Defining an injectable class is very easy in hlit, You just need to annotate it constructor with @inject annotation.
class Student @Inject constructor() { //some code.... }
If you want access to the object you have to create a simple variable and add @Inject annotation, then it will automatically initialize the object when you will use it for the first time. This is called lazy initialization. Don’t forget to add @AndroidEntryPoint at the top of your activity.
@AndroidEntryPoint class MainActivity : AppCompatActivity() { @Inject lateinit var stud:Student override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Toast.makeText(this,stud.name,Toast.LENGTH_LONG).show() } }
That’s it!, we are done with simple class creation and accessing its object with Hlit.
Module Creation and Injection
Let’s say you want to inject an object from an external lib or want to create an object of the interface. How can we use Hilt in this case? For that, we have something called Hilt module. It helps to inject interfaces and classes from external libraries. As you can see we also have @InstallIn annotation to deploy the module.
@InstallIn(ApplicationComponent::class) @Module object CustomModule { @Provides fun provideSubject():Subject{ return object:Subject{ override fun subjectName()="Maths" } } }
Now, just simply inject the object using @Inject annotation. It’s damn easy.
@Inject lateinit var subject: Subject
ViewModel Demo
Firstly we will create viewmodel and annotate it with @ViewModelInject
class MainViewModel : @ViewModelInject ViewModel() { // }
Here we are done with ViewModel creation, Now we will create an object of it.
For that will use viewModels() extensions function, and if you want a shared Viewmodel then use activityViewModel(), this both the functions create the object lazily.
private val mainViewModel : MainViewModel by viewModels() //private val mainViewModel : MainViewModel by activityViewModels()
Read: How to add Google Analytics Data into your website using PHP and Laravel
Conclusion
Hlit is definitely more powerful and easy to use DI solutions for Android Developers. It resolves dependencies at compile time. So, no more runtime exceptions. Just try it out and have fun.