DEV Community

Cover image for Dependency injections in a Android Service
Tristan Elliott
Tristan Elliott

Posted on

Dependency injections in a Android Service

Table of contents

  1. Injecting into a service

My app on the Google Playstore

GitHub code

Dependency injecting into a Android Service

  • So I ran into a problem where I wanted to do some dependency injection inside a Android Service. I initialy thought we could do constructor injection just like any other class. However, that is not is the case. Thanks to THIS stackoverflow question and looking at the services of several open source Android projects, I have learned that we must do dependency injection into a Service like so:
@AndroidEntryPoint
class BillingService: Service() {

  @Inject lateinit var billingClientWrapper: BillingClientWrapper

}

Enter fullscreen mode Exit fullscreen mode
  • Note how the Service is annotated with @AndroidEntryPoint and we add @Inject lateinit to whatever we want to inject.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)