"Unresolved reference: sourceCompatibility" after upgrading Gradle build to Kotlin 1.7.0 - kotlin

Following some answers at Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 I have a Kotlin Gradle DSL script containing
tasks.compileKotlin {
sourceCompatibility = JavaVersion.VERSION_11.toString()
targetCompatibility = JavaVersion.VERSION_11.toString()
kotlinOptions {
jvmTarget = "11"
}
}
but after upgrading to Kotlin 1.7.0 I get the exception
Unresolved reference: sourceCompatibility
Unresolved reference: targetCompatibility
It is clear to me they have apparently removed these, as I found it listed at https://kotlinlang.org/docs/whatsnew17.html#changes-in-compile-tasks
My question is, what do I replace it with? How should I ensure to keep compatibility?

Setting the JVM target, and the Kotlin API and Language versions can be done by configuring all KotlinCompile tasks.
// build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.0"
}
// configure all Kotlin compilation tasks,
// using the Gradle configuration avoidance API
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = "11"
apiVersion = "1.6"
languageVersion = "1.6"
}
// you can also add additional compiler args,
// like opting in to experimental features
kotlinOptions.freeCompilerArgs += listOf(
"-opt-in=kotlin.RequiresOptIn",
)
}
apiVersion restricts the use of declarations to those from the specified version of bundled libraries
languageVersion means the compiled code will be compatible with the specified version of Kotlin
All the compiler options are documented here. There is also additional documentation for other build tools, like Maven and Ant.
You can use the new Toolchains feature to set the version of Java that will be used to compile the project.
// build.gradle.kts
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of("11"))
}
}
Read more: Gradle Java toolchains support

Related

Kotlin Gradle include runtime when building jar

I'm trying to include the kotlin runtime when building my jar with gradle. I cannot find an update to date thread anywhere online that shows how to do this.
Currently I have tried the following
plugins {
kotlin("jvm") version "1.5.10"
}
repositories {
mavenCentral()
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + "-include-runtime"
jvmTarget = "16"
}
}
This builds the jar fine but doesn't include the runtime like I would expect. How can I configure gradle to do this?
You can use the shadowJar plugin. Add
id("com.github.johnrengelman.shadow") version "7.0.0"
To your plugins, then run ./gradlew shadowJar to create a jar file in build/lib containing the kotlin runtime as well as any of your runtime dependencies

IntelliJ + Kotlin + Gradle: How can I correct this error "Language version 1.5 is experimental"?

I have a project that is using Kotlin 1.5 with Gradle 7 and IntelliJ 2021.1.1. The project was originally based on Kotlin 1.4 and was recently upgraded to 1.5.
When I build the project, I see the following warning, which seems to be related to the compileTestKotlin Gradle task. There are no warnings for non-test compiles. I have scoured through IntelliJ settings, Gradle configuration files, but to no avail. I'd appreciate any help to resolve this warning... Thank you for any help!
Task :services:track:compileTestKotlin
w: Language version 1.5 is experimental, there are no backwards compatibility guarantees for new language and library features
Here are my compiler settings in Gradle:
tasks {
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
//allWarningsAsErrors = true // best practice
apiVersion = "1.5"
freeCompilerArgs += listOf("-Xjsr305=strict")
jvmTarget = "11"
languageVersion = "1.5"
}
}
withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>> {
kotlinOptions {
//allWarningsAsErrors = true // best practice
apiVersion = "1.5"
languageVersion = "1.5"
}
}
}
IntelliJ Kotlin Configuration
The issue has been corrected. The buildSrc folder contained a build.gradle.kts file that had Kotlin 1.4 dependencies. I've changed them to 1.5, and the project builds properly.

Kotlin + Gradle for JVM New Module Error in IntelliJ

My project is composed of Kotlin/JVM + Gradle
I'm trying to add a module
After creating the module it gives me a single files:
|--main-project
|----new-module
|------build.gradle.kts
|--<other-main-project-files>
module gradle:
plugins {
kotlin("jvm") version "1.4.32"
}
group = "com.example"
version = "0.0.1"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
}
It gives me an error Unresolved reference implementation and I can't create kotlin/java files
I have no issue if I use java instead of kotlin/jvm
I fixed the problem after going through Gradle logs
kotlin("jvm") version "1.4.32"
should be changed to
kotlin("jvm")

How to set up Kotlin PSI for IntelliJ IDEA Gradle Plugin?

I'm building a Plugin for the IntelliJ IDE to manipulate Kotlin files in a project. I've been able to write a bunch of tests to take a Kotlin file and generate a new file based on its contents. When I run this plugin in the IDE I'm unable to detect files as Kotlin files. When looking at the debugger my file says that it is a KtFile from the org.jetbrains.kotlin.psi library. But if I try to cast it to a KtFile I get an error:
java.lang.ClassCastException: org.jetbrains.kotlin.psi.KtFile cannot be cast to org.jetbrains.kotlin.psi.KtFile
So apparently the library version is off between runtime and compile time. What do I have to do to configure my plugin to use the correct Kotlin PSI at plugin runtime?
My plugin.xml looks like this:
<idea-plugin>
<id>...</id>
<name>...</name>
<vendor email="..." url="...">...</vendor>
<description><...</description>
<depends>com.intellij.modules.all</depends>
<depends>org.jetbrains.kotlin</depends>
<actions>...</actions>
</idea-plugin>
My build.gradle.kts looks like:
plugins {
id("org.jetbrains.intellij") version "0.4.16"
kotlin("jvm") version "1.3.61"
}
group = "..."
version = "..."
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("compiler-embeddable", "1.3.61"))
implementation(kotlin("gradle-plugin", "1.3.61"))
testImplementation(group = "junit", name = "junit", version = "4.12")
}
buildscript {
repositories { mavenCentral() }
dependencies {
classpath(kotlin("compiler-embeddable", "1.3.61"))
classpath(kotlin("gradle-plugin", "1.3.61"))
}
}
intellij {
version = "2019.1.4"
setPlugins("Kotlin")
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
}
tasks.getByName<org.jetbrains.intellij.tasks.PatchPluginXmlTask>("patchPluginXml") {
changeNotes("...")
}
I already am aware of How to include Kotlin PSI classes (e.g. KtClass) in Intellij IDEA Gradle plugin project written in Kotlin? and How to add Kotlin PSI source files to IDEA Plugin project configuration which is essentially what I want to be answered but haven't gotten anything to fix my issue. Maybe there is some documentation on this issue but it evades my searches.
Your dependencies should include implementation(kotlin("reflect")) and plugin.xml should include <depends>org.jetbrains.kotlin</depends>

TornadoFX unresolved JavaFx

I wanted to create a new project that should be a desktop application. For this purpose, I have selected Kotlin language and TornadoFX framework. I have installed the TornadoFXplugin and created a new Ttornadofx-gradle-project. The base setup made by Intellij was successful but I have encountered a problem. When I wanted to run the generated project it failed. The project cannot resolve the java fx. I have dug through the web and found nothing that would fix the problem. The error log that I receive after the failed build is:
HAs anyone faces the same issue? How can I get rid of it?
I have installed the JDK 11 and set it up to the build config and I still receive the problem:
java.lang.UnsupportedClassVersionError: org/openjfx/gradle/JavaFXPlugin has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Is there a change that I have missed something in the middle?
It looks like you are running the TornadoFX project with Java 11 or 12.
It also looks like the TornadoFX plugin is intended for Java 1.8, but it is not advised what to do with Java 11+.
Since Java 11, JavaFX is no longer part of the JDK.
You can read all about getting JavaFX as a third party dependency into your project here: https://openjfx.io/openjfx-docs/, and since you are using Gradle, this section will be helpful: https://openjfx.io/openjfx-docs/#gradle.
I've just installed the Tornado plugin, and created a project, using JDK 12.0.1. I've also updated the gradle-wrapper.properties file to use Gradle 5.3-bin as the default 4.4 doesn't work with Java 11+.
If I run it, I get the same errors:
e: /.../src/main/kotlin/com/example/demo/app/Styles.kt: (3, 8): \
Unresolved reference: javafx
e: /.../src/main/kotlin/com/example/demo/app/Styles.kt: (18, 13): \
Cannot access class 'javafx.scene.text.FontWeight'. Check your module classpath for missing or conflicting dependencies
...
Basically these errors indicate that JavaFX is not found. The Tornado plugin wasn't expecting this.
Solution
There is an easy solution to make this work: add the JavaFX gradle plugin to the build, so it deals with the JavaFX part.
According to the plugin's repository, all you need to do is edit the build.gradle file and add:
buildscript {
ext.kotlin_version = "1.2.60"
ext.tornadofx_version = "1.7.17"
ext.junit_version = "5.1.0"
repositories {
mavenLocal()
mavenCentral()
maven {
setUrl("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.0"
// Add JavaFX plugin:
classpath 'org.openjfx:javafx-plugin:0.0.7'
}
}
apply plugin: "kotlin"
apply plugin: "application"
apply plugin: "org.junit.platform.gradle.plugin"
// Apply JavaFX plugin:
apply plugin: 'org.openjfx.javafxplugin'
// Add the JavaFX version and required modules:
javafx {
version = "12.0.1"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
...
And this is it, refresh your project, the IDE should recognize all the JavaFX classes.
If you modify the default MainView.kt like:
class MainView : View("Hello TornadoFX \n with JavaFX "
+ System.getProperty("javafx.version")) {
override val root = hbox {
label(title) {
addClass(Styles.heading)
}
}
}
you should be able to run it:
This answer is for those who wish to use Gradle Kotlin DSL.
An example of minimal build.gradle.kts:
plugins {
kotlin("jvm") version "1.4.0-rc"
application
id("org.openjfx.javafxplugin") version "0.0.9"
}
application { mainClassName = "com.example.MyApp" }
repositories {
mavenCentral()
jcenter()
maven("https://dl.bintray.com/kotlin/kotlin-eap")
}
dependencies {
// Kotlin standard library
implementation(kotlin("stdlib-jdk8"))
// TornadoFX dependency
implementation("no.tornado:tornadofx:1.7.20")
}
// JavaJX module to include
javafx { modules = listOf("javafx.controls", "javafx.fxml", "javafx.graphics") }
// Set Kotlin/JVM target versions
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "11" // or higher
kotlinOptions.languageVersion = "1.4"
}
// Be sure to use lates Gradle version
tasks.named<Wrapper>("wrapper") { gradleVersion = "6.6" }
For a full working example, check out GitHub repository
Please note that it also works with JDK 13 and 14
i'm recieved this error when download Kodein-Samples and trying to run tornadofx sample under Java11/12 and JavaFX13.
java.lang.UnsupportedClassVersionError: org/openjfx/gradle/JavaFXPlugin has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
The solution was is quite simple: i'm only comment another modules in settings.gradle (because the error occurred in some other module). Unfortunately, after the launch the application generates an error when trying to edit the record. I haven't dealt with it yet.
so my build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val kodeinVersion: String by rootProject.extra
plugins {
kotlin("jvm")
application
id("org.openjfx.javafxplugin") version "0.0.8"
}
repositories {
jcenter()
maven(url = "https://dl.bintray.com/kodein-framework/Kodein-DI/")
}
application {
mainClassName = "org.kodein.samples.di.tornadofx.KodeinApplication"
}
javafx {
version = "13"
modules = mutableListOf("javafx.controls", "javafx.fxml", "javafx.base", "javafx.media")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("no.tornado:tornadofx:1.7.19")
implementation("org.kodein.di:kodein-di-generic-jvm:$kodeinVersion")
implementation("org.kodein.di:kodein-di-conf:$kodeinVersion")
implementation("org.kodein.di:kodein-di-framework-tornadofx-jvm:$kodeinVersion")
}
i made fork for this example with changes: https://github.com/ibelozor/Kodein-Samples