Skip to content

Kotlin Support

Erik C. Thauvin edited this page Jan 21, 2024 · 23 revisions

Kotlin Support

Kotlin is supported via Extensions.

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 by changing:

bld.extensions=
bld.repositories=MAVEN_CENTRAL,RIFE2_RELEASES

to:

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

And also, 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 editing 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_LOCAL, MAVEN_CENTRAL, RIFE2_RELEASES);

final var kotlin = version(1, 9, 22);
scope(compile).include(dependency("org.jetbrains.kotlin", "kotlin-stdlib", kotlin));

and download again:

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

Configure 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 (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, 10, 1)))
        .include(dependency("org.junit.platform", "junit-platform-console-standalone", version(1, 10, 1)));

Next learn more about Examples

Clone this wiki locally