← Impulso Blog

Kotlin and Koin: An evolution of Java and Dagger?

Kotlin and Koin, alternatives to Java and Dagger, may be good options for anyone looking to simplify Android mobile development.

Kotlin and Koin

Editorial translation by Impulso.

Understanding the main features and advantages of programming languages for mobile development is essential for anyone who wants to work on apps. In native Android development, one of the most common comparisons is Java vs Kotlin.

Java is perhaps the best-known language on the market. Kotlin is newer and less well known.

Koin, a dependency injection tool developed in Kotlin, also emerged as an alternative to Google Dagger. Both make life easier for developers.

But there are differences. Want to know what they are? Read on to find out!

What is Kotlin?

Kotlin is an object-oriented language created by JetBrains in 2011. One of its main features is that it’s multiplatform. You can use it to develop an entire app, from the back end to the front end.

It’s also statically typed and compatible with Java. That means you can migrate app development from one language (Java) to the other (Kotlin) gradually, class by class, without disrupting how the app works.

Java vs Kotlin

Java vs Kotlin

There are several reasons to use this language. Here are some of the main ones! 👇

1. Greater simplicity

For starters, there’s less “verbosity.” In Kotlin, you can write standalone functions without creating a class just for them. That means you can write fewer lines for each operation than you would in Java.

2. Null Safety

Kotlin also provides null safety. Its type system can distinguish between references that cannot hold null values (non-null references) and those that can (nullable references).

Basically, the developer defines which parameters or variables can accept a null value. That makes it possible to avoid NullPointException. This way, the language doesn’t produce that compilation error.

3. Extensions

Another interesting feature is support for extensions. You only need to write the function, without creating a separate class. You don’t have to pass the value you want to modify or process as an argument either, because the function becomes an extension on a string.

So you type a string, a dot (the symbol), and the name of the extension. From then on, you can use the same dot notation on any string to access the extension you created.

Basically, an extension lets you add new functionality to a class without dealing with inheritance. You don’t need to use a Decorator pattern either.

4. Details that make programming easier

Kotlin also makes life easier for developers in smaller ways. You can omit semicolons after statements, declare variables without specifying their types, and use features like coroutines.

This feature makes it easier to write asynchronous code in a sequential style without using the Callback pattern. In short, coroutines are like lighter threads.

As a result, they use fewer computing resources. They’re designed for non-blocking and parallel tasks.

Dependency injection: Dagger and Koin

Dagger and Koin: Dependency injection

Dependency injection is a useful technique widely used in Android development because it allows objects to be reused. It makes the software responsible for initializing dependencies.

When you use a dependency injection framework, the framework itself is responsible for things like instantiating dependencies at a particular point in the code.

Dependency injection offers several advantages for a project, including loose coupling, easier refactoring and unit testing, and, of course, code reuse.

Dagger is a dependency injection tool developed by Google. Koin, which was developed in Kotlin, is an alternative.

Koin vs Dagger

Koin vs Dagger

Although Dagger is a “powerful” tool, it’s more complex to configure. That can get in the way on smaller, simpler software projects.

The same goes for its extensive capabilities. After all, sometimes a simple solution is more useful than one with more features than you need.

That added complexity also means a steep learning curve. It generally takes longer to learn how to configure Dagger. The tool can also be seen as somewhat “verbose.”

Koin works primarily through module declarations. It has some advantages over Dagger, including being easier to learn and use. It also has drawbacks, though.

For example, certain dependency injection errors can go undetected when you “build” the app, causing it to crash later when you go through the affected flows.

With Dagger, on the other hand, omissions can show up at compile time. In other words, when you “build” the app, you’ll get an error and can figure out what happened so you can fix it.

However, using Dagger tends to make an app’s build take longer, since all dependencies are resolved at compile time.

Hilt and KMM

Cubes showing social network and app icons on a blue surface.

It’s worth pointing out that Kotlin and Koin don’t spell the “end” of Java and Dagger. Java is used for mobile development as well as other types of projects. Kotlin itself runs on a JVM.

Google recently released Hilt, a “new” Dagger. One of the tool’s goals is to simplify Dagger’s infrastructure.

It also aims to develop a standard set of scopes and components to make code easier to read, configure, and share across apps.

However, some plugins already do some of what Hilt aims to do. For example, the plug-in Anvil is a library built on top of Dagger specifically to make it easier for Dagger and Android to work together.

Kotlin, for its part, has a project called “Kotlin Multiplatform Mobile” (KMM). The aim is to attract more hybrid app developers, since KMM will make it possible to develop apps for both Android and iOS.

When comparing Java vs Kotlin and Koin vs Dagger to decide which options to use, remember to try them all. That way, you can make a more informed decision.

➕ Take a moment to learn more about Android modularization and build your mobile development knowledge!

Related articles