"Module was compiled with an incompatible version of Kotlin." - the case with "build.gradle.kts" file - kotlin

My IntelliJ IDEA plugin based on official Kotlin template. I had no Kotlin experience until tried to develop the plugin for IntelliJ IDEA.
I got the error:
C:/Users/I/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2022.2/42c296374014a649785bb84aa6d8dc2d18f2ca0e/ideaIC-2022.2/lib/3rd-party-rt.jar!/META-INF/kotlinx-datetime.kotlin_module:
Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1.
when updated some options in gradle.properties:
pluginGroup = com.yamatodaiwa
pluginName = Yamato-Daiwa Frontend
pluginVersion = 0.0.3
pluginSinceBuild = 221
pluginUntilBuild = 222.*
pluginVerifierIdeVersions = 2022.1, 2022.2
platformType = IC
platformVersion = 2022.2
platformDownloadSources = true
javaVersion = 11
gradleVersion = 7.2
kotlin.stdlib.default.dependency = false
I checked the similar topics, but the solutions like
ext.kotlin_version = '1.6.10'
are not actual for the projects with build.gradle.kts (not build.gradle) - there is no ext.kotlin_version option:
import org.jetbrains.changelog.markdownToHTML
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
fun properties(key: String) = project.findProperty(key).toString()
plugins {
// Java support
id("java")
// Kotlin support
id("org.jetbrains.kotlin.jvm") version "1.5.30"
// Gradle IntelliJ Plugin
id("org.jetbrains.intellij") version "1.1.6"
// Gradle Changelog Plugin
id("org.jetbrains.changelog") version "1.3.0"
// Gradle Qodana Plugin
id("org.jetbrains.qodana") version "0.1.12"
}
group = properties("pluginGroup")
version = properties("pluginVersion")
...
📄 Full code
If you need to check ano other files to answer, please check the plugin's repository.

Solution: upgrade all the used Gradle plugins to their latest versions.

Related

Specify kotlin version in buildSrc kotlin convention plugin

I am developing a Kotlin project with multiple subprojects as explained here
Building Kotlin Applications with libraries Sample (gradle.org)
Is it correct to specify the kotlin version as as shown below?
file: buildSrc/build.gradle.kts
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:<kotlin-version>")
}
Also, is it possible to move this kotlin version to a central location for declaring project-wide dependency version (for example gradle.properties or buildSrc/**/Dependencies.kt or versions catalog)? None of the approaches mentioned seems to be supported in buildSrc/build.gradle.kts.
There are many approaches to specify the kotlin version (or versions of any dependency) at a centralized location in a Gradle Multimodule Project.
gradle.properties
buildSrc/**/Dependency.kt
versions catalog
However, if you are defining a Kotlin Convention Plugin in buildSrc, the kotlin version is not available in buildSrc/build.gradle.kts using any of the methods mentioned above so as to pick the right kotlin-gradle-plugin.
For a workaround solution, use the following.
# file:<root>/gradle.properties
kotlinVersion=1.5.31
// file:<root>/buildSrc/build.gradle.kts
import java.io.*
import java.util.*
// read gradle.properties programmatically
val props = Properties()
FileInputStream(file("../gradle.properties")).use {
props.load(it)
}
plugins {
`kotlin-dsl`
}
repositories {
gradlePluginPortal()
}
dependencies {
val kotlinVersion = props.getProperty("kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
}
In all other <root>/**/<subproject>/build.gradle.kts files access the kotlinVersion like
val kotlinVersion = project.properties["kotlinVersion"]
In settings.gradle.kts, access the kotlinVersion like
val kotlinVersion:String by settings
If anyone from the Gradle team reading this, please provide an API to access gradle.properties consistently across any of the build script files.
in app gradle
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
and project gradle
ext.kotlin_version = "1.6.0"
I have solved this issue by specifying kotlinVersion in gradle.properties file as below.
kotlinVersion=1.6.10
Keep gradle.properties file at outermost project level.
You can access kotlinVersion in any submodules and project as
//top level declaration
val kotlinVersion by extra(project.property("kotlinVersion"))
//usage in dependency block
"org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
To verify whether you are able to read it, you can specify following
println(project.property("kotlinVersion"))
This should print kotlin version as
> Configure project :
1.6.10
build.gradle.kts file
val kotlinVersion by extra(project.property("kotlinVersion"))
println("${kotlinVersion}")
group = "com.company"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
mavenLocal()
maven { url = uri("https://repo.spring.io/milestone") }
maven { url = uri("https://repo.spring.io/snapshot") }
gradlePluginPortal()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
}
gradle.properties file
kotlin.code.style=official
#kotlin.parallel.tasks.in.project=true
#kapt.incremental.apt=true
org.gradle.parallel=true
org.gradle.caching=false
org.gradle.daemon=true
org.gradle.jvmargs=-Dfile.encoding=UTF-8
kotlinVersion=1.6.10
Ref:
project_properties
extra_properties

Kotlin QueryDsl not generating Q classes

I'm using kts gradle with koltin. However when i try to add QueryDsl it's not genetation Q classes from my JPA entities.
My build.gradle.kts looks like:
plugins {
id("org.springframework.boot") version "2.3.1.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
kotlin("jvm") version "1.3.72"
kotlin("plugin.spring") version "1.3.72"
kotlin("plugin.jpa") version "1.3.72"
kotlin("kapt") version "1.4.0"
kotlin("plugin.allopen") version "1.4.10"
}
dependencies {
// some spring boot dependencies here...
implementation("com.querydsl:querydsl-jpa:4.2.1")
kapt("com.querydsl:querydsl-apt:4.2.1:general")
}
kapt {
annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")
}
I suggest it should build Q classes to build/generated/source/kapt/main. Any ideas why it's not working?
Also tried to annotate my entity with #QueryEntity annotation. Not working as well
Issue fixed by adding next configuration
// quert dsl
implementation("com.querydsl:querydsl-jpa:4.2.1")
kapt("com.querydsl:querydsl-apt:4.2.2:jpa")
annotationProcessor(group = "com.querydsl", name = "querydsl-apt", classifier = "jpa")
Also i removed
annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")

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")

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

How to ignore explicitly importing classes like gradle kotlin script when writing custom kotlin-dsl

In the gradle's kotlin build script, we don't need explicitly import classes or functions like plugins, repositories or dependencies in build script build.gradle.kts.
plugins {
val kotlinVersion = "1.3.10"
val springBootVersion = "2.1.0.RELEASE"
val detektVersion = "1.0.0-RC10"
id("org.springframework.boot") version springBootVersion
id("org.jetbrains.kotlin.jvm") version kotlinVersion
id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
id("io.spring.dependency-management") version "1.0.6.RELEASE"
id("io.gitlab.arturbosch.detekt") version detektVersion
}
repositories {
mavenLocal()
mavenCentral()
maven(url = uri("https://dl.bintray.com/s1m0nw1/KtsRunner"))
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-script-runtime")
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
implementation("org.jetbrains.kotlin:kotlin-script-util")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("cn.pilipa:pilipa-spring-boot-starter-logging:2.0.10")
implementation("de.swirtz:ktsRunner:0.0.5")
testImplementation("org.springframework.boot:spring-boot-starter-test"){
exclude(module = "junit")
}
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.cloud:spring-cloud-stream-test-support")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntime("org.junit.jupiter:junit-jupiter-engine")
testCompile("io.kotlintest:kotlintest-runner-junit5:${kotlinTestVersion}")
testCompile("io.kotlintest:kotlintest-extensions-spring:${kotlinTestVersion}")
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:${detektVersion}")
}
How to implement this similar feature in the custom kotlin-dsl script to implicitly import classes in kotlin-dsl script?
Gradle defines a list of implicit imports that has no mechanism for extending this list. This is the same as for build.gradle and Groovy versions as it is for the Kotlin versions.
See also: Automatic imports in Gradle plugin
Which still holds true as of today. There is a TODO remaining in the Kotlin Gradle Script source code (master branch as-of Nov 22, 2018) related to this:
// TODO: let this be contributed by :plugins