How to combine Scala with Kotlin using Gradle?

Hello,

I recently became interested in Scala because the PEG parser library Parboiled runs significantly faster in Scala than in Kotlin.

I’m currently developing a plugin for JetBrains IDEs using Kotlin and Gradle. I’d like to include some Scala code alongside my main Kotlin code. Here is my build.gradle.kts:

plugins {
    kotlin("jvm") version "2.1.20"
    scala
}

group = "io.parstools"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.parboiled:parboiled_2.13:2.1.8")
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<ScalaCompile> {
    scalaCompileOptions.apply {
        additionalParameters = listOf("-deprecation", "-feature", "-unchecked")
    }
}

kotlin {
    jvmToolchain(17)
}

Even though I’m not explicitly setting any -target:jvm-XX flag, I get the following error when compiling a simple Scala function (not even using Parboiled yet):

Task :compileScala FAILED
2 actionable tasks: 2 executed
'jvm-1.17' is not a valid choice for '-target'
bad option: '-target:jvm-1.17'

What might be causing this error? How can I ensure Kotlin and Scala sources can coexist and compile cleanly in one Gradle project?

Any guidance or best practices for setting up a mixed Kotlin + Scala Gradle project would be appreciated.

Correct -target setting for Scala 2 would be -target:17, see scalac --help / scala -S 2.13 -O -help would print.

  -target:<target>             Target platform for object files. ([8],9,10,11,12,13,14,15,16,17,18,19,20,21)

-target is also an alias to -Xunchecked-java-output-version and -Xtarget in Scala 3

There also exists -java-output-version which is an alias to -release which behaves as javac -release

That’s also how I can help here, unfortuentlly I have no experience of Scala in Gradle
I can see some table on Gradle website related to -target flags, maybe check if you’re using up-to-date version of Gradle/Scala plugin

2 Likes

I would maybe try to add two subprojects/modules, one for the kotlin code and one for the scala code. And hope that you don’t require a bidirectional dependency between the kotlin and scala code.