How to inject SharedPreferences to Background service using Hilt

Viewed 866

I am trying to inject SharedPrefernces to Timer foreground service class. I use @EntryPoint annotation over service class.

ApplicationModule class

@Module
@InstallIn(SingletonComponent::class)
class ApplicationModule() {

@Singleton
@Provides
fun provideSharedPreferences(@ApplicationContext appContext: Context): SharedPreferences {
      return PreferenceManager.getDefaultSharedPreferences(appContext)
  }
}

Timer service class

@EntryPoint
class Timer : Service() {

    @Inject
    lateinit var preferences: SharedPreferences
    ...
    }

When I run this code, I get this error: @EntryPoint com.beu.coroutineskotlin.Timer must also be annotated with @InstallIn [Hilt] Processing did not complete. See error above for details.warning: File for type 'com.beu.coroutineskotlin.App_HiltComponents' created in the last round will not be subject to annotation processing.

1 Answers
Related