Create an application CoroutineScope using Hilt

In this article, you’ll learn how to create an application-scoped CoroutineScope using Hilt, and how to inject it as a dependency. To further improve the way we work with Coroutines, we’ll see how to inject the different CoroutineDispatchers and replace their implementations in tests.

Manual dependency injection

To create an application-scoped CoroutineScope following dependency injection (DI) best practices manually without any library, you’d typically add a new variable to your application class with an instance of a CoroutineScope. The same instance would be manually passed around when creating other objects.

Since there isn’t a reliable way to know when the Application is destroyed in Android, you don’t need to call applicationScope.cancel() manually as the scope and all ongoing work will be destroyed when the application process finishes.

Website