0
0
Android Kotlinmobile~20 mins

Dependency injection with Hilt in depth in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hilt Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when injecting a singleton with Hilt?
Consider a Hilt module that provides a singleton instance of a Repository class. If two different Android components inject this Repository, what will be the behavior of the injected instances?
Android Kotlin
@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {
    @Provides
    @Singleton
    fun provideRepository(): Repository = Repository()
}

class Repository {
    val id = System.identityHashCode(this)
}

@AndroidEntryPoint
class ActivityA : AppCompatActivity() {
    @Inject lateinit var repository: Repository
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println("ActivityA Repository ID: ${repository.id}")
    }
}

@AndroidEntryPoint
class ActivityB : AppCompatActivity() {
    @Inject lateinit var repository: Repository
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println("ActivityB Repository ID: ${repository.id}")
    }
}
AEach Activity receives a new Repository instance with different IDs.
BBoth ActivityA and ActivityB receive the same Repository instance with identical IDs.
CInjection fails because Repository is not annotated with @Inject constructor.
DBoth Activities receive null for Repository because it is not scoped to activities.
Attempts:
2 left
💡 Hint
Think about the @Singleton annotation and the component scope it is installed in.
📝 Syntax
intermediate
1:30remaining
Which Hilt annotation is missing to enable constructor injection?
Given the following Kotlin class, which annotation must be added to enable Hilt to inject it via constructor injection?
Android Kotlin
class NetworkClient(private val baseUrl: String) {
    fun fetchData(): String = "Data from $baseUrl"
}
A@Inject constructor(private val baseUrl: String)
B@Provides fun NetworkClient()
C@Singleton class NetworkClient(private val baseUrl: String)
D@Module class NetworkClient(private val baseUrl: String)
Attempts:
2 left
💡 Hint
Constructor injection requires a specific annotation on the constructor.
🔧 Debug
advanced
2:30remaining
Why does this Hilt injection cause a runtime error?
Examine the following code snippet. What is the cause of the runtime error when injecting UserRepository into ViewModel?
Android Kotlin
@HiltViewModel
class UserViewModel @Inject constructor(
    private val userRepository: UserRepository
) : ViewModel()

class UserRepository @Inject constructor(
    private val apiService: ApiService
)

@Module
@InstallIn(ActivityComponent::class)
object NetworkModule {
    @Provides
    fun provideApiService(): ApiService = ApiService()
}
AApiService class is missing @Inject constructor causing provider failure.
BUserRepository is missing @Singleton annotation causing multiple instances and conflict.
CApiService is provided in ActivityComponent scope but UserViewModel is in ViewModelComponent, causing a scope mismatch.
DUserViewModel lacks @Singleton annotation causing injection failure.
Attempts:
2 left
💡 Hint
Check the component scopes where dependencies are provided and injected.
🧠 Conceptual
advanced
1:40remaining
What is the role of @InstallIn in Hilt modules?
Why is the @InstallIn annotation required on a Hilt module, and what does it control?
A@InstallIn is used to declare a module as a replacement for another module.
B@InstallIn marks a module as injectable via constructor injection.
C@InstallIn automatically generates a singleton instance of all provided dependencies.
D@InstallIn specifies the Hilt component(s) where the module's bindings are available, controlling the lifecycle and scope of provided dependencies.
Attempts:
2 left
💡 Hint
Think about how Hilt knows where to use the module's bindings.
state_output
expert
3:00remaining
What is the output of this Hilt assisted injection example?
Given the following code using Hilt's AssistedInject, what will be printed when creating a UserViewModel with userId = 42?
Android Kotlin
class UserRepository @Inject constructor()

class UserViewModel @AssistedInject constructor(
    private val userRepository: UserRepository,
    @Assisted private val userId: Int
) {
    fun printUser() = println("UserViewModel with userId: $userId and repo: $userRepository")
}

@AssistedFactory
interface UserViewModelFactory {
    fun create(userId: Int): UserViewModel
}

fun main() {
    // Simulate assisted injection factory usage
    val repo = UserRepository()
    val factory = object : UserViewModelFactory {
        override fun create(userId: Int) = UserViewModel(repo, userId)
    }
    val vm = factory.create(42)
    vm.printUser()
}
AUserViewModel with userId: 42 and repo: UserRepository@<hashcode>
BUserViewModel with userId: 0 and repo: UserRepository@<hashcode>
CCompilation error due to missing @Inject on UserViewModel constructor
DRuntime error because AssistedInject is not supported without Hilt setup
Attempts:
2 left
💡 Hint
Assisted injection allows passing runtime parameters while injecting other dependencies.