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?
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 {
.........
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.