kosi-libs / Kodein

Painless Kotlin Dependency Injection
https://kosi-libs.org/kodein
MIT License
3.22k stars 176 forks source link

No binding found when i try to retrive Android SystemServices #455

Closed SeleznevAM closed 6 months ago

SeleznevAM commented 6 months ago

I migrated from kodein version 5 to verson 7 And after migrating i started to get DI$NotFoundException when i try to get SharedPreferences or for example ConnectivityManager though di container containes binding for this type. I use DIGlobalAware approach -add my module to DI.global with(DI.global) { addImport(Services.layer, true) }

-import androidXModule to mu own module

        import(androidXModule(shared), true)
        bind<AppSettingsService>() with singleton { AppSettingsServiceImpl(instance(App), instance(arg = "name")) }

-class signature where i using SharedPreferences

class AppSettingsServiceImpl(private val context: Context, private val preferences: SharedPreferences) : AppSettingsService {...}

module Auth { bind { singleton { AuthServiceImpl } } } module Services { bind { singleton { MixPanelInstanceProvider } } bind { singleton { ChatSessionController } } bind<UploadRepository> { singleton { ContactUploadRepository } } bind { singleton { LocationServiceImpl } } bind { singleton { AppSettingsServiceImpl } } bind { singleton { AirportsRepository } } bind(tag = "firstPoint") { singleton { FirstPointRecentSearchDataSource } } bind { singleton { PersonDocumentsStorageImpl } } bind { singleton { PicassoProvider } } bind { singleton { WhatsAppMessagingService } } bind { singleton { ShareLinkGenerator } } bind { singleton { PermissionsDialogController } } bind(tag = "emptyLegsFirstPoint") { singleton { EmptyLegsFirstPointRecentSearch bind(tag = "otherPoint") { singleton { OtherPointsRecentSearchDataSource } } bind { singleton { PackageInfo } } bind(tag = "Companion") { singleton { MainScreenBannerController } } bind { singleton { NetworkReachabilityServiceImpl } } bind { singleton { UserServiceImpl } } bind { singleton { MemoryLogger } } bind { singleton { ApiService } } bind { singleton { PinCodeServiceImpl } } bind { singleton { NearByLocationDataSource } } bind { singleton { PermissionController } } bind { singleton { MixpanelAnalyticsService } } bind(tag = "ru.syrupmg.flighttaxi.App$Companion@52af12") { provider { Context } } bind { singleton { PushTokenSenderWithoutRepetitions } } bind { singleton { MiraiEnvoironmentRepository } } bind { singleton { DocumentImageLoaderProvider } } bind { singleton { PaymentSdkLauncherImpl } } bind { singleton { AccountNameEmailRequestControllerImpl } } bind { singleton { NotificationServiceImpl } } bind { singleton { EmptyLegAdvertDataSource } } bind { singleton { DealsRepository } } bind { singleton { RemoteConfig } } bind(tag = "emptyLegsOtherPoint") { singleton { EmptyLegsOtherPointRecentSearch bind<DataProvider> { singleton { ContactsProvider } } bind { singleton { ProfileAvatarLoaderProvider } } bind { singleton { RouteDelegateImpl } } bind { singleton { SwiftTemplateRepository } } bind { singleton { CryptoServiceImpl } } } module ⁣androidModule { bind { contexted().provider { UiModeManager } } bind { contexted().provider { WifiP2pManager } } bind { contexted().provider { TelecomManager } } bind { contexted().provider { StorageManager } } bind { contexted().provider { Resources.Theme } } bind { contexted().provider { MediaProjectionManager } } bind { contexted().provider { NotificationManager } } bind { contexted().provider { AudioManager } } bind { contexted().provider { SystemHealthManager } } bind { contexted().provider { AlarmManager } } bind { contexted().provider { PackageManager } } bind { contexted().provider { Vibrator } } bind { contexted().provider { RestrictionsManager } } bind { contexted().provider { SearchManager } } bind { contexted().provider { WallpaperManager } } bind { contexted().provider { AssetManager } } bind { contexted().provider { DownloadManager } } bind { contexted().provider { BatteryManager } } bind { contexted().provider { MidiManager } } bind { contexted().provider { JobScheduler } } bind { contexted().provider { UsbManager } } bind { contexted().provider { HardwarePropertiesManager } } bind { contexted().provider { MediaSessionManager } } bind(tag = "cache") { contexted().provider { File } } bind { contexted().provider { NetworkStatsManager } } bind { contexted().provider { CarrierConfigManager } } bind { contexted().provider { DropBoxManager } } bind { contexted().provider { KeyguardManager } } bind { contexted().provider { Looper } } bind { contexted().provider { CameraManager } } bind { contexted().provider { InputMethodManager } } bind { provider { Application } } bind { contexted().provider { SubscriptionManager } } bind(tag = "obb") { contexted().provider { File } } bind { contexted().provider { AccessibilityManager } } bind { contexted().provider { SensorManager } } bind { contexted().provider { WindowManager } } bind { contexted().provider { TelephonyManager } } bind { contexted().provider { AppWidgetManager } } bind { contexted().provider { ApplicationInfo } } bind(tag = "files") { contexted().provider { File } } bind { contexted().provider { WifiManager } } bind { contexted().provider { FingerprintManager } } bind { contexted().provider { LauncherApps } } bind { contexted().provider { SharedPreferences } } bind { contexted().factory { String -> SharedPreferences } } bind { contexted().provider { LayoutInflater } } bind { contexted().provider { ShortcutManager } } bind(tag = "packageCodePath") { contexted().provider { String } } bind(tag = "packageResourcePath") { contexted().provider { String } } bind { contexted().provider { ContentResolver } } bind { contexted().provider { AccountManager } } bind { contexted().provider { UsageStatsManager } } bind { contexted().provider { LocationManager } } bind { contexted().provider { ClipboardManager } } bind(tag = "packageName") { contexted().provider { String } } bind { contexted().provider { TvInputManager } } bind { contexted().provider { ConnectivityManager } } bind { contexted().provider { ActivityManager } } bind { contexted().provider { PowerManager } } bind { contexted().provider { DevicePolicyManager } } bind { contexted().provider { TextServicesManager } } bind { contexted().provider { NfcManager } } bind { contexted().provider { Resources } } }

Container has binding for simpleSharedPreferences and named ShredPreferences but when i try to retrive dependency from container i have exeption

romainbsl commented 6 months ago

I reproduced your issue. I don't know when this has been changed, but as you can see in the logs all the Android stuff are contexted. This means that they must be retrieved on a given context. As we are on Android, they are bound to a Android Context.

So when binding your AppSettingsService you need to specify this context. like:


val myModule by DI.Module {
    bindSingleton<AppService> {
        DefaultAppService(
            sharedPreferences =
                on(instance<Context>())
                    .instance<String, SharedPreferences>(arg = "myPref")
        )
    }
}

What's important here, is on(instance<Context>()).

Hope this helps.

SeleznevAM commented 6 months ago

Thank you, very match!!! It's helped me %))