'JacocoTaskExtension' does not exist with jdk11 , AGP 7.0.+ and ,Gradle 7.0.+ - testing

jacoco works like charm with AGP 4.0.1, gradle 6.+ , jdk1.8 but throw error "JacocoTaskExtension" not found with upgraded gradle version which is below.
Gradle 7.0.2
------------------------------------------------------------
Build time: 2021-05-14 12:02:31 UTC
Revision: 1ef1b260d39daacbf9357f9d8594a8a743e2152e
Kotlin: 1.4.31
Groovy: 3.0.7
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 11.0.12 (Oracle Corporation 11.0.12+8-LTS-237)
OS: Mac OS X 11.5 x86_64
Steps to reproduce :
I am not able to import JacocoTaskExtension in below kotlin class CodeCoveragePlugin
It's working fine with jkd 1.8 but not with jdk11
why am i using jdk11 ?
jdk11 is mandatory for agp 7.+ and gradle 7.+
My final goal : make it functional with agp7.+
package com.jitendra.sdk.android.gradle.quality.coverage
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.*
import org.gradle.testing.jacoco.plugins.JacocoPluginExtension
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension
#Suppress("UnstableApiUsage")
class CodeCoveragePlugin : Plugin<Project> {
override fun apply(project: Project) = project.run {
val extension = extensions.create("codeCoverage", CodeCoverageExtension::class)
apply(plugin = "jacoco")
jacoco {
toolVersion = 0.8.7
}
java { createJavaTasks(sourceSets["main"], extension) }
setupAndroidTasks(extension)
}
private fun Project.java(run: JavaPlugin.() -> Unit) = plugins.withType(JavaPlugin::class, run)
private val Project.sourceSets get() = convention.getPlugin(JavaPluginConvention::class).sourceSets
private fun Project.jacoco(run: JacocoPluginExtension.() -> Unit) = configure(run)
}

I have had a similar issue. Could you please check the listed stack trace from the console?
If it contains:
Unexpected SMAP line: *S KotlinDebug"
Then it's clear that you haven't used the correct version 0.8.7 of Jacoco, as discussed here. In my case, I still need to add the following configuration:
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "org.jacoco") {
useVersion("0.8.7")
}
}
}
You can put it inside your project.run closure. Hope it works. If not, please share your stack trace as part of your question.

Related

java.lang.NoSuchMethodError when installing plugin in ktor

I am trying to add Resources plugin into my ktor client using the following code:
...
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.java.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.resources.*
import io.ktor.client.request.*
...
...
val HTTP_CLIENT = HttpClient(Java) {
install(Resources)
}
...
And here's part of my build.gradle.kts:
val ktor_version: String by project
plugins {
val kotlinVersion = "1.6.10"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-java:$ktor_version")
implementation("io.ktor:ktor-client-resources:$ktor_version")
}
The compile is successful, but when running I got following error:
java.lang.NoSuchMethodError: 'void io.ktor.client.HttpClientConfig.install$default(io.ktor.client.HttpClientConfig, io.ktor.client.plugins.HttpClientPlugin, kotlin.jvm.functions.Function1, int, java.lang.Object)'
2022-04-16 13:28:45 W/stderr: at NMEBoot-1.1.mirai.jar//NoMathExpectation.NMEBoot.wolframAlpha.Conversation$Companion$HTTP_CLIENT$1.invoke(Conversation.kt:20)
2022-04-16 13:28:45 W/stderr: at NMEBoot-1.1.mirai.jar//NoMathExpectation.NMEBoot.wolframAlpha.Conversation$Companion$HTTP_CLIENT$1.invoke(Conversation.kt:19)
2022-04-16 13:28:45 W/stderr: at io.ktor.client.HttpClientKt.HttpClient(HttpClient.kt:41)
2022-04-16 13:28:45 W/stderr: at NMEBoot-1.1.mirai.jar//NoMathExpectation.NMEBoot.wolframAlpha.Conversation.<clinit>(Conversation.kt:19)
I checked the jar and the source code and they both did have install method, but their signature was different from the signature described in the error.
I don't know if it is my fault or something else. Could someone help me solve this problem?
Edit: Here's a simplified project: KtorInstallTest
This is a bug on the Mirai side.
Mirai Console uses Kotlin 1.6.0 and for some reasons forces all plugins to use the same version of Kotlin, causing this problem. We plan to update to 1.6.20 by Mirai 2.11.0.

Kotlin native platform posix compilation without gradle

I am trying to compile a basic Kotlin/Native program with just the kotlinc-native CLI compiler rather than a full Gradle build. However, I cannot get the platform.posix library to work, despite compiling on linux for linux.
The source code I am using is:
import platform.posix.exit
fun main() {
println("Should exit with status 10")
exit(10)
}
When compiled with the following build.gradle.kts it works fine:
plugins {
kotlin("multiplatform") version "1.6.10"
}
repositories {
mavenCentral()
}
kotlin {
linuxX64("native") {
binaries {
executable()
}
}
}
However, when compiled from the command-line with:
kotlinc-native test.kt
I get:
test.kt:1:8: error: unresolved reference: platform
import platform.posix.exit
^
test.kt:4:5: error: unresolved reference: exit
exit(10)
^
Anyone know how to get Kotlin/Native compilation working fully without using Gradle? Or is there some magic that Gradle does that makes it work?
Thanks.

Kotlin/Native compileKotlinIosX64 task fails when building iOS app

I have a Kotlin Multiplatform project. I recently updated to Kotlin 1.4-M2 (I need it to solve some issues with Ktor).
After updating all the required libraries, resolving all gradle issues and having my Android project compile successfully, I now encounter the following error when building the iOS app:
Task :shared:compileKotlinIosX64
e: Compilation failed: Could not find declaration for unbound symbol org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionPublicSymbolImpl#56f11f08
* Source files: [all shared folder kt files]
* Compiler version info: Konan: 1.4-M2 / Kotlin: 1.4.0
* Output kind: LIBRARY
e: java.lang.IllegalStateException: Could not find declaration for unbound symbol org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionPublicSymbolImpl#56f11f08
at org.jetbrains.kotlin.ir.util.ExternalDependenciesGeneratorKt.getDeclaration(ExternalDependenciesGenerator.kt:76)
The curious thing is that in Source files it shows all the files in the shared code folder. I checked and absolutely all kt files appear in there. So my guess is that it is some issue when building the shared code, but does not seem specific of any library.
This is a slightly reduced version of how my build.gradle.kts looks like:
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("kotlinx-serialization")
id("com.android.library")
id("io.fabric")
}
// CocoaPods requires the podspec to have a version.
version = "1.0"
tasks {
withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
kotlin {
ios()
android()
cocoapods {
// Configure fields required by CocoaPods.
summary = "Some description for a Kotlin/Native module"
homepage = "Link to a Kotlin/Native module homepage"
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationVersion")
api("org.kodein.di:kodein-di:7.1.0-kotlin-1.4-M3-84")
implementation("io.mockk:mockk:1.9.2")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
api("com.russhwolf:multiplatform-settings:$multiplatformSettingsVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-json:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
}
}
}
}
And the library versions are as follows:
val ktorVersion = "1.3.2-1.4-M2"
val kotlinVersion = "1.4-M2"
val coroutinesVersion = "1.3.7-native-mt-1.4-M2"
val serializationVersion = "0.20.0-1.4-M2"
val multiplatformSettingsVersion = "0.6-1.4-M2"
It's worth mentioning this was building correctly in iOS when using 1.3.72.
As #KevinGalligan suggested, I updated Kotlin and all related libs to 1.4.0-rc and the problem was solved.
The root issue with 1.4-M2 remains unknown.

Runing coroutines in IntelliJ Kotlinx error

I'm trying to run a native Kotlin project using coroutines using IntelliJ IDEA Community 2020.
Here is how my build.gradle looks:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}
repositories {
mavenCentral()
}
kotlin {
// For ARM, should be changed to iosArm32 or iosArm64
// For Linux, should be changed to e.g. linuxX64
// For MacOS, should be changed to e.g. macosX64
// For Windows, should be changed to e.g. mingwX64
mingwX64("mingw") {
binaries {
executable {
// Change to specify fully qualified name of your application's entry point:
entryPoint = 'sample.main'
// Specify command-line arguments, if necessary:
runTask?.args('')
}
}
}
sourceSets {
// Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
// in gradle.properties file and re-import your project in IDE.
mingwMain {
}
mingwTest {
}
}
}
// Use the following Gradle tasks to run your application:
// :runReleaseExecutableMingw - without debug symbols
// :runDebugExecutableMingw - with debug symbols
And here is a simple KT file:
package sample
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
val deferred = async(Dispatchers.Unconfined, CoroutineStart.LAZY) {
println("Running Async Unconfined: on thread ${Thread.currentThread().name} has run.")
42
}
val result = deferred.await()
println("Async Unconfined Result is ${result}")
}
I installed the maven plugin under Project Structure | Module and screenshot attached.
Nevertheless, I'm getting "Unresolved References..." error. Attached screenshot...
Request if someone can help me to resolve this please?
Thanks
In your Project Structure > Modules > Dependencies tab, you selected Runtime as the scope, which makes the dependency only available at runtime (usually used for transitive dependencies). Try selecting Compile here.

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