I'm trying to use Hilt to inject weather repository into my view model, but get the following error message:
WeatherRepository cannot be provided without an @Provides-annotated method.
public abstract static class SingletonC implements WeatherApplication_GeneratedInjector
Weather DAO:
@Dao
interface WeatherDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertWeatherItem(weatherItem: WeatherItem)
@Query("DELETE FROM WEATHERITEM WHERE cityName = :cityName")
suspend fun deleteWeatherItem(cityName: String)
@Query("SELECT * FROM WEATHERITEM WHERE cityId = :id")
fun getWeatherItem(id: Int): Flow<WeatherItem>
@Query("SELECT * FROM WEATHERITEM")
fun getAllWeatherItems(): Flow<List<WeatherItem>>
}
Weather Repository:
class WeatherRepository(private val weatherDao: WeatherDao) {
suspend fun insertWeatherItem(weatherItem: WeatherItem) = weatherDao.insertWeatherItem(weatherItem)
suspend fun deleteWeatherItem(cityName: String) = weatherDao.deleteWeatherItem(cityName)
fun getWeatherItem(id: Int): Flow<WeatherItem> = weatherDao.getWeatherItem(id)
fun getAllWeatherItems(): Flow<List<WeatherItem>> = weatherDao.getAllWeatherItems()
}
Weather Database:
@Database(entities = [WeatherItem::class] ,version = 1)
abstract class WeatherDatabase :RoomDatabase(){
abstract fun weatherDao(): WeatherDao
}
Weather Module:
@Module
@InstallIn(ActivityComponent::class)
object WeatherModule {
@Provides
@Singleton
fun provideDatabase(@ApplicationContext context: Context): WeatherDatabase {
return Room.databaseBuilder(
context,
WeatherDatabase::class.java,
"WeatherDatabase")
.fallbackToDestructiveMigration()
.build()
}
@Provides
@Singleton
fun provideWeatherDao(weatherDatabase: WeatherDatabase): WeatherDao {
return weatherDatabase.weatherDao()
}
@Provides
@Singleton
fun provideWeatherRepository(weatherDao: WeatherDao): WeatherRepository {
return WeatherRepository(weatherDao)
}
}
Weather View Model:
@HiltViewModel
class WeatherViewModel @Inject constructor(
private val weatherRepository: WeatherRepository,
) : ViewModel()
Main Activity:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModel: WeatherViewModel by viewModels()
setContent {
WindSpellTheme(darkTheme = darkTheme) {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
MainScreen(viewModel) {
}
}
}
}
}
}
Application:
@HiltAndroidApp
class WeatherApplication: Application()
Hilt version is 2.47
NOTE
When navigating to Find usages, it's shown that there is a reference to the injected object:

Please try removing
@Inject constructorfromclass WeatherRepository @Inject constructor(private val weatherDao: WeatherDao)as you already have it in theWeatherModule.For now it should work.
P.S. If you later decide to make
WeatherRepositoryan interface with its implementation, say,WeatherRepositoryImpl, you can do it as follows:and then, in some
Modulethat is an interface or abstract class: