Konsist: Protect Kotlin Multiplatform projects from architecture guidelines violations.
Tags: kotlinmultiplatform, staticcodeanalysis, konsist, architecture
Published: 2024-01-21 05:36:35

---
title: Konsist: Protect Kotlin Multiplatform projects from architecture guidelines violations.
published: true
date: 2023-10-08 12:50:02 UTC
tags: kotlinmultiplatform,staticcodeanalysis,konsist,architecture
canonical_url: https://medium.com/@lahirujay/konsist-protect-kotlin-multiplatform-projects-from-architecture-guidelines-violations-d88db0614cbd
---

Step-by-step instructions on integrating **Konsist** with **Kotlin Multiplatform Mobile**.

Let’s say you have created a project architecture and you expect your team members to follow the architecture guidelines. But there might be a developer who does not follow the guidelines strictly always. Depending on the scope of the project, developers may get reassigned, or teams may be divided into multiple small teams where they must focus on specific modules. So most of the team members will not have a holistic view of the project and with time, things can get messy.

And here comes the Linter tool to save your day. Even though it won’t solve your personal debt and problems, it certainly prevents you from accumulating technical debt and also provides visibility into your code health.

So to have consistency through the project structure and to test whether the project architecture guideline hasn’t been violated we can use an architectural linter called **Konsist**. Konsist is a Kotlin static code analyzer that supports Kotlin-based projects including Android, Kotlin Multiplatform, and Spring. Although this library is in its early stages of development, it covers the problems and usecases we see every day today.

**Konsist** offers two types of checks: Declaration Checks, which test for declaration-level violations, and Architectural Checks, which test for architectural guidelines and boundaries.

Without further delay, let’s dive right in.

#### **Prerequisites**

> [#Kotlin Multiplatform Mobile Project](https://kotlinlang.org/docs/multiplatform-mobile-getting-started.html)  
> [#Konsist](https://docs.konsist.lemonappdev.com/getting-started/readme)

For this example, I’m using a Multi-Module Kotlin Multiplatform Mobile project with the following structure. Alternatively, you can use Kotlin Multiplatform Mobile’s default structure.

![](https://cdn-images-1.medium.com/max/1024/1*5Czgp5Pxyi0UCEtUOBx9gw.png)
_Multi-Module KMM_

#### **Configuration**

As a first step, we’ll create a new module for the Konsist Test that separates the _konsist test_ from the _unit test_ to improve readability.

1. Right click on the shared module.
2. Select _New_ then _Module_ to open the _Create New Module_ window.
3. Select the Kotlin _Multiplatform Shared Module_ Template
4. Enter your desired module name. Ex: _:shared:konsisttest._
5. Name your package. The rest is the same as the following image, so click _Finish_.

![](https://cdn-images-1.medium.com/max/1024/1*qBhryHjPTsHTrQRQv_JZyg.png)

![](https://cdn-images-1.medium.com/max/1024/1*Apft1p-1mdgJSxm9dVzZZQ.png)

You will then be able to see your new module on the project as follows.

![](https://cdn-images-1.medium.com/max/1024/1*qkdJUQYdd0AAn2gTVB5xcg.png)

#### **Dependency**

Add _mavenCentral_ repository

```
repositories {
    //...
    mavenCentral()
    //...    
}
```

Next, we will configure the Gradle file for :shared:konsisttest.

1. Remove the Android Gradle plugin from the _:shared:konsisttest_ module’s Gradle, along with its corresponding ‘_android()_’ Kotlin Target
2. Add the ‘_jvm()_’ Kotlin Target and ‘_jvmTest_’ source set.
3. Add the Konsist dependency to ‘_jvmTest_’ in the _:shared:konsisttest_ module’s Gradle.

```
val jvmTest by getting {
    dependencies {
        implementation("com.lemonappdev:konsist:{VERSION}")
    }
 }
```

{% gist https://gist.github.com/stargatex/3babef44cce94b4792975edf14c6d4d6.js %}

Following these steps, create a file in your ‘jvmTest’ source set that will be used to write test methods.

1. Select _New_ then _Directory_ from the right-click menu on the module (_:shared:konsisttest_)
2. Create the source set directory (_src/jvmTest/kotlin_)
3. Create your package (Ex: _com.stargatex.mt.lahiru.konsisttest_)
4. Create a file (Ex: _AppKonsistTest_)

![](https://cdn-images-1.medium.com/max/1024/1*VeZKzj8iNKWUiK6y0yOfrA.png)
_Directory creation_

![](https://cdn-images-1.medium.com/max/1024/1*HsVQ1bmGsebmMVaXX8goSA.png)
_jvmTest source set directory_

![](https://cdn-images-1.medium.com/max/1024/1*W3pPL-vlRPAXP_Gs8JHEtQ.png)
_Package creation_

![](https://cdn-images-1.medium.com/max/1024/1*8UZWG0QuTqEkO58Krbyk9g.png)
_Test file creation_

#### Implementation

First let’s take a look at the steps for ‘_declaration check_’.

- As a first step, we need to determine the scope, which includes files from an entire project, a module, a package, or just one Kotlin file. ([_Create the scope_](https://docs.konsist.lemonappdev.com/writing-tests/koscope)_)_

```
// Scope for Kotlin files availble in the project
Konsist.scopeFromProject() 
// Scope for Kotlin files availble in the 'app' module
Konsist.scopeFromModule("app")
// Scope for Kotlin files availble in the 'test' source set
Konsist.scopeFromSourceSet("test")
// Scope for Kotlin files availble for production code
Konsist.scopeFromProduction()

```

- We then query all items in the scope and filter them according to our needs. ([_Query and Filter_](https://docs.konsist.lemonappdev.com/writing-tests/declaration-query-and-filter))
- Finally, we define the assertion. ([_Assert_](https://docs.konsist.lemonappdev.com/writing-tests/declaration-assert))

![](https://cdn-images-1.medium.com/max/1024/1*Pe5WXu8I_DxTN2DZSYAHVQ.png)

Now let’s examine the steps for ‘_architecture check_’.

- As a first step, we need to determine the scope.
- Next, we define layers of the project and assertions using the ‘_assertArchiteture_’ method

```
Konsist
    .scopeFromProduction() // Define the scope
    .assertArchitecture { // Assert architecture
        // Define layers
        val domain = Layer("Domain", "com.stargatex.mt.kmm.lahiru.shared.domain..")
      
        // Define the relations between each layer for architecture assertions
        domain.dependsOnNothing()
    }
```

![](https://cdn-images-1.medium.com/max/1024/1*Bm57jsFLq8kKAPiBajnUzQ.png)

Lastly, both ‘_declaration check_’ and ‘_architecture check_’ of Konsist test code will be executed as unit tests.

{% gist https://gist.github.com/stargatex/3fd825b968445c7270c6c2fb02c10c28.js %}

#### Use cases

Here are some examples of use cases you might need to test in a project.

{% gist https://gist.github.com/stargatex/e4656dab531d8da891296bff008a155d %}

{% gist https://gist.github.com/stargatex/41ab760758c29a81005757f645af0621 %}

{% gist https://gist.github.com/stargatex/6bfb923ba0cd7cfb660a5bcf76a08094 %}

For more examples, They have provided good examples that you can use as inspiration on how this can be used ([Snippets](https://docs.konsist.lemonappdev.com/inspiration/snippets) /  [Articles](https://docs.konsist.lemonappdev.com/getting-started/getting-started/articles)).

All right, that’s it. Using this library correctly could mean you don’t accumulate technical debt resulting from architectural violations and those violations might be found in reviews rather than going unnoticed. I have begun integrating this library into my current organization’s Kotlin Android and Kotlin Multiplatform Mobile projects. It was a pleasant experience thanks to good documentation, seamless integration abilities, and extensibility for custom needs.

I hope you found this useful. Until next time 🙏 👋