DEV Community

Dev Arena
Dev Arena

Posted on • Edited on

4 1

Hilt Introduction - Starting DI Hiltastic way

Introduction

Hilt is an DI library for Android introduced by Google. Hilt is built on top of powerful Dagger library which leverages the benefits of Dagger like compile-time correctness, runtime performance, scalability and Android Support. Hilt has good integration with Jetpack Libraries like Compose, Navigation and WorkManager etc.

  • Hilt is Stable and Production ready.
  • Designed particularly for Android.
  • Supports from Android Studio 4.0.
  • Officially recommended DI library for Android.
  • Incorporates best practices of DI.
  • Hilt is just an API around Dagger but not new library.

Advantages

  1. Simplify the Dagger implementation.
  2. Reduces the boilerplate code.
  3. Decouples the build dependencies.
  4. Robust configurations for testing.
  5. Refactoring and Testing is Easy.

Annotations

Hilt and Dagger can work together. Most of the Dagger annotations are supported by Hilt. Hilt support for following Android specific classes.

  • Application
  • ViewModel
  • Activity
  • Fragment
  • View
  • Service
  • BroadcastReceiver

Alt Text

Components & Scopes

Hilt supports set of predefined Android lifecycle aware Components and Scopes. Lifetime of the components are usually bound during the creation and destruction of the corresponding instance. Below table shows the components and their scopes.
Alt Text

Setting Up

Adding Dependencies

Add the gradle plugin into the projects root level build.gradle.

buildscript {
    ...
    ext.hilt_version = '2.35'
    dependencies {
        ...
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}
Enter fullscreen mode Exit fullscreen mode

Add the dependencies into app level app/build.gradle.

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

android {
    ...
}

dependencies {
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
}

Enter fullscreen mode Exit fullscreen mode

Hilt uses Java 8 features. Hence, add the Java 8 support into app level app/build.gradle.

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
Enter fullscreen mode Exit fullscreen mode

Keep Watching :) Let's dive into the implementation part in next article.

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay