Skip to content

Android Hilt @AndroidEntryPoint — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Android Hilt @AndroidEntryPoint. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You inject a dependency into your Activity but it's null, or you forgot @AndroidEntryPoint and your app crashes with a cryptic Hilt error.

Wrong Approach ❌

// No @AndroidEntryPoint — Hilt ignores this Activity
class MainActivity : AppCompatActivity() {
    @Inject lateinit var repository: UserRepository

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        repository.getUsers() // NPE! repository is null
    }
}
// Using Hilt in a non-Hilt base class
class BaseActivity : FragmentActivity() { /* No @AndroidEntryPoint */ }

@AndroidEntryPoint
class ChildActivity : BaseActivity() { // WON'T WORK!
    @Inject lateinit var api: ApiService
}

Output: NullPointerException on injected fields. Hilt doesn't set up the component.

Right Approach ✅

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    @Inject lateinit var repository: UserRepository

    override fun onCreate(savedInstanceState: Bundle?) {
        // Hilt sets up before super.onCreate
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // repository is now injected
        viewModel = ViewModelProvider(this).get(UserViewModel::class.java)
    }
}

// For base classes — propagate @AndroidEntryPoint
@AndroidEntryPoint
open class BaseActivity : AppCompatActivity() {
    @Inject lateinit var analytics: AnalyticsService
}

@AndroidEntryPoint
class HomeActivity : BaseActivity() { // Works!
    @Inject lateinit var userRepo: UserRepository
}
// For non-@AndroidEntryPoint parents, use a Hilt wrapper
@AndroidEntryPoint
class HiltWrapperActivity : AppCompatActivity() {
    @Inject lateinit var dep: MyDependency

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // delegate to non-Hilt parent logic
    }
}

Output: All injected fields populated before onCreate.

Prevention

  • Annotate every Activity that uses @Inject with @AndroidEntryPoint.
  • Always call super.onCreate() before accessing injected dependencies.
  • If using a base Activity, annotate it with @AndroidEntryPoint.
  • For Fragments, use @AndroidEntryPoint on every Fragment.

Common Mistakes with hilt activity

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What Android components support @AndroidEntryPoint?

Activity, Fragment, View, Service, BroadcastReceiver, and ContentProvider. Use @HiltAndroidApp for the Application class.

### Can I use @AndroidEntryPoint on an abstract class?

No. Only concrete classes that are instantiated by the Android framework. Abstract classes don't have a Hilt component.

### What happens if I forget @AndroidEntryPoint?

Hilt cannot set up the component, so @Inject fields remain null and accessing them causes a NullPointerException. The error message points to the missing annotation.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro