Skip to content

Kotlin Support

Erik C. Thauvin edited this page Aug 31, 2024 · 21 revisions

Kotlin is supported via Extensions.

Be sure to satisfy the Kotlin compiler requirement.

TL;DR

The quickest way to create a bld project with Kotlin support is by using the Kotlin Example Project for bld template on GitHub.

Screenshot from 2024-01-21 08-41-55

The template has built-in support for Kotlin, Dokka and Detekt.

Manual Steps

Create a New Project

To create a new project:

bld create-lib

You'll be asked for some configuration options, use something like:

bld create-lib
Please enter a package name (for instance: com.example):
com.example
Please enter a project name (for instance: myapp):
MyKotlinExample
Downloading finished successfully.
The project was successfully created at '/tmp/MyKotlinExample'.

Install the Kotlin Extension

First, go to the new project directory:

cd MyKotlinExample

Then, edit the lib/bld/bld-wrapper.properties file by changing:

bld.extensions=
bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES

to:

bld.extensions=com.uwyn.rife2:bld-kotlin:1.0.1
bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES,RIFE2_SNAPSHOTS

And finally, download the extension and dependencies:

./bld download

Configure the Project

Create the Source Directories

mkdir src/{main,test}/kotlin

Configure the Build File

Open the project in IntelliJ IDEA (or Visual Studio Code)

Make sure the compile command is using the extension by adding the following to the src/bld/java/com/example/MyKotlinExampleBuild.java build file, right before the main method:

@BuildCommand(summary = "Compiles the Kotlin project")
@Override
public void compile() throws IOException {
   // The source code located in src/main/kotlin and src/test/kotlin will be compiled
   new CompileKotlinOperation()
           .fromProject(this)
           .execute();
}

Configure Kotlin

Include the standard repositories and Kotlin standard library dependency within the build file ExampleBuild method:

repositories = List.of(MAVEN_CENTRAL, RIFE2_RELEASES);

final var kotlin = version(2, 0, 20);
scope(compile).include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", kotlin));

and download again:

Screenshot from 2024-01-19 11-26-08

Configure IntelliJ IDEA Modules

In IDEA, select File > Project Structure > Project Settings > Modules and add the src/main/kotlin and src/test/kotlin directories:

Screenshot from 2024-01-19 00-06-45 Screenshot from 2024-01-19 00-07-14

Done!

You are done. The project will compile everything located in the src/main/kotlin and src/test/kotlin directories.

Open the terminal pane and type:

Screenshot from 2024-01-19 00-44-38

Convert the Example Sources (IntelliJ IDEA, Optional)

For example, to convert the main example source to Kotlin:

  1. Create a MyKotlinExampleLib.kt file in src/main/kotlin
  2. Copy the content of the src/main/java/com/example/MyKotlinExampleLib.javaand paste in the new Kotlin file
  3. IDEA will ask you to convert it, do so.
  4. Delete the Java file that was just converted.

The same process can be used to copy the test example source.

Do not use the IDEA option to convert a class to Kotlin, as it will mess up the project.

Using JUnit (Optional)

In order to use JUnit for tests, remove the following line from the Build file:

testOperation().mainClass("com.example.MyKotlinExampleTest");

and add the JUnit test dependencies:

scope(test)
    .include(dependency("org.jetbrains.kotlin", "kotlin-test-junit5", kotlin))
    .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 11, 0)))
    .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 11, 0)));

Next learn more about JDK Tools