In this tutorial, you will be learning about the new Dependency Injection ( DI ) in Android development called “Hilt”, which is built upon Dagger. I know you really want to know what hilt is all about, the benefits and how to implement it in your Android project. Let’s start

NOTE: I won’t be talking about Dependency Injection basics

What is Hilt?

Hilt is an opinionated Dependency Injection library for Android that reduces the boilerplate of using manual DI in your project. Hilt makes it easy to add dependency injection to your Android app.

Hilt provides a standard way to do DI injection in your application by providing containers to every Android component in your project and managing the container’s lifecycle automatically for you.

The Benefits of Hilt

  • Reusability of code.
  • Ease of refactoring.
  • Ease of testing.
  • Reduced boilerplate
  • Decoupled build dependencies
  • Simplified configuration
  • Improved testing
  • Standardized components

Setup

Hilt dependencies

To use Hilt, add the following build dependencies to the Android Gradle module’s build.gradle file:

dependencies {
 def hilt_jetpack = "1.0.0-alpha01"

 // Room
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    // Activity Ktx for by viewModels()
    implementation "androidx.activity:activity-ktx:1.1.0"
    //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_jetpack"
    kapt "androidx.hilt:hilt-compiler:$hilt_jetpack"
    // coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4"
    // ViewModel
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0-alpha04'
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
  // For instrumentation tests
  androidTestImplementation  'com.google.dagger:hilt-android-testing:<VERSION>'
  androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:<VERSION>'
  // For local unit tests
  testImplementation 'com.google.dagger:hilt-android-testing:<VERSION>'
  testAnnotationProcessor 'com.google.dagger:hilt-android-compiler:<VERSION>'
}

Then, to use the Gradle plugin in the app module, we specify it in the app/build.gradle file by adding the plugin to the top of the file, below the kotlin-kapt plugin:

...
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

android {
    ...
}

Now let’s understand some few terms in Hilt that will help you understand it and its implementations, below are a few of the terms.

Let’s build a simple Hilt todo android application using room that will add a todo and also list them

Kindly follow the simple steps to get it up and running.

Step 1:

Create an Entity Class

@Entity(tableName = "todo")
data class Todo(
    @PrimaryKey(autoGenerate = true)
    var id: Int? = null,
    var title: String? = null,
    var description: String? = null
)

Step 2:

Create a DAO

@Dao
interface TodoDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(todo: Todo)
    @Query("SELECT * FROM todo")
    fun getAllTodos(): LiveData<List<Todo>>
}

Step 3:

Create a database

@Database(entities = [Todo::class], version = 1, exportSchema = false)
abstract class AppDb : RoomDatabase() {
    abstract fun todoDao(): TodoDao
}

#androiddev #android-app-development #android

Basics of Dagger Hilt and Implementation
11.85 GEEK