Use Hilt for constructor injection and module-provided dependencies.
// App.kt
@HiltAndroidApp
class App: Application()
// NetworkModule.kt
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Provides @Singleton fun okHttp(): OkHttpClient = OkHttpClient.Builder().build()
@Provides @Singleton fun retrofit(client: OkHttpClient): Retrofit =
Retrofit.Builder().baseUrl("https://example.com").client(client).addConverterFactory(MoshiConverterFactory.create()).build()
@Provides @Singleton fun api(retrofit: Retrofit): Api = retrofit.create(Api::class.java)
}
// RepoModule.kt
@Module
@InstallIn(SingletonComponent::class)
object RepoModule {
@Provides fun todosRepo(api: Api, dao: TodoDao): TodosRepo = TodosRepoImpl(api, dao)
}
Paste into Android Studio project (see sandboxes/android-compose):
@HiltAndroidApp
.@HiltViewModel
and obtain instances with hiltViewModel()
in composables.