3 min read

Koin Annotations 1.0 - Now Stable!

I’m happy to announce that the Koin Annotations project is now getting to its first stable version. With this, we are introducing a new way to declare modules, components, and new tooling capacity.
Koin Annotations 1.0 - Now Stable!
Andrew RidleyUnsplash

Hi Koin users 👋 I’m happy to announce that the Koin Annotations project is now getting to its first stable version. With this new launch, we are introducing a new way to declare modules, components, and new tooling capacities.

Check out the Koin Annotations setup page to get ready.

Why would we matter about having annotations in the Koin framework? we are already upgrading developer experience with the new DSL introduced in 3.2 (see previous "Inside Koin 3.2 - new DSL" article). Even if the Koin DSL is really easy to read and write, some could find that scaling on big apps with it is challenging.

Since the beginning, our obsession with Koin is to help you build your Kotlin application with dependency injection in a concise and smart way ... and even make you forget that you are using Koin.

Annotations allow being less disturbed by your configuration files and keep you focused on your code. While your application codebase is growing, annotating your component classes is more straightforward, that going back to your configuration file. Our plan here, is not to reproduce once again what you could find elsewhere, but rather propose a smarter Koin experience. We let you write the minimum (only one annotation), and we care about the rest thanks to Kotlin Compiler and Google KSP.

Just annotate it, what else? ☕️

To declare a component in Koin, use one of the definition annotations. For example, to declare a unique instance, let's add the @Single annotation on our class:

class MyComponent()

Koin compiler plugin analyze your first constructor and generate Koin DSL configuration for you. For example, you can deal with nullable dependencies directly (nothing to do from your side):

class MyComponentB(val a : MyComponentA?)

You can even specify to use an injected parameter with @InjectedParam by adding it to your constructor parameter, like this:

class MyComponent(@InjectedParam val id : String)

// inject the id when resolving
val myComponent : MyComponent by inject { parametersOf("_an_id_") }

The Koin plugin will generate the needed DSL for you. As it's Koin DSL, you can easily debug it or find where a component is built.

The Koin Annotation documentation gathers all annotations that you can use on your classes: https://insert-koin.io/docs/reference/koin-annotations/annotations#definitions

Every definitions will be generated inside a Koin module, let's see how.

Easy way with Modules 📦

To declare a Koin module with an annotation, we need a class and annotate it with @Module like follow:

class MyModule

From a module class, you can declare a definition with a class function, and use a definition annotation on it:

class MyModule {

    fun myComponentB(a : MyComponentA?) = MyComponentB(a)

The easy thing here, is the use of @ComponentScan to scan definitions in the target package. No need to declare them in functions:

class MyModule

Don't miss out also the possibility the include other modules, that uses new includes() DSL function (introduced in 3.2) under the hood:

class MyModuleA

@Module(includes = [MyModuleA::class])
class MyModuleB

Let's start 🚀

The Koin compiler project in 1.0.0 is using Google KSP in version 1.6.21-1.0.5. Please, refer to the setup page for complete setup information.

To use our Koin annotated code, simply use the usual startKoin function and the class modules with their generated .module extension:

// to use generated code 
import org.koin.ksp.generated.*

fun main() {

	startKoin {

Koin KSP Compiler plugin generates your configuration as an extension on your module class. You are now ready to retrieve your dependencies!

Koin DSL or Annotations? Both ✌️

You are not forced to use either DSL or annotated approach. You can mix whether you need it. You can use both DSL and annotated modules. You only need to specify in Koin what you want to load in terms of modules. That's it.

It's super easy to add Koin Annotations to any existing Koin project. Be sure to use Koin 3.2.0 or superior.

To finish, here are some resources to let you start easily. Below are the quickstart project with Koin Annotations:

If you are interested in Kotlin Multiplatform example app with Koin Annotation, take a look at our Hello KMP app on branch "annotations": https://github.com/InsertKoinIO/hello-kmp/tree/annotations

Hope this new project will interest you! We will be happy to see your feedback.

Cheers 👋 Arnaud