I want my project to perform ktlintCheck on all kotlin file, but it only check on build.gradle.kts file.
build.gradle.kts file as below
ktlint {
version.set("9.4.0")
debug.set(true)
verbose.set(true)
android.set(false)
outputToConsole.set(true)
reporters {
reporter(ReporterType.PLAIN)
reporter(ReporterType.CHECKSTYLE)
}
ignoreFailures.set(false)
kotlinScriptAdditionalPaths {
include(fileTree("src/"))
}
filter {
exclude("**/generated/**")
include("**/kotlin/**")
}
}
subprojects {
apply(plugin = "org.jlleitschuh.gradle.ktlint")
ktlint {
debug.set(true)
}
}
When I run gradlew ktlintCheck, the Terminal output as below:
gradlew ktlintCheck
> Task :ktlintKotlinScriptCheck
[DEBUG] Discovered ruleset with " standard" id.
[DEBUG] Discovered reporter with "checkstyle" id.
[DEBUG] Discovered reporter with "json" id.
[DEBUG] Discovered reporter with "html" id.
[DEBUG] Discovered reporter with "plain" id.
[DEBUG] Initializing "plain" reporter with {verbose=true, color=true, color_name=DARK_GRAY}
[DEBUG] Initializing "plain" reporter with {verbose=true, color=true, color_name=DARK_GRAY}, output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.txt
[DEBUG] Initializing "checkstyle" reporter with {verbose=true, color=true, color_name=DARK_GRAY}, output=C:\Code\XXXX\build\reports\ktlint\ktlintKotlinScriptCheck\ktlintKotlinScriptCheck.xml
[DEBUG] Checking C:\Code\XXXX\build.gradle.kts
Resolving .editorconfig files for C:\Code\XXXX\build.gradle.kts file path
[DEBUG] 809ms / 1 file(s) / 0 error(s)
To begin there is an error in the configuration of your build.gradle file, in the block Ktlint settings, the line where it appears "version.set (" 9.4.0 ")" is incorrect as well as unnecessary. In any case, if you still decide to use this setting, it should be, for example, version.set ("0.37.2") , since it refers to the Ktlint version and not the jlleitschuh / ktlint-gradle plugin
To solve the problem make the following modifications to the build.gradle.kts file (I use Gradle 6.6.1 with Kotlin DSL in Intellij IDEA community Edition)
Please replace the plugin with the previous version (9.3.0):
build.gradle.kts file in root project
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType // for Ktlint reports
plugins {
kotlin("jvm") version "1.4.10" // Kotlin Compiler
id("org.jetbrains.dokka") version "1.4.10" // Documentation Engine For Kotlin
id("org.jlleitschuh.gradle.ktlint") version "9.3.0" // Kotlin Linter
}
group = "my group" // Replace with your group
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation("org.junit.jupiter:junit-jupiter:5.4.2")
testImplementation(kotlin("test-junit5"))
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.test {
useJUnitPlatform()
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
ktlint {
// THIS LINE IS NOT necessary and is incorrect -> version.set("9.4.0")
verbose.set(true)
outputToConsole.set(true)
coloredOutput.set(true)
debug.set(false) // in your configuration this option must be set to true
android.set(false)
outputColorName.set("RED")
ignoreFailures.set(false)
enableExperimentalRules.set(false)
reporters {
reporter(ReporterType.CHECKSTYLE)
reporter(ReporterType.JSON)
reporter(ReporterType.HTML)
}
filter {
exclude("**/style-violations.kt")
exclude("**/generated/**")
include("**/kotlin/**")
}
}
Related
I want to make a JAR to test and deploy my app but it doesn't work, locally it can't find the main attribute in the manifest file, and in the deployement it starts but i have this error :
java.lang.noclassdeffounderror: kotlinx/coroutines/slf4j/mdccontex
W
hen I do it locally using the artifact Jar I can't find the MainClass of my app, it recommands me all sorts of other modules.
I tried modying my gradle, the manifest, the configuration but really nothing works it's depressing, it looks so easy on some youtube videos but it just doesn't work
what I do to make the JAR
The classes i have access to when i try to select them
Here is my Gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.5.10"
application
kotlin("plugin.serialization") version "1.6.10"
}
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven(url = "https://jitpack.io")
}
val ktor_version: String by project
dependencies {
// Fix HTML issue on some responses
implementation("org.apache.commons:commons-text:1.10.0")
// Ktor dependencies
implementation("io.ktor:ktor-client-auth:$ktor_version")
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-resources:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-client-logging:$ktor_version")
// Logging dependencies
implementation("ch.qos.logback:logback-classic:1.4.0")
implementation(kotlin("stdlib-jdk8"))
// Database
implementation ("mysql:mysql-connector-java:8.0.30")
implementation ("org.ktorm:ktorm-core:3.5.0")
implementation ("org.ktorm:ktorm-support-mysql:3.5.0")
}
application {
mainClass.set("MainKt")
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
tasks {
jar {
manifest {
attributes["Main-Class"] = application.mainClass
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
configurations.compileClasspath.get().forEach {
from(if (it.isDirectory) it else zipTree(it))
}
}
compileKotlin{
kotlinOptions.jvmTarget = "1.8"
}
}
Thanks in advance
My project had a lot of issues and inconsistency so I started a new one and copied all the files / packages, spent an hour redoing all the imports and plugins that my code actually needed to run and it finally worked, the same comments don't work on the previous project
I tried to implement Kotlin check style in project using ktlint.
I added
plugins {
id("org.jlleitschuh.gradle.ktlint") version "10.1.0" apply false
}
in root build.gradle.kts and
plugins {
id("org.jlleitschuh.gradle.ktlint")
}
in build.gradle.kts in subprojects
When I called
gradlew ktlintCheck
I got
FAILURE: Build failed with an exception.
* What went wrong:
org/jetbrains/kotlin/gradle/dsl/KotlinProjectExtension
How can I fix it?
This is how I went about it for Klint with Kotlin DSL on gradle.kts files for Android Gradle. Have also added Spotless with Kotlin DSL ( code formatter ) at the same time.
It is also worth noting mine is NOT a Multi Module project.
Step 1 - gradle.kts (app) - Add Klint and Spotless plugins and import Reporters
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
plugins {
id("com.android.application") ...
//Klint
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
//Spotless
id("com.diffplug.spotless")
}
Step 2 - gradle.kts (app) - Configure Klint only
// ktlintFormat task will need to run before preBuild
tasks.getByPath("preBuild")
.dependsOn("ktlintFormat")
configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
android.set(true)
ignoreFailures.set(false)
disabledRules.set(setOf("final-newline", "no-wildcard-imports"))
reporters {
reporter(ReporterType.PLAIN)
reporter(ReporterType.CHECKSTYLE)
reporter(ReporterType.SARIF)
}
}
Step 3 - gradle.kts(app) - Configure Spotless only
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
// version, setUseExperimental, userData and editorConfigOverride are all optional
ktlint("0.45.2")
.setUseExperimental(true)
.userData(mapOf("android" to "true"))
.editorConfigOverride(mapOf("indent_size" to 2))
}
kotlinGradle {
target("*.gradle.kts") // default target for kotlinGradle
ktlint() // or ktfmt() or prettier()
}
}
Step 4 - gradle.kts(project) - Add Spotless in the Classpath
buildscript {
repositories{
google()
mavenCentral()
}
dependencies {
...
//class path for Spotless
classpath("com.diffplug.spotless:spotless-plugin-gradle:6.9.1")
}
}
The 2 plugins works fine with this set-up.
I wanted to try out Kotlin and ktlint and I was happy to see that it supports tab indentation via the editorconfig file (since this PR). Sadly it doesn't seem to work for me. I haven't used editorconfig before, I might have made some simple mistake.
My .editorconfig in the root folder:
indent_style = tab
My gradle file:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val koinVersion: String by project
val junitVersion: String by project
plugins {
application
kotlin("jvm") version "1.4.30"
id("org.jlleitschuh.gradle.ktlint") version "10.0.0"
}
group = "me.me"
version = "1.0-SNAPSHOT"
application {
mainClassName = "de.me.bot.translate.MainKt"
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation("com.sksamuel.hoplite:hoplite-core:+")
implementation("org.koin:koin-core:$koinVersion")
testImplementation("org.koin:koin-test:$koinVersion")
testImplementation(kotlin("test-junit5"))
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "13"
}
However running gradle ktlintCheck still throws exceptions because of unexpected tab characters. I don't understand why. I ran it with --debug, but it didn't give me any useful information.
Here is my project: github.com
EditorConfig specification states:
With the exception of the root key, all pairs MUST be located under a section to take effect.
So your .editorconfig file should be:
root = true
[*.{kt,kts}]
indent_style = tab
I'm trying to run the simplest example with coroutines:
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch {
delay(1000L)
println("${Thread.currentThread().name}: World")
}
println("${Thread.currentThread().name}: Hello")
Thread.sleep(2000L)
println("${Thread.currentThread().name}: Finish!")
}
And my build.gradle file looks like this:
buildscript {
// Consider moving these values to `gradle.properties`
ext.kotlin_version = '1.3.0-rc-146'
ext.kotlin_gradle_plugin_version = '1.3.0-rc-198'
ext.kotlinx_coroutines = '1.0.0-RC1'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.1.51"
}
apply plugin: 'idea'
apply plugin: 'application'
group 'by.kotlin'
version '1.0-SNAPSHOT'
mainClassName = 'MainKt'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
But when I run this example, I've got the following errors:
e: ...Main.kt: (6, 17): 'launch(CoroutineContext = ..., CoroutineStart = ..., [ERROR : Bad suspend function in metadata with constructor: Function2]<CoroutineScope, Continuation<Unit>, Any?>): Job' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
e: ...Main.kt: (7, 9): Suspend function 'delay' should be called only from a coroutine or another suspend function
e: ...Main.kt: (7, 9): 'delay(Long): Unit' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
> Task :compileKotlin FAILED
Why do these errors occur? I'm completely confused, because the first error says that launch "is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2", but I use Kotlin 1.3 in my build.gradle file (in particular, '1.3.0-rc-146')...
UPD
It seems that the reason of problem is in IntelliJ IDEA Settings:
But how to fix it, if the latest language version, which can be selected there, is 1.2, not 1.3?
Make sure you have updated Kotlin to 1.3. You can do this from Preference->Lanugage & Framework->Kotlin Updates
Then change the version of kotlin.jvm plugin to 1.3.0 in gradle. (https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm)
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.3.0"
}
And for including coroutines
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.0'
}
It should be fine now.
You must change kotlin plugin version
Your current kotlin plugin version is 1.2.51
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
this is correct
buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_gradle_plugin_version = '1.3.0'
ext.kotlinx_coroutines = '1.0.0'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
'org.jetbrains.kotlin:kotlin-gradle-plugin:'+kotlin_version
}
}
I solved this by manually updating the Kotlin-IntelliJ Plugin.
First, download the newer version of the Kotlin plugin that is compatible with your version of IntelliJ https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable
Then in IntelliJ's Settings -> Plugins, click on the settings/gear icon on the top-right side. From there choose Install Plugin from Disk..., choose the zip file you got from the intellij website. Then it will ask you to restart the IDE, and that's it :)
If it has started to occur only recently then chances are two links are generating same file with same name, in this case removing one of your recent file from build.gradle will help you resolving the issue.
In my case this was causing the issue
implementation 'com.miguelcatalan:materialsearchview:1.4.0'
implementation 'com.github.Ferfalk:SimpleSearchView:0.2.0'
After removing first one out of them the issue was resolved.
Before upgrading to Android Gradle Plugin 1.3, the custom lint scope was only Android source files.
After I upgraded to 1.3.1, my tests files started getting checked and as a scenario fails my custom lint rule, the build fails.
There is no documentation about this. I read some warnings have become fatal but nothing around test files getting scanned.
Anyone facing such an issue?
EDIT:
top level build.gradle
buildscript {
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'org.sonarqube.gradle:gradle-sonarqube-plugin:1.0'
}
}
allprojects {
repositories {
flatDir {
dirs './prebuilt-libs'
}
mavenCentral()
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}
Error:
HardCoding: Detects HardCoded "abc" string
../../src/androidTest/java/com/xyz/mobile/trips/test.java:108: HardCoding Found. (This is my custom lint rule)
hasExtra(CODE, "abc")