Android Academy · Lesson

Setting Up Hilt

Add Hilt and the application component.

Lesson 2 of 413 steps

Setting Up Hilt is a free Android Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

Adding Hilt to a Project

Hilt needs a Gradle plugin and dependencies, plus an annotation processor (KSP or kapt). Setup is one-time per project.

Gradle Plugin

Apply the Hilt plugin in your module build file alongside KSP.

// module build.gradle.kts
plugins {
    id("com.google.dagger.hilt.android")
    id("com.google.devtools.ksp")
}

Dependencies

Add the Hilt runtime and compiler.

dependencies {
    implementation("com.google.dagger:hilt-android:2.x")
    ksp("com.google.dagger:hilt-compiler:2.x")
}

Annotate the Application

Hilt needs an entry point at the app level. Create an Application subclass annotated with @HiltAndroidApp.

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MyApp : Application()

Register It in the Manifest

Point your manifest’s application tag at this class.

<application
    android:name=".MyApp"
    ... >

What @HiltAndroidApp Does

It triggers Hilt’s code generation and creates the application-level dependency container, the root of the whole dependency graph.

Enabling Injection in Activities

Annotate any Activity that needs injection with @AndroidEntryPoint.

import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    // Hilt can now inject members here
}

Entry Points for Other Classes

@AndroidEntryPoint also works on Fragments, Services, and BroadcastReceivers. It connects each to the Hilt graph at the right scope.

Field Injection

Inside an entry point you can request dependencies with @Inject on a field.

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject lateinit var analytics: Analytics
}

Component Hierarchy

Hilt generates a hierarchy: SingletonComponent (app) → ActivityRetainedComponent → ViewModelComponent → ActivityComponent. Each has a matching scope annotation.

Setup Checklist

  • Apply the Hilt + KSP plugins.
  • Add runtime + compiler deps.
  • Create an @HiltAndroidApp Application.
  • Mark consumers with @AndroidEntryPoint.

Quick Check

Test your understanding of Hilt setup.

Recap

You learned:

  • Apply the Hilt + KSP plugins and add dependencies.
  • @HiltAndroidApp on the Application creates the root container.
  • @AndroidEntryPoint enables injection in Activities/Fragments.
  • Use @Inject for field injection.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Setting Up Hilt” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Setting Up Hilt”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

What will I learn in “Setting Up Hilt”?

Add Hilt and the application component. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Setting Up Hilt” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Dependency Injection?
  2. Setting Up Hilt
  3. Modules and Providers
  4. Injecting into ViewModels
← Back to Android Academy