-1

I have

BeatPlayer.kt

interface BeatPlayer {
   fun getSession(): MediaSessionCompat
   fun playSong(extras: Bundle = bundleOf(BY_UI_KEY to true))
   fun playSong(id: Long)
   fun playSong(song: Song)
 }

class BeatPlayerImplementation(
private val context: Application,
private val musicPlayer: AudioPlayer,
private val songsRepository: SongsRepository,
private val queueUtils: QueueUtils,
private val audioFocusHelper: AudioFocusHelper
) : BeatPlayer {
 .........

 }

MusicService.kt

@AndroidEntryPoint
class MusicService :  CoroutineService(Main) {
   @Inject
  lateinit var beatPlayer: BeatPlayer
}

When I run it says:

[Dagger/MissingBinding] BeatPlayer cannot be provided without an @Provides-annotated method.

So I added this:

@Module
@InstallIn(SingletonComponent::class)
abstract class StorageModule {
@Singleton
@Binds
abstract fun bindBeatPlayer(beatPlayer: BeatPlayer): BeatPlayerImplementation
}

Now, I run, it says:

error: @Binds methods' parameter type must be assignable to the return type hilt

How to do it properly?

Ibccx
  • 105
  • 3
  • 11

1 Answers1

0

I will answer my question based on comment from @HenryTest.

StorageModule.kt

@Module
@InstallIn(SingletonComponent::class)
abstract class StorageModule {
@Binds
abstract fun bindsPreferenceStorage(preferenceStorageImpl: 
PreferenceStorageImpl): PreferenceStorage

@Singleton
@Binds
abstract fun bindBeatPlayer(beatPlayer: BeatPlayerImplementation): 
BeatPlayer
}

Now Provide BeatPlayerImplementation.

AppModule.kt

 @Singleton
 @Provides
 fun providesBeatPlayerImplementation(@ApplicationContext context: 
 Context.,.,.,.) = 
 BeatPlayerImplementation(
    context, .., .., ..,  
 )

OR

In BeatPlayerImplementation

 class BeatPlayerImplementation @Inject constructor(
 @ApplicationContext private val context: Application,
 .....
 ) : BeatPlayer {
 .........
 }
Ibccx
  • 105
  • 3
  • 11
  • `fun providesBeatPlayerImplementation` you can just use `@Inject` on this class constructor, and Hilt will know how to provide `BeatPlayerImplementation` – Pentiux Jul 24 '21 at 09:50