I’m working on a Dagger Hilt setup in my Android project and encountered an error when using @Binds
methods.
The error message is:
error: @Binds methods' parameter type must be assignable to the return type public abstract kotlin.time.TimeSource bindTimeSource(@org.jetbrains.annotations.NotNull ^
Here is my AppModule
code:
package com.viswa.befocused.di
@Module
@InstallIn(SingletonComponent::class)
abstract class AppModule {
companion object {
@Provides
fun provideFocusedStatisticsDao(db: FocusedDatabase): FocusedStatisticsDao =
db.focusedStatisticsDao()
@Singleton
@Provides
fun provideDatabase(
app: Application,
callback: FocusedDatabase.Callback,
): FocusedDatabase =
Room.databaseBuilder(app, FocusedDatabase::class.java, "focused_database")
.addCallback(callback)
.build()
@ApplicationScope
@Singleton
@Provides
fun provideApplicationScope(): CoroutineScope = CoroutineScope(SupervisorJob())
@MainDispatcher
@Singleton
@Provides
fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
@Provides
fun provideTimeSourceAdapter(defaultTimeSource: DefaultTimeSource): TimeSourceAdapter {
return TimeSourceAdapter(defaultTimeSource)
}
}
@Binds
@JvmSuppressWildcards
abstract fun bindTimeSource(timeSourceAdapter: TimeSourceAdapter): TimeSource
@Binds
@JvmSuppressWildcards
abstract fun bindPreferencesManager(preferencesManager: DefaultPreferencesManager): PreferencesManager
@Binds
@JvmSuppressWildcards
abstract fun bindPomodoroTimerStateManager(focusedTimerManager: DefaultFocusedTimerStateManager): FocusedTimerStateManager
@Binds
@JvmSuppressWildcards
abstract fun bindTimerServiceManager(timerServiceManager: DefaultTimerServiceManager): TimerServiceManager
@Binds
@JvmSuppressWildcards
abstract fun bindNotificationHelper(notificationHelper: DefaultNotificationHelper): NotificationHelper
}
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class ApplicationScope
@Retention(AnnotationRetention.RUNTIME)
@Qualifier
annotation class MainDispatcher
I’m using:
- Kotlin version: 1.9.0
- Dagger Hilt version: 2.51.1
The DefaultTimeSource
class does not directly implement kotlin.time.TimeSource
,
code :
package com.viswa.befocused.feature.timer
import android.os.SystemClock
import javax.inject.Inject
class DefaultTimeSource @Inject constructor() : TimeSource {
override val elapsedRealTime: Long
get() = SystemClock.elapsedRealtime()
}
Code : Focused Database
@Database(
entities = [PomodoroStatistic::class],
version = 1,
exportSchema = true
)
abstract class FocusedDatabase : RoomDatabase() {
abstract fun focusedStatisticsDao(): FocusedStatisticsDao
class Callback @Inject constructor(
private val database: Provider<FocusedDatabase>,
@ApplicationScope private val applicationScope: CoroutineScope,
) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
}
}
}
Code: FocusedStatisticDao
@Dao
interface FocusedStatisticsDao {
@Query("SELECT * FROM focused_statistics ORDER BY timestampInMilliseconds")
fun getAllPomodoroStatistics(): Flow<List<PomodoroStatistic>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertPomodoroStatistics(pomodoroStatistic: PomodoroStatistic)
@Query("DELETE FROM focused_statistics")
suspend fun deleteAllPomodoroStatistics()
}
Detailed Error
Executing tasks: [:app:assembleDebug] in project C:UsersagentStudioProjectsBeFocused
C:UsersagentStudioProjectsBeFocusedappbuildtmpkapt3stubsdebugcomviswabefocuseddatadbFocusedDatabase.java:12: warning: Schema export directory was not provided to the annotation processor so Room cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument by applying the Room Gradle plugin (id 'androidx.room') OR set exportSchema to false.
public abstract class FocusedDatabase extends androidx.room.RoomDatabase {
^
C:UsersagentStudioProjectsBeFocusedappbuildtmpkapt3stubsdebugcomviswabefocuseddiAppModule.java:41: error: @Binds methods' parameter type must be assignable to the return type
public abstract kotlin.time.TimeSource bindTimeSource(@org.jetbrains.annotations.NotNull
^
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
Any guidance on how to resolve this type assignment issue with Dagger Hilt would be greatly appreciated.
I have tried adding @JvmSuppressWildcards
to the @Binds
methods but still face the same issue.
Any guidance on how to resolve this type assignment issue with Dagger Hilt would be greatly appreciated.