4

I want to use the new Vector API from a Kotlin project using a Gradle build.

I tried this:

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "17"
    kotlinOptions.freeCompilerArgs = listOf("--add-modules", "jdk.incubator.vector")
}

Result is:

Invalid argument: --add-modules

I've checked and the right SDK is being used, and help for javac at least shows --add-modules as a valid flag, so what I guess is going on is, this is the Kotlin compiler, and I need to provide a different flag, or some kind of prefixed flag to add a module to the classpath. What is the magic incantation here?

Hakanai
  • 12,010
  • 10
  • 62
  • 132
  • `--add-modules` might be a valid `javac` flag, but these `freeCompilerArgs` are indeed for the `kotlinc` compiler. That said, I don't know of a way to add modules like this (and I don't see anything in [Kotlin compiler options](https://kotlinlang.org/docs/compiler-reference.html#kotlin-jvm-compiler-options)). But I never personally looked too much into Java modules tbh, maybe someone else can help here. – Joffrey Dec 17 '21 at 11:54

1 Answers1

2

The Kotlin compiler's argument for this is named -Xadd-modules. You can pass it in this way:

kotlinOptions.freeCompilerArgs = listOf("-Xadd-modules=jdk.incubator.vector")

(Note that right now there is a bug KT-54288 which incorrectly makes this compiler argument unnecessary. So until it's fixed, you can get away without adding it to the build.)

Alexander Udalov
  • 31,429
  • 6
  • 80
  • 66