Android Hilt @AndroidEntryPoint — Complete Guide
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
@Injectwith@AndroidEntryPoint. - Always call
super.onCreate()before accessing injected dependencies. - If using a base Activity, annotate it with
@AndroidEntryPoint. - For Fragments, use
@AndroidEntryPointon every Fragment.
Common Mistakes with hilt activity
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro