IntelliJ missing reference to slf4j in new Ktor project - kotlin

I started a new Ktor project through their IntelliJ plugin and everything is compiling and running fine with Gradle. However in IntelliJ everything that references the slf4j logger is having a Unresolved reference: ... error.
My import and setting log leve looks like this (sorry my reputation is not high enough to post images):
The project is generated with the following dependencies
// gradle.properties
ktor_version=1.4.0
logback_version=1.2.1
// build.gradle.kts
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-auth:$ktor_version")
implementation("io.ktor:ktor-auth-jwt:$ktor_version")
implementation("io.ktor:ktor-jackson:$ktor_version")
testImplementation("io.ktor:ktor-server-tests:$ktor_version")
}
I have tried to add org.slf4j:slf4j-nop:1.7.30 to my dependencies without any luck.
So my questions is, has anyone seen this error before and know if there is a dependency or setting i could change? It is annoying to not have any code completion and errors scattered around the IDE even though it compiles and run fine.
The full application code generated with the Ktor plugin is below if needed:
package com.example
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.features.*
import org.slf4j.event.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.auth.*
import com.fasterxml.jackson.databind.*
import io.ktor.jackson.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
#Suppress("unused") // Referenced in application.conf
#kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(CallLogging) {
level = Level.INFO
filter { call -> call.request.path().startsWith("/") }
}
install(Authentication) {
}
install(ContentNegotiation) {
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
get("/json/jackson") {
call.respond(mapOf("hello" to "world"))
}
}
}

This was a caching issue with Gradle and IntelliJ, probably caused from other project dependencies. To IntelliJ the slf4j-api library was empty. Removing the global Gradle cache with rm -rf ~/.gradle/caches and re-download the dependencies solved the issue for me.

Related

Unable to use plugin in gradle.kts file

i´m trying to get a plugin to have the current version in a gradle kts file. I´m not able to get the apply for some reason, this is my code ->
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("com.google.code.gson:gson:${rootProject.extra["gson_version"]}")
}
}
plugins {
id("me.tadej.versioning") version "0.2.0"
}
But i´m getting this error ->
"The plugins {} block must not be used here. If you need to apply a plugin imperatively, please use apply() or apply(plugin = "id") instead."
What i´m doing wrong here ?
Thanks!
EDIT:
I tried to write this as:
import com.google.gson.Gson
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.net.URL
plugins {
id("me.tadej.versioning") version "0.2.0"
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("com.google.code.gson:gson:${rootProject.extra["gson_version"]}")
}
And got this error message:
ScriptCompilationException(errors=[ScriptCompilationError(message=Unresolved reference: gson, location=/Users/develop/.gradle/.tmp/gradle-kotlin-dsl-14359683498958959271.tmp/jira.gradle.kts (1:19)), ScriptCompilationError(message=Unresolved reference: classpath, location=/Users/develop/.gradle/.tmp/gradle-kotlin-dsl-14359683498958959271.tmp/jira.gradle.kts (17:5)), ScriptCompilationError(message=Unresolved reference: Gson, location=/Users/develop/.gradle/.tmp/gradle-kotlin-dsl-14359683498958959271.tmp/jira.gradle.kts (50:21)), ScriptCompilationError(message=Unresolved reference: Gson, location=/Users/develop/.gradle/.tmp/gradle-kotlin-dsl-14359683498958959271.tmp/jira.gradle.kts (100:16)), ScriptCompilationError(message=Unresolved reference: it, location=/Users/develop/.gradle/.tmp/gradle-kotlin-dsl-14359683498958959271.tmp/jira.gradle.kts (101:26)), ScriptCompilationError(message=Unresolved reference: it, location=/Users/develop/.gradle/.tmp/gradle-kotlin-dsl-14359683498958959271.tmp/jira.gradle.kts (101:40))])
at org.gradle.kotlin.dsl.support.KotlinCompilerKt.compileKotlinScriptModuleTo(KotlinCompiler.kt:187)
at org.gradle.kotlin.dsl.support.KotlinCompilerKt.compileKotlinScriptToDirectory(KotlinCompiler.kt:148)
at org.gradle.kotlin.dsl.execution.ResidualProgramCompiler$compileScript$1.invoke(ResidualProgramCompiler.kt:708)
at org.gradle.kotlin.dsl.execution.ResidualProgramCompiler$compileScript$1.invoke(ResidualProgramCompiler.kt:85)
at org.gradle.kotlin.dsl.provider.StandardKotlinScriptEvaluator$InterpreterHost$runCompileBuildOperation$1.call(KotlinScriptEvaluator.kt:162)...

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.

Compose gradle community plugins requiring a version in a binary plugin

I'm writing a binary plugin that's pushed as an artifact to a remote repository to be re-used. One of the things I want to accomplish with this plugin is to compose a set of additional plugins that should always be present. When composing official gradle plugins like the java plugin, that works fine. But I can't find the strategy for composing a community plugin requiring a version that would use this syntax in a build.gradle.kts file:
plugins {
id("com.diffplug.spotless") version "6.1.0"
}
All of the APIs I'm discovering in the gradle plugin library makes no reference of specifying a version, which makes me think I need to configure it elsewhere, like how a build can specify defaults in the pluginManagement block in settings.gradle.
This is a distilled version of how I'm trying to apply this.
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyPlugin : Plugin<Project> {
override fun apply(target: Project) {
// This works OK
target.plugins.apply("java")
// This is a community plugin, so it requires a version be set and fails
target.plugins.apply("com.diffplug.spotless")
}
}
Is there an API I'm missing? Or am I approaching this from the wrong direction?
Solved this by following aSemy's advice in a comment. I stopped trying to declare community plugins by ID and switched to applying the class of the plugin, after adding it to my plugin's classpath.
build.gradle.kts
dependencies {
api("com.diffplug.spotless:spotless-plugin-gradle:6.3.0")
}
Plugin class:
import com.diffplug.gradle.spotless.SpotlessPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.plugins.apply("java")
target.plugins.apply(SpotlessPlugin::class.java)
}
}

Kotlin in VS Code - Unresolved reference for user defined class in another file

I'm trying to set up Kotlin for VS Code following the instructions in this article, which basically says to install the Kotlin and Code Runner extensions.
So far, so good, I am able to run the following code:
App.kt:
fun main() {
MyApp().printTest()
}
class MyApp {
fun printTest() {
println("Hello test")
}
}
However, when I try to extract the MyApp class to another file in the same folder, I can't import it:
App.kt:
import MyApp.MyApp // import MyApp doesn't work also
fun main() {
MyApp().printTest()
}
MyApp.kt:
class MyApp {
fun printTest() {
println("Hello test")
}
}
I've tried to import the Java Import Snippets extension, but it doesn't work, showing a unresolved reference for MyApp.
Am I missing some extension or VS Code configuration?
That's the command VS Code is running:
cd ".../testapp/src/" && kotlinc App.kt -include-runtime -d App.jar && java -jar App.jar
Should it reference MyApp.kt somehow?
Note: I'm on Debian.
You have no project's package name defined.
Put the files App.kt and MyApp.kt in the same folder level.
In App.kt, only use
import MyApp // or no import at all,
fun main() {
MyApp().printTest()
}
Read more about Java/Kotlin package

Unable to resolve cinterop IOS import in Kotlin Multiplatform

I have followed the Kotlin documentation for adding iOS dependencies. In my case the dependency is a pre-compiled framework provided through a third party. So I have followed the case for framework without cocoapod.
I placed my MyFramework.def file in /src
language = Objective-C
modules = MyFramework
package = MyFramework
Then I added the following to the build.gradle.kts in the Kotlin object
```
ios {
binaries {
framework {
baseName = "shared"
}
}
}
iosArm64() {
compilations.getByName("main") {
val JWBLe by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting
val iosTest by getting
}
Then I build the project. The library does indeed get seen and I see that in External Libraries, there is a shared-cinterop-MyFramework.klib
However, when I try to import this package into my code under src/iosMain/kotlin/com.example.testapp.shared/platform.kt
I get unresolved error for the library. It seems like I should also need to add something to sourceSets? But I am unsure.
First of all, I got to notice that the Gradle script is incorrect. In this case, the iosArm64 target was declared twice - by the target shortcut and once again where you configure the cinterop. To avoid this duplication, it would be better to configure cinterop like that:
ios()
val iosArm = targets.getByName("iosArm64") as org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
// A bit dirty cast, but as I'm sure iosArm64 is the Native target, it should be fine. Needed to make highlighting below work as expected.
iosArm.apply {
compilations.getByName("main") {
val JWBLe by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
However, this adjustment won't help with accessing cinterop bindings from the iosMain. In the current state of commonizer, it can share only platform libraries. So anyway, moving all code utilizing those bindings into the src/iosArm64Main folder is the best option available at the moment. Here go an issue from the official tracker to upvote and subscribe - Support commonization of user-defined libraries.
So after some playing around I found the answer.
The dependency was set for a module of iosArm64 which is not available to the iosMain.
I created another folder src/iosArm64Main and placed the source file there. At that point it was able to resolve the library.