Let’s develop the simplest of libraries with just one API class and method.

This is the conventional layout for a Kotlin project. Luckily, Gradle is flexible and would allow us to easily change it by adjusting the sourceSets {} . We won’t worry about that today, though.

Kotlin project structure my-kotlin-library ├── build.gradle.kts └── src ├── main │ └── kotlin │ └── org │ └── example │ └── MyLibrary.kt └── test └── kotlin └── org └── example └── MyLibraryTest.kt

The first step is to create a folder for the new project and change directory into it. In a *nix shell this would be:

$ mkdir -p my-kotlin-library/src/main/kotlin/org/example $ cd my-kotlin-library

In our new directory we add our library class called MyLibrary that returns a value object Language .

src/main/kotlin/org/example/MyLibrary.kt package org.example /** * The `Language` type defines a programming language with a name and hotness score. * * @property name The name of the language. * @property hotness A score from 1 to 10 of user enthusiasm. 10 = so hot right now */ data class Language(val name: String, val hotness: Int) class MyLibrary { /** * @return data relating to the Kotlin {@code Language}. */ fun kotlinLanguage() = Language("Kotlin", 10) }

With the following simple build script we can compile our new Kotlin library.

build.gradle.kts plugins { kotlin("jvm") version "1.3.61" (1) } repositories { jcenter() (2) } dependencies { implementation(kotlin("stdlib")) (3) }

1 Apply the Kotlin plugin, targeting the Java VM 2 Declare a dependency repository on Bintray jcenter 3 Declare a dependency on the Kotlin standard library

We use the implementation dependency configuration so that we don’t expose any of the Kotlin standard library through our API. This has architectural and performance benefits.