how to inject dependency outside activity or fragment in Kodein or Koin?

kotlin dependency injection
android viewmodel dependency injection
android koin constructor injection
koin sharedpreferences
koin-test
koin qualifier
kodein tutorial
kodein vs koin

want to initialize interface in a non activity or fragment class with Kodein DI Android

sample shows only hot to use Kodein inside activity, but not on the other parts

 class MainViewModel() :  KodeinAware{
   override val kodein by closestKodein()
   val repository : Repository by instance()
 }

in activity it works, but in other classes it shows error. I want to initialize interface inside another class

closestKodein only works in Android Context aware classes (such as fragments & activities). To use it outside of these classes, you need an Android context.

The android documentation clearly states:

Caution: A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

[...]

If the ViewModel needs the Application context, for example to find a system service, it can extend the AndroidViewModel class and have a constructor that receives the Application in the constructor, since Application class extends Context.

Therefore, to access Kodein from a ViewModel:

class MainViewModel(app: Application) : ApplicationViewModel(app), KodeinAware {
    override val kodein = app.kodein
    val repository : Repository by instance()
}

how to inject dependency outside activity or fragment in Kodein , want to initialize interface in a non activity or fragment class with Kodein DI To use it outside of these classes, you need an Android context. koin library solved this problem https://insert-koin.io/docs/1.0/getting-started/android-​viewmodel/  You can use "by inject ()" in Fragment. But if your app is based on Android Lifecycle Architecture components, Koin needs more extensions to help you :/ If you have a Context or ContextApplication object around, you can use the "getKoin ()" accessor, for then inject your component. Android Lifecycle Architecture components is on the roadmap

Simpy pass a context or activity as param

override val kodein by closestKodein(context)

More info https://kodein.org/Kodein-DI/?5.0/android#_getting_a_kodein_object

How to inject outside a Fragment or an Activity? (Android Lifecycle , arnaudgiuliani changed the title How to inject outside a Fragment or an KoinComponent /** * inject lazily given dependency * * @param  The House class, which can access Koin’s inject() method once it implements the KoinComponent interface: We have to declare a Koin module for providing the dependencies: MainApplication has to implement the KoinComponent interface as well, and call startKoin() in its onCreate() :

Use it in any place. appKodein is global function.

val dataLayer: DataLayer = appKodein().instance()

how to inject dependency inside non activity class? · Issue #172 , Dependency injection is an instrumental technique, used to decouple dependencies from your code. Dependency Injection with Kotlin — #Kodein #​KOIN Because we are on Android here, let's see how we would do it in an Activity. (for AppCompatActivity , other available for Fragments and Services). There is an alternative to KODEIN called KOIN. It’s younger, doesn’t have the Java interoperability part, but on the other hand, it’s much slicker and supports the new ViewModel from Google out of the box. In next blog post, I will show some more advanced features of Kodein, and we can also look into KOIN, and it’s comparison with KODEIN.

override val kodein by kodein(activity!!)

Dependency Injection with Kotlin, One thing to note is out of the three, technically only Dagger uses a dependency injection (DI) pattern; Koin and Kodein are service locators. Multiton binding is very similar to Factory binding. The only difference is that the same object is returned for the same argument in subsequent calls:. val kodein = Kodein { bind<Dao>() with singleton { MongoDao() } bind<Service>() with multiton { tag: String -> Service(instance(), tag) } } val service1: Service = kodein.with("myTag").instance() val service2: Service = kodein.with("myTag

Exploring Dependency Injection in Android, Use Koin as dependency injector in your Kotlin apps, you will fall in love with its Architecture Components what makes more attractive than others such as Kodein And then, in the activity or fragment, we need to retrieve it. how to inject dependency outside activity or fragment in Kodein or Koin? Use it in any place. appKodein is global function. val dataLayer: DataLayer = appKodein().instance()

Insert Koin in your apps, simplify dependency injection in Kotlin , The first time I came across dependency injection was when I started If you are using dependency injection on Android you have probably heard of Dagger2, Koin, and Kodein but Activity , Fragment , and ViewModel should come to mind as each of These classes can't be used outside of this scope. Katana is a lightweight, minimalistic dependency injection library (similar to the service locator pattern) for Kotlin on the JVM, designed especially with Android in mind. Extremely lightweight footprint (only ~15kB in classes and ~130 methods after ProGuard), plain Kotlin, no third-party dependencies

Toothpick 3, Ideally we would probably want to have also Fragment scopes, in an outer layer of our What's Kodein. 01:53 Injecting dependencies on the Activity Scope. What is Koin? A pragmatic lightweight dependency injection framework for Kotlin developers. Written in pure Kotlin using functional resolution only: no proxy, no code generation, no reflection! Koin is a DSL, a lightweight container and a pragmatic API.

Comments
  • If you are using Android ViewModel, then I suppose MainViewModel should extend ViewModel.
  • but I can not send context to ViewModel
  • I've edited the answer to be specific to Android ViewModel. Hope it answers your question.