I want to inject a class in Service
. Lets have a look at the code below:
class DeviceUtil @Inject constructor() {
...
}
@AndroidEntryPoint
class LocationUpdateService : Service() {
@Inject
lateinit var deviceUtil: DeviceUtil
...
}
@Inject lateinit var deviceUtil: DeviceUtil
is working fine in Activity but not working in Service
.
Its giving the error: kotlin.UninitializedPropertyAccessException: lateinit property deviceUtil has not been initialized
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Doing manual dependency injection requires you to construct every class and its dependencies by hand, and to use containers to reuse and manage dependencies.
Hilt provides a standard way to incorporate Dagger dependency injection into an Android application. The goals of Hilt are: To simplify Dagger-related infrastructure for Android apps. To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.
To inject dependencies in any class we have to add it to the Hilt tree. For fragments or activities we do it by adding the annotation @AndroidEntryPoint. Similarly we add @HiltViewModel to add a view model to the Hilt tree. Hilt doesn't provide such annotations for all Android classes.
In Dagger, we create scope annotations such as ActivityScope, FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.
Dependency Injection is whereby dependencies are provided to a class instead of the class having to create them itself. Hilt is a standardized way of enforcing dependency injection in an Android application. This tutorial aims to: Define dependency injection. Explain why dependency injection is important.
Hilt is a standardized way of enforcing dependency injection in an Android application. This tutorial aims to: Define dependency injection. Explain why dependency injection is important. Show in detail how to use Hilt for dependency injection. A basic understanding of Android app development with Kotlin. Android Studio 4.0 or higher.
Manual dependency injection is alright. However, it may get cumbersome as the application scales. Hilt then came in with its @Inject annotation that creates injectable fields, methods, and constructors. @Inject also helps Hilt know how to provide a certain class by giving it access to the constructor.
Doing manual dependency injection requires you to construct every class and its dependencies by hand, and to use containers to reuse and manage dependencies. Hilt provides a standard way to use DI in your application by providing containers for every Android class in your project and managing their lifecycles automatically.
For those dummies like me. As said by OP in the comments, a full example on how you can inject object in your service
like so:
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.Log
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import com.yourapp.services.UserService
@AndroidEntryPoint
class MyService: Service() {
@Inject lateinit var userService: UserService
override fun onCreate() {
super.onCreate()
userService.getUserList()
.subscribe { userList -> Log.d("tag", "users: $userList") }
}
override fun onBind(intent: Intent?): IBinder? {
return object: Binder() {
// ...
}
}
}
As for the service you're injecting make sure it has the @Inject annotation in its constructor like so:
class UserService @Inject() constructor() {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With