0
0
Android Kotlinmobile~10 mins

Dependency injection with Hilt in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A@Inject
B@HiltAndroidApp
C@AndroidEntryPoint
D@Module
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @HiltAndroidApp on the Application class.
Forgetting to annotate the Application class at all.
2fill in blank
medium

Complete 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'
A@Module
B@Provides
C@Inject
D@Singleton
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Provides instead of @Inject for field injection.
Not annotating the Activity with @AndroidEntryPoint.
3fill in blank
hard

Fix 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'
ASingleton
BProvides
CInject
DBinds
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Provides alone without @Singleton for shared instances.
Using @Inject on provider functions.
4fill in blank
hard

Fill 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'
AuserRepositoryImpl
BUserRepository
CuserRepository
DUserRepositoryImpl
Attempts:
3 left
💡 Hint
Common Mistakes
Using the interface name as the parameter name.
Swapping the parameter and return types.
5fill in blank
hard

Fill 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'
A"https://api.example.com/"
BGsonConverterFactory.create()
CScalarsConverterFactory.create()
DOkHttpClient()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong URL format without quotes.
Using the wrong converter factory.
Confusing client creation with converter factory.