Under the package com.example, create a data class called Store. To be able to use DI, we are going to need an object that depends on some other objects. This is where your main function lives, so create a main() function here. Ĭreate a new file Entry.kt under the package com.example. Later on, we will put Dagger boilerplate code in them. Under the kotlin directory, create the packages below. This will bring in necessary libraries and annotation processing for your project. In your IDE, go to Build -> Build Project. TestImplementation(":kotlin-test:1.5.21") Wait a few seconds for the project to build.Ĭopy and paste this exact same gradle configuration below into your file. Leave the next screen as default and click Finish. This is how the project setting looks like: I find this version to be most stable when working with Kotlin. You can leave the Location as default if you wish.į. Use Dagger2Practice as project name(optional).Ĭ. In IntelliJ, go to File > New > Project.ī.How to use Dagger constructor injection.įirst we need to set up our Kotlin project:.GoalsĪt the end of this tutorial, you would have learned: The main feature that we are going to look at in this tutorial is constructor dependency injection using Dagger 2. This tutorial introduces Dagger 2 in its original form using Kotlin to make it easy for Kotlin-only developers to follow. Hilt is built on top of Dagger 2, so the terminologies and concepts are hard to understand for developers who did not use Dagger before being introduced to Hilt. Hilt is a great library, but it is not perfect. The most obvious benefits of using Hilt are reduced boilerplate code and lifecycle-aware components( and their associated annotations). In Android development, the current recommended library for Dependency Injection is Hilt.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |