What’s next with Koin? — 2.2 & 3.0 releases

Arnaud Giuliani
Oct 13, 2020 · 6 min read

Hi Koin users 👋, Koin 2.2.0 is almost there. This new milestone brings better stability, some API rework and also, helps you inject Work Manager & Jetpack Compose. Another big step, we have started to open Koin 3.0.0 alpha releases for the Kotlin Multiplatform version of Koin.

Let’s see what is coming in this 2.2 release & the roadmap for 3.0, and check the last Koin version, to wait for the stable version update.

Making Android ViewModel API more consistent

Starting from Koin 2.1, we introduced the possibility to inject the SavedStateHandle parameter through your injected parameters. This was requiring a new keyword by stateViewModel() and a strict way of using it with injection parameters.

This keyword and related APIs have been removed: we just want to keep one viewModel API and try to avoid any complexity. The state is in your ViewModel constructor: no need anymore to use an extra keyword for your components that are using the state Bundle and pass it via injection parameters. Just resolve it with get() function in your Koin definition:

You can use the state parameter, to return a function to define a Bundle data: state = { Bundle(...) }

Another point for ViewModel API, we bring back the ownership parameter with owner :

This parameter allows wiring your ViewModel with a custom ViewModelOwner, like Graph Navigation component:

owner = { ViewModelOwner.from(findNavController().getViewModelStoreOwner(id)) }

Injecting your Work Manager

Part of Koin’s new feature, inject a Work Manager:

  • Define a Worker class, including constructor parameters: Context & WorkerParameters (also your other dependencies)
  • Use the worker DSL definition keyword, to declare your definition
  • Just enqueue a task for your Worker

Add the Koin Work manager module to your project with this Gradle package: org.koin:koin-androidx-workmanager:$version

Be sure to update your manifest with the following:

And add workManagerFactoryat startKoin:

Ready for Jetpack Compose

The other good surprise of those past few months of work is now the capacity to inject your components in your Jetpack Compose application. Just setup Koin as a regular Android app and add the Compose module: org.koin:koin-androidx-compose:$version

Now you can inject any @Composable function with: get() , by inject() , getViewModel() or even by viewModel() APIs.

We encourage you to inject your component in the function parameters like this:

This allows you to inject your component with Koin, in the default implementation but doesn’t block you to inject any other value if needed (testing components…)

You can find Sean McQuillan’s JetNews Compose sample app setup with Koin here: https://github.com/InsertKoinIO/koin/tree/master/koin-projects/examples/androidx-compose-jetnews

Writing Android scopes with more simplicity

In previous versions, addressing scopes with Koin was not an easy thing. Things are progressing, as it’s easier to describe a scope for a given component (define a scope for a given class).

We will assume some API breaking: Koin won’t propose anymore Scope API extensions on generic type. Instead, we propose to use theKoinScopeComponent interface to explicitly bring it.

The new proposal for scoping instances in Koin is the following:

  • Scoped Component: ScopeActivity, ScopeFragment & ScopeService. Just use those classes that already create the given scope, and proposes inject() & get() functions directly tied to your scope
  • Use KoinScopeComponent interface to unlock scoped component extensions get , inject viewModel
  • Dedicated scope lifecycle functions: activityScope() & fragmentScope() , to let wire the scope tied the component lifecycle (backed by ViewModel) — AndroidX only

The idea is to help you declare components that will follow one Android component’s scope.

Here below an example using ScopeActivity :

If you can’t use ScopeActivity or any abstract class directly, use the KoinScopeComponentto help you declare your scope:

Just override the current scope property definition like above, with newScope()function. This way you gain access to KoinScopeComponent.inject and KoinScopeComponent.get extensions, and create lazily your scope.

In the AndroidX version of the scope functions & components, to be more close to the Activity/Fragment lifecycle, you can use activityScope() or fragmentScope() scope functions to create a scope instance tied to the component:

With such functions, no need to manage theonDestroy phase “by hand”. The associated scope will be closed automatically, as it’s backed by a dedicated ViewModel instance to support the linked Koin scope.

This is available in the Koin scope Gradle packages: org.koin:koin-android-scope:$version & org.koin:koin-androidx-scope:$version

Android & AndroidX Gradle modules

For those who didn’t notice, Koin brings the koin-android & the koin-androidx modules version. If your app is using AndroidX libraries, pick the right modules flavor 👍

Stronger Internals

Among the raised issues in Koin 2.0 & 2.1, was the inconsistency ofstartKoin initialization. This is now fixed, bringing simpler Koin Context building. Please note that now,startKoin is using GlobalContext by default. KoinContextHandler has been deprecated.

One important requirement was to lock most internal APIs of the project. Check your code if you were using internal Koin API.

The other thing I’ve seen around is the misuse of KoinComponent interface to extend service locator API. Be aware that as a Koin framework user, you shouldn’t need to use this interface. KoinComponentinterface must be used to extend Koin API or “bootstrap” component where we don’t have Koin APIs for that. In your app, .

For such sensible API functions, we introduce the @KoinApiExtension annotation to such API.

Graph resolution & injection parameters

Important addition is the new capacity to put the injected parameters to the graph resolution, allowing to resolve any injected parameters with the get function:

  • allow passing technical parameters to the stack (like StateHandleBundle)
  • allow compact definition writing: viewModel<>() or any future API
  • allow any evolved integration of Koin with compiler plugin

Improving testing tools

The test packages are getting some good improvements as well:

  • new JUnit 5 test tools: org.koin:koin-test-junit5:$version
  • Mock Provider allow bringing your own mock library
  • CheckModules help define default values, to be injected in the graph to check

Koin 3.0 — Preparing the next milestone 💪

The big work of the next milestone is to merge branch 2.2 to the new . This implies the rework of the Gradle modules and all projects. The documentation will require some work, to allow some new content & help users for the Multiplatform part. target will be the end of the year. Koin Multiplatform is included in https://github.com/touchlab/KaMPKit. Some projects are already using it well: https://github.com/joreilly/PeopleInSpace from John O'Reilly

Note that the old koin-android-* packages (covering API 28) will be surely dropped. The scope packagekoin-androidx-scope will be integrated into the main android package. A migration notice will be published

The other work target is the beginning of Kotlin compiler plugins investigation to help us write things even easier 🎉

Hope you’ll enjoy this new release. Cheers. ✌️

Koin developers

Latest news about Koin