android5-10 Exercise 1: Container to Hilt ===================================== // Root build.gradle.kts (versions are placeholders; take current ones from the Hilt docs) plugins { id("com.google.dagger.hilt.android") version "" apply false id("com.google.devtools.ksp") version "" apply false } // Module build.gradle.kts plugins { id("com.google.devtools.ksp") id("com.google.dagger.hilt.android") } dependencies { implementation("com.google.dagger:hilt-android:") ksp("com.google.dagger:hilt-compiler:") // the docs also show the name hilt-android-compiler implementation("androidx.hilt:hilt-lifecycle-viewmodel-compose:") // for hiltViewModel() } import android.app.Application import dagger.hilt.android.HiltAndroidApp @HiltAndroidApp class NotesApp : Application() // the container, onCreate and lateinit are gone import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class MainActivity : ComponentActivity() { // onCreate and setContent as before } import androidx.lifecycle.ViewModel import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject @HiltViewModel class NotesViewModel @Inject constructor( private val repository: NotesRepository, private val settings: SettingsRepository ) : ViewModel() { // the body is unchanged; the companion object Factory is gone } import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel // check the exact package in the IDE @Composable fun NotesRoute(viewModel: NotesViewModel = hiltViewModel()) { // ...as before } import javax.inject.Inject import javax.inject.Singleton @Singleton class DefaultNotesRepository @Inject constructor( private val noteDao: NoteDao, private val api: NotesApi ) : NotesRepository { // unchanged } @Singleton class SettingsRepository @Inject constructor( private val dataStore: DataStore ) { // unchanged } import android.content.Context import dagger.Binds import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.components.SingletonComponent import javax.inject.Singleton // @Provides: for things you cannot (or do not want to) construct with @Inject @Module @InstallIn(SingletonComponent::class) object DataModule { @Provides @Singleton fun provideDatabase(@ApplicationContext context: Context): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "notes.db").build() @Provides fun provideNoteDao(database: AppDatabase): NoteDao = database.noteDao() @Provides @Singleton fun provideDataStore(@ApplicationContext context: Context): DataStore = context.settingsDataStore @Provides @Singleton fun provideNotesApi(): NotesApi = Retrofit.Builder() .baseUrl("https://jsonplaceholder.typicode.com/") .addConverterFactory(json.asConverterFactory("application/json".toMediaType())) .build() .create(NotesApi::class.java) } // @Binds: "when someone asks for the interface, give them this implementation" @Module @InstallIn(SingletonComponent::class) abstract class RepositoryModule { @Binds abstract fun bindNotesRepository(impl: DefaultNotesRepository): NotesRepository } Delete: AppContainer, DefaultAppContainer, AppViewModelProvider, and the NotesApp body. Keep android:name=".NotesApp" in the manifest. Expected: the app behaves as before. If the build fails, read the first error: a missing binding names the type Hilt could not build. Report your Hilt, KSP and compiler artifact versions and which compiler artifact name worked (hilt-compiler or hilt-android-compiler). Code unrun.