Agent skill
kotlin-di
Dependency Injection - Hilt, Koin, scopes, testing
Install this agent skill to your Project
npx add-skill https://github.com/pluginagentmarketplace/custom-plugin-kotlin/tree/main/skills/kotlin-di
SKILL.md
Kotlin DI Skill
Dependency Injection with Hilt and Koin.
Topics Covered
Hilt for Android
@HiltAndroidApp
class App : Application()
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides @Singleton
fun provideDatabase(@ApplicationContext context: Context) =
Room.databaseBuilder(context, AppDatabase::class.java, "app.db").build()
@Provides
fun provideUserDao(db: AppDatabase) = db.userDao()
}
@HiltViewModel
class UserViewModel @Inject constructor(
private val repository: UserRepository
) : ViewModel()
Koin for Multiplatform
val appModule = module {
single { HttpClient(getEngine()) }
single { UserRepository(get()) }
viewModel { UserViewModel(get()) }
}
// Start Koin
startKoin {
modules(appModule)
}
// Inject
val repository: UserRepository by inject()
Testing with DI
@HiltAndroidTest
class UserViewModelTest {
@get:Rule val hiltRule = HiltAndroidRule(this)
@BindValue @JvmField
val repository: UserRepository = mockk()
@Inject lateinit var viewModel: UserViewModel
@Before fun setup() { hiltRule.inject() }
}
Troubleshooting
| Issue | Resolution |
|---|---|
| "No binding for..." | Add @Provides or @Binds |
| ViewModel not injected | Use hiltViewModel() in Compose |
Usage
Skill("kotlin-di")
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
kotlin-serialization
kotlinx.serialization - JSON, Protobuf, custom serializers
kotlin-testing
Kotlin testing - JUnit 5, MockK, Kotest, coroutine testing
kotlin-spring
Spring Boot with Kotlin - controllers, services, coroutines
kotlin-fundamentals
Kotlin language fundamentals - syntax, null safety, data classes, extensions
kotlin-android
Modern Android development - Jetpack, Compose, Architecture Components
kotlin-multiplatform
Kotlin Multiplatform - shared code, expect/actual, iOS integration
Didn't find tool you were looking for?