0Pricing
Android Academy · Lesson

Setting Up Hilt

Add Hilt and the application component.

Setting Up Hilt is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Setting Up Hilt” lesson free?

Yes — the full text of “Setting Up Hilt” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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