Koin 3.4 and Koin Annotations 1.2 are out! Starting out Koin Compose 1.0.

Arnaud Giuliani
Koin developers
Published in
3 min readApr 13, 2023

--

Koined image from Hal Gatewood

Hello dear Koin community 👋

I’m pleased to announce the Koin releases for H1 2023: Koin 3.4 and Koin Annotations 1.2. It’s also a good time to announce Koin for Compose 1.0, which opens dependency injection from Android Jetpack Compose to the whole Jetbrains Compose platform.

Let’s dive into the content of those new versions 🚀🤘

Koin 3.4

First, we are making some good updates with all our dependencies and mainly catching up with Kotlin 1.8.10. Some other updates are coming like for Android (appcompat:1.6.1, activity-ktx:1.6.1, fragment-ktx:1.5.5 …) and Ktor 2.2.4.

The graph verification API Verify() has been updated to better detect cycles and handle primitives as already “whitelisted” types (you don’t need to declare those last ones into your tests). The “whitelisted” types are considered as existing by the API as if you were injecting it by parameter or using a dedicated setup function like androidContext() in your Koin start process.

Also in terms of testing, the koin-android-test project proposes testing features for Android. One first feature is the androidVerify() function to help you verify Android-related modules:

androidAppModule.androidVerify()

This extension brings the following default Android types out of the box:

val androidTypes = listOf(
Context::class,
Activity::class,
Fragment::class,
Application::class,
SavedStateHandle::class,
WorkerParameters::class
)

A new Kotlin Multiplatform API called KoinPlatform, to help propose easy access to all platforms for some main features. You can do KoinPlatform.getKoin() to get the current instance of Koin from any Kotlin platform.

In terms of native targets, incoming versions of Kotlin may update targets or even deprecate/remove some old ones. Stay tuned.

And finally, Koin now offers an Extension management system to help extend Koin's capacities. Let’s see our first extension below.

Extension Manager, Lazy Modules & Coroutines Engine

Let’s unblock the Koin startup process with background modules loading, by running modules loading in coroutines. Warning, this feature is still experimental (API may change in the next versions), but it’s really super promising.

Our first Koin Extension extends Koin by including a Coroutines engine, with the following objectives:

  • Lazy modules definitions
  • Background loading of lazy modules
  • wait and run code after loading completion
  • allow synchronous and asynchronous module loading

Let’s take a small example, with the following classes


// Just 2 classes to inject
class ClassA : IClassA
class ClassB(val a: IClassA)
interface IClassA

Let’s write lazy modules with it:

val m2 = lazyModule {
singleOf(::ClassB)
}
val m1 = lazyModule {
includes(m2)
singleOf(::ClassA) { bind<IClassA>() }
}

And finally, let’s start:

startKoin {
lazyModules(m1)
}

That’s it, it’s loading in background freeing the current process.

If you need to wait for completion, Koin offers a suspend function called Koin.waitAllStartJobs() or the JVM blocking function Koin.awaitAllStartJobs().

The lazyModules() function from Koin application DSL offers a new way to load modules. After that, up to you to choose modules() or lazyModules() in your application configuration.

Koin Annotations 1.2

The Koin Annotations project is getting stronger with a few additions.

Top-level function annotation allows declaring a component with a simple function:

@Single
fun buildB(a : IClassA) = ClassB(a)

The definition will be tied to the closest module declaration.

The new @KoinWorker annotation helps you declare Worker components:

@KoinWorker
class SyncWorker (
private val appContext: Context,
workerParams: WorkerParameters,
private val niaPreferences: NiaPreferencesDataSource,
) : CoroutineWorker(appContext, workerParams)

Don’t forget to start the WorkManagerFactory at start:

Koin for Compose 1.0

One of the big announcements also is the opening of the koin-compose project, as the base of all Compose UI-related tooling. It’s based on the latest Compose compiler versions:

  • Compose compiler 1.4.2
  • Jetbrain Compose 1.3.x
  • Introduced new APIs such as koinInject, rememberKoinScope, rememberKoinModules

The project will chase new Compose compiler versions closely.

We will dive in details later about it, to give more details.

Thanks to all the contributors and give us your feedback on Slack or GitHub.

Cheers.

--

--

Arnaud Giuliani
Koin developers

Creator/Maintainer of Koin framework -- Kotlin Google Dev Expert -- Cofounder @ Kotzilla.io