androidkotlinandroid-studiodependency-injectionkoin

How to inject LocationTracker using Koin DI framework


I'm migrating from Hilt to Koin.

With Hilt I'm doing the following for injecting LocationTracker:

@Module
@InstallIn(SingletonComponent::class)
object LocationModule {
    @Provides
    @Singleton
    fun providesFusedLocationProviderClient(application: Application): FusedLocationProviderClient =
        LocationServices.getFusedLocationProviderClient(application)

    @Provides
    @Singleton
    fun providesLocationTracker(
        fusedLocationProviderClient: FusedLocationProviderClient,
        application: Application
    ): LocationTracker = LocationTracker(
        fusedLocationProviderClient = fusedLocationProviderClient,
        application = application
    )
}

But with koin I don't know how to inject Application in order to be able to inject FusedLocationProviderClient.

So, how can I inject Application to end up injecting LocationTracker???


Solution

  • In Koin, you can get the application instance using androidApplication().

    https://insert-koin.io/docs/reference/koin-android/get-instances/#android-scope--android-context-resolution