Writing Documentation for a Kotlin Project: From A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why is Documentation the Key to a Kotlin Project's Success?

In the world of modern Kotlin development, writing clean and efficient code is only half the battle. The second, equally important part is creating high-quality documentation. A well-documented project is not just a set of comments. It is an investment in the future: it simplifies onboarding for new developers, speeds up debugging, eases codebase maintenance, and makes your API understandable to others. Without documentation, even the most brilliant code risks becoming a "black box" that no one wants to touch.

In this article, we will break down the complete process of writing documentation for Kotlin projects — from the basics of KDoc syntax to automatically generating beautiful websites using Dokka. You will learn how to make documentation not just useful, but also readable, and you will get acquainted with best practices that will help you keep it up to date.



1. KDoc Basics: How to Write Comments in Kotlin

KDoc is the standard markup language for documenting Kotlin code, in many ways similar to JavaDoc. KDoc comments start with /** and end with */. Special markup is used inside to describe parameters, return values, and exceptions.



1.1. Basic Structure of a KDoc Comment

The main tags you will use most often:

  • @param — describes a function or class parameter.
  • @return — describes the return value.
  • @throws or @exception — describes exceptions that a function may throw.
  • @see — a link to another code element.
  • @sample — a link to a usage example.


1.2. Example for a Function

Consider a simple function that calculates the arithmetic mean:

/** * Calculates the arithmetic mean of two numbers. * * This function takes two integers and returns their average value * as a floating-point number. If both numbers are zero, the function * will return 0.0. * * @param a The first integer. * @param b The second integer. * @return The arithmetic mean of numbers a and b. * @throws IllegalArgumentException if both parameters are zero (optional, for the example). * @see Math#sqrt * @sample com.example.calculator.averageSample */fun average(a: Int, b: Int): Double {    if (a == 0 && b == 0) {        throw IllegalArgumentException("Both numbers cannot be zero")    }    return (a + b) / 2.0}


1.3. Documenting Classes and Properties

Not only functions but also classes, interfaces, and their properties should be documented. This helps to quickly understand the purpose of a module.

/** * Repository for working with users. * * This class encapsulates the logic for accessing user data, * including search, save, and delete operations. * * @property databaseSource The data source for the database. * @property cacheSource The data source for the cache. * @constructor Creates an instance of the repository with the specified data sources. */class UserRepository(    private val databaseSource: UserDatabaseSource,    private val cacheSource: UserCacheSource) {    /**     * The ID of the current user.     * Can be null if the user is not authenticated.     */    var currentUserId: String? = null}


2. Generation Tools: Dokka and KDoc

Writing comments manually is only half the job. To turn them into convenient documentation (for example, an HTML site or PDF), you need a generation tool. For Kotlin, the standard is Dokka — a powerful engine developed by JetBrains.



2.1. What is Dokka?

Dokka is a documentation generator that understands KDoc and can create documentation in various formats:

Blogs

Book Recommendations