title: Injecting a repository into a Service in Android using Hilt
body: <p>I have an Android project with Hilt dependency injection. I have defined <code>MyApplication</code> and <code>MyModule</code> as follows.</p>
  <pre><code>@HiltAndroidApp
  class MyApplication : Application()
  
  @Module
  @InstallIn(ApplicationComponent::class)
  abstract class MyModule {
      @Binds
      @Singleton
      abstract fun bindMyRepository(
          myRepositoryImpl: MyRepositoryImpl
      ): MyRepository
  }
  </code></pre>
  <p><code>MyRepositoryImpl</code> implements the <code>MyRepository</code> interface:</p>
  <pre><code>interface MyRepository {
      fun doSomething(): String
  }
  
  class MyRepositoryImpl
  @Inject
  constructor(
  
  ) : MyRepository {
      override fun doSomething() = &quot;&quot;
  }
  </code></pre>
  <p>I can now inject this implementation of <code>MyRepository</code> into a ViewModel:</p>
  <pre><code>class MyActivityViewModel
  @ViewModelInject
  constructor(
      private val myRepository: MyRepository,
  ) : ViewModel() { }
  </code></pre>
  <p>This works as expected. However, if I try to inject the repository into a service, I get an error <code>java.lang.Class&lt;MyService&gt; has no zero argument constructor</code>:</p>
  <pre><code>class MyService
  @Inject
  constructor(
      private val myRepository: MyRepository,
  ): Service() { }
  </code></pre>
  <p>The same error occurs with an activity, too:</p>
  <pre><code>class MyActivity
  @Inject
  constructor(
      private val myRepository: MyRepository,
  ) : AppCompatActivity(R.layout.my_layout) { }
  </code></pre>
  <p>What am I doing wrong with the injection?</p>