Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to annotate the Application class for Hilt.
Android Kotlin
@[1] class MyApp : Application() { override fun onCreate() { super.onCreate() } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @HiltAndroidApp on the Application class.
Forgetting to annotate the Application class at all.
✗ Incorrect
The @HiltAndroidApp annotation tells Hilt to generate components for dependency injection in the Application class.
2fill in blank
mediumComplete the code to inject a dependency into an Android Activity using Hilt.
Android Kotlin
@AndroidEntryPoint class MainActivity : AppCompatActivity() { @[1] lateinit var repository: UserRepository override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Provides instead of @Inject for field injection.
Not annotating the Activity with @AndroidEntryPoint.
✗ Incorrect
The @Inject annotation tells Hilt to provide the dependency instance into the field.
3fill in blank
hardFix the error in the Hilt module by completing the annotation to provide a singleton instance.
Android Kotlin
@Module @InstallIn(SingletonComponent::class) object AppModule { @Provides @[1] fun provideDatabase(@ApplicationContext context: Context): AppDatabase { return Room.databaseBuilder(context, AppDatabase::class.java, "app_db").build() } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Provides alone without @Singleton for shared instances.
Using @Inject on provider functions.
✗ Incorrect
The @Singleton annotation ensures that Hilt provides a single instance of the database throughout the app.
4fill in blank
hardFill both blanks to create a Hilt module that binds an interface to its implementation.
Android Kotlin
@Module @InstallIn(SingletonComponent::class) abstract class RepositoryModule { @Binds abstract fun bindUserRepository( [1]: UserRepositoryImpl ): [2] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the interface name as the parameter name.
Swapping the parameter and return types.
✗ Incorrect
The function parameter name is usually camelCase of the implementation class, and the return type is the interface to bind.
5fill in blank
hardFill all three blanks to create a Hilt module that provides a Retrofit instance with a base URL.
Android Kotlin
@Module @InstallIn(SingletonComponent::class) object NetworkModule { @Provides @Singleton fun provideRetrofit(): Retrofit { return Retrofit.Builder() .baseUrl([1]) .addConverterFactory([2]) .client([3]) .build() } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong URL format without quotes.
Using the wrong converter factory.
Confusing client creation with converter factory.
✗ Incorrect
The baseUrl is a string URL, the converter factory is GsonConverterFactory, and OkHttpClient is often used but here the blank is for the client setup (though not shown in code, the third blank is for client).