Create an application CoroutineScope using Hilt

<p>In this article, you&rsquo;ll learn how to create an application-scoped&nbsp;<code>CoroutineScope</code>&nbsp;using Hilt, and how to inject it as a dependency. To further improve the way we work with Coroutines, we&rsquo;ll see how to inject the different&nbsp;<code>CoroutineDispatcher</code>s and replace their implementations in tests.</p> <h1>Manual dependency injection</h1> <p>To create an&nbsp;<a href="https://medium.com/androiddevelopers/scoping-in-android-and-hilt-c2e5222317c0" rel="noopener">application-scoped</a>&nbsp;<code>CoroutineScope</code>&nbsp;following dependency injection (DI) best practices&nbsp;<a href="https://developer.android.com/training/dependency-injection/manual" rel="noopener ugc nofollow" target="_blank">manually</a>&nbsp;without any library, you&rsquo;d typically add a new variable to your application class with an instance of a&nbsp;<code>CoroutineScope</code>. The same instance would be manually passed around when creating other objects.</p> <p><iframe frameborder="0" height="260" scrolling="no" src="https://medium.com/media/9dc4e40b6cf16570d8efcd62976a7a82" title="MyApplication.kt" width="680"></iframe></p> <p>Since there isn&rsquo;t a reliable way to know when the&nbsp;<code>Application</code>&nbsp;is destroyed in Android, you don&rsquo;t need to call&nbsp;<code>applicationScope.cancel()</code>&nbsp;manually as the scope and all ongoing work will be destroyed when the application process finishes.</p> <p><a href="https://medium.com/androiddevelopers/create-an-application-coroutinescope-using-hilt-dd444e721528">Website</a></p>