hey Guys! I’m struggling with a strange issue! I’m...
# kodein
h
hey Guys! I’m struggling with a strange issue! I’m injecting SharedPref all around the app but sometimes, I’m getting this error.
Copy code
org.kodein.di.Kodein$NotFoundException: No binding found for bind<SharedPreferences>() with ? { ? }
    Available bindings for this type:
            module ⁣androidModule {
                bind<SharedPreferences>() with contexted<Context>().factory { String -> SharedPreferences }
                bind<SharedPreferences>() with contexted<Context>().provider { SharedPreferences }
            }
    Registered in this Kodein container:
            module analyticsModule {
                bind<Analytics>() with singleton { AnalyticsImpl }
            }
            ....
            module apiM
I tried to solve it by manually bind SharedPreferences, without any luck
Copy code
bind<SharedPreferences>() with singleton { PreferenceManager.getDefaultSharedPreferences(instance()) }
but what I eventually do (as I need Android Context as well for injection) is to use androidXmodule in the Application object. However the issue is still coming
Copy code
override val kodein = Kodein.lazy {
        import(androidXModule(this@MainApplication))
        import(generalModule)
        import(repositoryModule)
        import(viewModelModule)
        import(appSettingsModule)
        import(apiModule)
        import(useCaseModule)
        import(analyticsModule)
        import(locationModule)
        import(dataSourceModule)
    }
Please help me! I can’t find the solution
d
Are you providing the context? ex:
Copy code
bind<SharedTokenManager>() with singleton {
    val context = instance<Context>()
    val sharedPrefs = on(context).instance<SharedPreferences>("tokens")
    SharedTokenManager(sharedPrefs)
  }
h
Nope, I'm using context from the androidxmodule
So technically I'm calling it with instance
How do you provide the context and the shared pref?
d
on(context).instance<SharedPreferences>("tokens")
val inflater: LayoutInflater by kodein.on(context = getActivity()).instance()
h
with that approach I’m getting
Copy code
Caused by: org.kodein.di.Kodein$NotFoundException: No binding found for bind<SharedPreferences>(tag = "settings") with ?<Context>().? { ? }
what bind do you use for context and for sharedpref?
if I provide a context with instance and retrieving the sharedpref out of it is working fine. Or do you have some kind of sharedpref wrapping object?