-
Notifications
You must be signed in to change notification settings - Fork 13
Kotlin Support
Kotlin is supported via Extensions.
Be sure to satisfy the Kotlin compiler requirement.
The quickest way to create a bld project with Kotlin support is by using the Kotlin Example Project for bld template on GitHub.
The template has built-in support for Kotlin, Dokka and Detekt.
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
mkdir src/{main,test}/kotlin
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();
}
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:
In IDEA, select File > Project Structure > Project Settings > Modules
and add the src/main/kotlin
and src/test/kotlin
directories:
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:
For example, to convert the main example source to Kotlin:
- Create a
MyKotlinExampleLib.kt
file insrc/main/kotlin
- Copy the content of the
src/main/java/com/example/MyKotlinExampleLib.java
and paste in the new Kotlin file - IDEA will ask you to convert it, do so.
- 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