Koin 0.9.0 — Getting close to stable release

Arnaud Giuliani
Mar 2, 2018 · 4 min read

Hi Koin users :) We begin this new month of March with some new Koin content. And again, thank you for your feedback. At the agenda of this last release: dynamic parameters, context scope isolation, better start API, a big upgrade of the ViewModel extensions... and other good things. Let’s go 👍

Upgrade your Gradle as usual with this new release number:

compile "org.koin:koin-spark:0.9.0"compile "org.koin:koin-android:0.9.0"compile "org.koin:koin-android-architecture:0.9.0"...

DSL update

bean ~ provide => singletonfactory ~ provide(isSingleton=false) => instance factory 

It was also weird to have bean and factory limited to definition (and can’t use the bind operator). Now you can use the bind operator on bean and factory definition.

Provide is just deprecated for now. You have the time to update your definitions. It will be passed as internal visibility in a next releases.

Dynamic parameters

Now you can pass parameters as arguments to your definition. Here below, we would like to pass directly our View to our presenter:

Every definition can use this lambda argument, which offers a Key/Value map.

Now, add your parameter in the parameters arguments in your by inject() or by viewModel() function. It can be an interesting way of binding an MVP architecture style:

That’s it. You will pass your parameter at runtime. If you use DryRun() to test your modules, you will have to provide a default parameter else Koin won’t be able to build your instance.

Context scope isolation

  • visibility — don’t allow components from incompatible scope to inject it
  • durability — a context can be dropped with all its instances at any time

Given some scope configuration:

The following visibility rules are applied:

Components from "A" can see components from context "A" and ROOTComponents from "B" can see components from context "A", "B" and ROOTComponents from "C" can see components from context "C" and ROOT

Now, if Koin can’t find one of your bean definition, check your contexts visibility 😎

Context release notification

Now you can react on Koin context drop, and handle more deeply your components lifecycle.

Android silent logging

Android ViewModel API updates

  • can now use parameters argument (like for by inject() functions), to pass dynamic parameters
  • can use the key argument, to indicate which instance to reuse from the Android ViewModelProvider

Below a ViewModel example, with dynamic parameter:

Dynamic parameters with ViewModel functions

Finally, If you can’t use the inferred type version of the API, you can now use the getViewModelByClass() and viewModelByClass() functions, which takes KClass argument to find your ViewModel.

Writing librairies with Koin (unlock loading Koin modules)

The startKoin API has been exploded and more opened. You are still limited to only one call of startKoin() in your app, but now you can load Koin modules from your library pars with loadKoinModules() function.

From Java to Kotlin and Koin

To help people bringing Koin to their project, I suggest a “holder” pattern. The holder consists of a Kotlin class, that will be manually instantiated in your Java activity to bring your Koin stuff. This Kotlin holder then let’s you use the by inject() syntax to lazy inject your components, and all the Kotlin Koin syntax where you can use it. Check below the gists that illustrates this:

https://gist.github.com/arnaudgiuliani/446e0d8ee79f4bda38b3057b01098f2e

Koin has been launched in mid 2017. The main features becomes to remains stable. Next releases will be focused on bug fixes and stability. Being compatible with Espresso for UI testing on Android is one of our “last” big subject for the target stable release.

Next months we will also see if we can propose Koin in tech conference in France :)

Stay tuned 👍

Koin developers

Latest news about Koin