Search code examples
javaandroidkotlininterfacedagger

how to fix "cannot be provided without an @Provides-annotated method." - Kotlin


whenever i try to run the application i am facing this error:

error: [Dagger/MissingBinding] api.PrayerTimesInterface cannot be provided without an @Provides-annotated method.

and this is the interface:

    @GET("calendar")
suspend fun getPrayerTimes(
    @Query("latitude") latitude: Double,
    @Query("longitude") longitude: Double,
    @Query("method") method: Int,
    @Query("month") month: Int,
    @Query("year") year: Int,
): Response<PlayerTime>

@GET("calendarByAddress")
suspend fun getPrayerAddress(
    @Query("address") address: String,
    @Query("method") method: Int,
    @Query("month") month: Int,
    @Query("year") year: Int,
): Response<PlayerTime>
}

i tried to add "@Provides" but still facing the same error. the same code available in different version of the app and it's working fine.

i tried the solution here: How to fix "cannot be provided without an @Provides-annotated method" error in dagger2 library

but it didn't work for me

did i do anything wrong here?


Solution

  • Interface classes cannot be directly injected as they do not have constructors. I suppose you are using the PrayerTimesInterface with Retrofit. So inject PrayerTimesInterface with Retrofit's create method.

    In module class

    @Provides
    internal fun providesPrayerTimes(): PrayerTimesInterface {
            return Retrofit.Builder().build().create(PrayerTimesInterface::class.java)
        }
    

    You can also create Retrofit injection with all the options required and take it in providesPrayerTimes method to create PrayerTimesInterface