paint-brush
7 Kotlin Libraries Every Developer Should Knowby@yaf
5,503 reads
5,503 reads

7 Kotlin Libraries Every Developer Should Know

by Fedor YaremenkoOctober 15th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Originally Kotlin was developed as a language for JVM, and interoperability with Java is one of its main features. This allows to call Java code and use existing Java libraries. Kotlin libraries provide interfaces that use language features like null safety, default and named arguments, and DSL. Kotlin's ecosystem is growing every year. In this article, we consider the most common libraries written in Kotlin.

Company Mentioned

Mention Thumbnail
featured image - 7 Kotlin Libraries Every Developer Should Know
Fedor Yaremenko HackerNoon profile picture

Originally Kotlin was developed as a language for JVM (Java Virtual Machine), and interoperability with Java is one of its main features. This allows to call Java code and use existing Java libraries. The Java world has a huge number of libraries, and the most common of them are considered in my previous article.


Kotlin libraries provide interfaces that use language features like null safety, default and named arguments, and DSL (Domain Specific Language). Kotlin's ecosystem is growing every year. In this article, we consider the most common libraries written in Kotlin.

1. Kotlinx Coroutines

Let's start with considering the three official libraries: kotlinx.coroutines, kotlinx.serialization, and Ktor. They are developed and supported by language creators, described in official Kotlin documentation but not included in the standard library.

Kotlin provides suspend modifier keyword that marks a suspending function or, in other words, coroutine. The standard library gives only basic coroutines API.


For writing code with asynchrony and parallelism in a convenient way, you need to use Kotlinx Coroutines library. It allows using high-level primitives for writing concurrency code, channels between coroutines, exception handling, mutex, and actors.

2. Kotlinx Serialization

Kotlin is not only a JVM language. Kotlin/JS transpiles a code to JavaScript that can be used on frontend or Node.js backend, Kotlin/Native is a compiler to native binaries, Kotlin Multiplatform Mobile (based on Kotlin/Native) provides the ability to use a single code on both Android and iOS. The frontend, backend, and mobile apps need to interact with other components using serialization. Hence, Java libraries like Jackson or Gson are not usable for code that is compiled or transpiled into a not-JVM platform.


JetBrains, the creator of Kotlin, has developed cross-platform kotlinx.serialization library. It supports several formats such as JSON, protocol buffers, and CBOR. Kotlinx Serialization comprises a compiler plugin, runtime library with core serialization API, and other runtime libraries. As Kotlinx Serialization starts work at compile-time, it has two advantages. Firstly, the library uses generated code to serialize objects instead of reflection, which positively affects performance. Secondly, if a developer forgot to mark classes as @Serializable, it will be captured at compilation time, not at runtime.


Also, in contrast to Java libraries, Kotlinx Serialization supports default values for properties.

3. Ktor

Often serialized objects are transmitted through the network over the HTTP protocol. Ktor is a lightweight asynchronous web framework. In official Kotlin documentation, Ktor is recommended for building HTTP clients and servers. Ktor leverages kotlinx.coroutines and kotlinx.serialization, and can also be used on all Kotlin platforms. But if you want, you can use Jackson or Gson serialization for JVM applications.


Ktor is divided into several libraries and supports plugins, so you can add only needed dependencies if you develop a microservice or frontend component that uses WebSockets. With the framework's flexibility, both of these solutions will be lightweight. Additionally, Ktor provides testing engines for client and server sides that reduce the execution time of tests.

4. Dokka

Documentation is an important part of developing applications. Like JavaDoc in Java, Kotlin uses KDoc documentation language that is very similar to JavaDoc. Documentation is generated by Dokka tool. Dokka supports both KDoc in Kotlin and JavaDoc in Java, so it can generate documentation for the whole mixed-language projects. Dokka can generate documentation in Javadoc, HTML, and Markdown formats.

5. Mockk

Mockito is the most common mocking Java library. But you may encounter some issues when testing a Kotlin code with Mockito. For example, notNull method returns null, so we can not use it for a not-null parameter in Kotlin because NPE is thrown. Also, Mockito does not support other language features of Kotlin. That is why using a library written on Kotlin and especially for Kotlin is the best option.


Mockk provides mocking objects, enums, extensions, top-level functions, coroutines, Nothing, chained calls (including in a hierarchical form), private methods, and constructors naturally. Like Mockito, it supports annotations, partial mocks, mocks that implement multiple interfaces, and capturing method arguments.

6. Android KTX

Android developers actively use the following two libraries. Android KTX (KTX means Kotlin Extensions) is a library developed by Google to make developing Android apps easier. Android KTX wraps existing Android APIs and does not provide new functionality. To a greater extent, it consists of extension functions with idiomatic declarations that use Kotlin features like parameters with default values and coroutines. Android KTX comprises the core module, which covers Animation, Content, Graphics, Drawables, Net, OS, Preference, Text, Transition, Util, View, Widget, and other specific modules.

7. Koin

Koin is a lightweight alternative for dependency injection frameworks like Dagger. Koin is not a dependency injector itself and implements Service Locator (anti-)pattern. But with Kotlin language features, usage of Koin is very similar to DI. One of the main features of Koin is a concise DSL. In contrast to Dagger, Koin is easy to learn and understand and resolves dependencies at runtime without code generation.

Conclusion

Kotlin libraries use the full power of the modern language and help to write idiomatic code. Not all of them are supported on Kotlin/Native or Kotlin/JS platforms, but the number of cross-platform libraries is constantly growing with the rising popularity of Kotlin.