Dagger Hilt (For Beginners)

I am a mobile application developer with more than 3 years of experience! I always develop mobile applications alone – my job requires it. But today I want to share with you what Dagger Hilt is!

Follow me on telegram

Dagger Hilt is a tool everyone needs that greatly simplifies the development of mobile applications. Used for dependency injection. First of all, it is necessary for those who have many classes in their project that require implementation. Do you want someone else to do this for you? Dagger Hilt is at your service.

Hilt is a dependency injection library for Android that makes it easy to manually inject dependencies in a boilerplate manner into your project. Performing manual dependency injection requires you to create each class and its dependencies manually, and use containers to reuse and manage dependencies

⭐ Benefits

  • Quick start

  • Simplified dependency management

  • Integration with Android components (Activity, Fragment, ViewModel)

⭐ Add to project

Version Catalog:

[versions]
# other versions
hilt-version = "2.51.1"


[libraries]
# other libraries
hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt-version" }
hilt-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt-version" }
hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version = "1.2.0" }


[plugins]
# other plugins
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt-version" }

build.gradle.kts (Module:app)

plugins {
    // other plugins
    id("kotlin-kapt")
    alias(libs.plugins.hilt)
}

kapt {
    correctErrorTypes = true
}

dependencies {
  // other dependencies
  implementation(libs.hilt.android)
  kapt(libs.hilt.compiler)
  implementation(libs.hilt.navigation.compose)

}

build.gradle.kts(Project: YourProjectName)

plugins {
    // other plugins
    alias(libs.plugins.hilt) apply false
}

⭐ How to use it

You must define an App class. This is the base class without which the project will not be able to improve Hilt.

All applications that use Hilt must contain an Application class marked @HiltAndroidApp.

@HiltAndroidApp starts Hilt code generation, including a base class for your application that serves as an application-level dependency container

Initialize this class in the manifest!

@HiltAndroidApp
class App: Application() {
}

⭐ Dependency injection into classes

Once Hilt is configured in your Application class and the application level component is available, Hilt can provide dependencies to other Android classes that have the @AndroidEntryPoint annotation

@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() { ... }

These are the components that Hilt supports. All those components that are not marked “with” use the @AndroidEntryPoint annotation

Each component must be annotated. This lets Hilt know which component to start injecting dependencies from. These are the starting points for Hilt

⭐ Real examples (cases)

Аннотация @Inject
class TestClassInject @Inject constructor() {
    fun invoke1() = "Hello world"
    val invoke2 = "Hello world 2"
}

In MainActivity.kt

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
  @Inject lateinit var testClassInject: TestClassInject

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    testClassInject.invoke1()

  }
}

IMPORTANT: a variable that is marked @Inject cannot be private!

This ends my post about Dagger Hilt for beginners. The next post will be about more complex components and their applications. You will learn how to beautifully design your repository when using Dager Hilt and how you can use Hilt in external classes. Interesting? Read on and you will become a real guru!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *