Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hilt injection in Android Services

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

like image 368
Amir Raza Avatar asked Feb 03 '21 22:02

Amir Raza


People also ask

What is the use of Hilt in Android?

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.

What is Hilt dependency injection?

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.

How do you inject a Hilt class?

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.

What is the difference between Dagger and Hilt?

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.

What is hilt dependency injection in Android?

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.

What is a hilt in Android?

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.

What is Hilt’s @Inject annotation?

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.

How do you use dependency injection in Android?

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.


Video Answer


1 Answers

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() {
  // ...
}
like image 61
Miko Chu Avatar answered Oct 28 '22 09:10

Miko Chu