Bird
Raised Fist0
Android Kotlinmobile~10 mins

Dependency injection with Hilt in depth in Android Kotlin - Interactive Code Practice

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

Complete the code to annotate an Android Application class for Hilt.

Android Kotlin
import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@[1]
class MyApp : Application() {}
Drag options to blanks, or click blank then click option'
AHiltAndroidApp
BInject
CModule
DProvides
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @HiltAndroidApp
Forgetting to annotate the Application class
Using @Module or @Provides on the Application class
2fill in blank
medium

Complete the code to inject a dependency into an Android Activity using Hilt.

Android Kotlin
import android.os.Bundle
import androidx.activity.ComponentActivity
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @Inject lateinit var repository: [1]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}
Drag options to blanks, or click blank then click option'
AModule
BInject
CMyRepository
DProvides
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject as the type instead of the class name
Not annotating the Activity with @AndroidEntryPoint
Forgetting to declare the variable as lateinit
3fill in blank
hard

Fix the error in the Hilt module by completing the annotation to provide a singleton instance.

Android Kotlin
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @[1]
    fun provideService(): ApiService {
        return ApiServiceImpl()
    }
}
Drag options to blanks, or click blank then click option'
ASingleton
BInject
CHiltAndroidApp
DAndroidEntryPoint
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @Singleton on provider methods
Forgetting to annotate the module with @InstallIn
Using @AndroidEntryPoint or @HiltAndroidApp on provider methods
4fill in blank
hard

Fill both blanks to create a Hilt module that provides a Retrofit instance.

Android Kotlin
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
    @Singleton
    @Provides
    fun provideRetrofit(): Retrofit {
        return Retrofit.Builder()
            .baseUrl([1])
            .addConverterFactory([2])
            .build()
    }
}
Drag options to blanks, or click blank then click option'
A"https://api.example.com/"
BGsonConverterFactory.create()
CRetrofit.create()
D"http://localhost/"
Attempts:
3 left
💡 Hint
Common Mistakes
Using Retrofit.create() instead of GsonConverterFactory.create()
Forgetting quotes around the base URL string
Using a wrong URL like localhost for production
5fill in blank
hard

Fill all three blanks to create a Hilt ViewModel with an injected repository and a saved state handle.

Android Kotlin
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class UserViewModel @Inject constructor(
    private val repository: [1],
    private val savedStateHandle: [2]
) : ViewModel() {

    fun getUserId(): String? {
        return savedStateHandle.get<String>([3])
    }
}
Drag options to blanks, or click blank then click option'
AUserRepository
BSavedStateHandle
C"userId"
DViewModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using ViewModel as a type for savedStateHandle
Forgetting quotes around the key string
Using wrong repository class name

Practice

(1/5)
1. What is the primary purpose of using @Module and @Provides annotations in Hilt?
easy
A. To tell Hilt how to create and supply dependencies
B. To mark an Android Activity for injection
C. To automatically generate UI components
D. To define navigation between screens

Solution

  1. Step 1: Understand the role of @Module

    @Module is used to group methods that provide dependencies to Hilt.
  2. Step 2: Understand the role of @Provides

    @Provides annotates methods inside a module to tell Hilt how to create specific dependencies.
  3. Final Answer:

    To tell Hilt how to create and supply dependencies -> Option A
  4. Quick Check:

    @Module + @Provides = supply dependencies [OK]
Hint: Remember: @Module groups, @Provides creates dependencies [OK]
Common Mistakes:
  • Confusing @Module with @AndroidEntryPoint
  • Thinking @Provides creates UI elements
  • Assuming @Module is for navigation
2. Which of the following is the correct way to inject a dependency into an Android Activity using Hilt?
easy
A. Annotate the Activity with @AndroidEntryPoint and use @Inject on the field
B. Use @Module on the Activity class
C. Call Hilt.inject(this) inside onCreate()
D. Add @Provides on the Activity constructor

Solution

  1. Step 1: Identify how to enable injection in Activity

    Hilt requires the Activity to be annotated with @AndroidEntryPoint to generate necessary code.
  2. Step 2: Inject dependencies with @Inject

    Fields or constructor parameters annotated with @Inject receive dependencies automatically.
  3. Final Answer:

    Annotate the Activity with @AndroidEntryPoint and use @Inject on the field -> Option A
  4. Quick Check:

    @AndroidEntryPoint + @Inject field = inject Activity dependency [OK]
Hint: Use @AndroidEntryPoint on Activity, then @Inject fields [OK]
Common Mistakes:
  • Trying to use @Module on Activity
  • Calling non-existent Hilt.inject() method
  • Adding @Provides on constructors
3. Given the following code snippet, what will be the output when MyViewModel is injected?
class MyRepository @Inject constructor() {
    fun getData() = "Data from repository"
}

@HiltViewModel
class MyViewModel @Inject constructor(private val repo: MyRepository) : ViewModel() {
    fun fetch() = repo.getData()
}
medium
A. "fetch() method not found"
B. NullPointerException at runtime
C. Compilation error due to missing @Provides
D. "Data from repository"

Solution

  1. Step 1: Understand constructor injection in MyRepository

    MyRepository has an @Inject constructor, so Hilt can create it automatically.
  2. Step 2: MyViewModel receives MyRepository via constructor injection

    Hilt injects MyRepository instance into MyViewModel, so calling fetch() returns repo.getData() string.
  3. Final Answer:

    "Data from repository" -> Option D
  4. Quick Check:

    Constructor injection returns data string [OK]
Hint: If constructor has @Inject, Hilt creates instance automatically [OK]
Common Mistakes:
  • Assuming @Provides is always needed
  • Expecting runtime null errors with constructor injection
  • Confusing method names in ViewModel
4. Identify the error in this Hilt module code:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
    @Provides
    fun provideHttpClient(): OkHttpClient {
        return OkHttpClient()
    }

    @Provides
    fun provideRetrofit(client: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .client(client)
            .baseUrl("https://api.example.com")
            .build()
    }
}
medium
A. Missing @Singleton annotation on provider methods
B. Retrofit.Builder() requires a converter factory
C. OkHttpClient cannot be provided without parameters
D. NetworkModule should be a class, not an object

Solution

  1. Step 1: Review Retrofit.Builder usage

    Retrofit requires a converter factory (like GsonConverterFactory) to parse responses; missing here causes runtime error.
  2. Step 2: Check other options for correctness

    @Singleton is optional but recommended; object is valid for modules; OkHttpClient can be created without parameters.
  3. Final Answer:

    Retrofit.Builder() requires a converter factory -> Option B
  4. Quick Check:

    Retrofit needs converter factory to build properly [OK]
Hint: Retrofit always needs a converter factory to avoid errors [OK]
Common Mistakes:
  • Thinking @Singleton is mandatory for all providers
  • Confusing object vs class for modules
  • Assuming OkHttpClient needs parameters
5. You want to inject two different implementations of an interface Logger using Hilt. How can you distinguish them during injection?
hard
A. Use @Singleton on one implementation and omit on the other
B. Create two separate modules with the same provider method name
C. Use @Named qualifiers with different names on provider methods and injection points
D. Inject both implementations into a list automatically

Solution

  1. Step 1: Understand multiple implementations injection

    When multiple bindings exist for the same type, Hilt requires qualifiers to distinguish them.
  2. Step 2: Use @Named qualifiers

    Annotate provider methods and injection points with @Named("name") to specify which implementation to inject.
  3. Final Answer:

    Use @Named qualifiers with different names on provider methods and injection points -> Option C
  4. Quick Check:

    Multiple implementations need @Named qualifiers [OK]
Hint: Use @Named("tag") to differentiate implementations [OK]
Common Mistakes:
  • Assuming module separation alone distinguishes implementations
  • Using @Singleton to differentiate implementations
  • Expecting automatic list injection without setup
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard