Morozov
Morozov

Reputation: 5250

Problems running ktlint from module

Updated ktlint, started tasks, everything works as it should.

Here is my code from build.gradle:

configurations {
    ktlint
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ktlint "com.pinterest:ktlint:0.34.2"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    //...another dependencies
}

repositories {
    jcenter()
}

configurations {
    ktlint
}

task ktlint(type: JavaExec, group: "verification") {
    description = "Check Kotlin code style."
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "src/**/*.kt"
    // to generate report in checkstyle format prepend following args:
    // "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml"
    // see https://github.com/pinterest/ktlint#usage for more
}
check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    args "-F", "src/**/*.kt"
}

But when I change the dependency to my module custom_ktlint_rules

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ktlint project(':custom_ktlint_rules')

and run the task, I get this error:

FAILURE: Build failed with an exception.

  • What went wrong: Could not determine the dependencies of task ':app:ktlint'.

    Could not resolve all task dependencies for configuration ':app:ktlint'. Could not resolve project :custom_ktlint_rules. Required by: project :app Cannot choose between the following variants of project :custom_ktlint_rules: - debugRuntimeElements - releaseRuntimeElements All of them match the consumer attributes: - Variant 'debugRuntimeElements': - Found com.android.build.api.attributes.BuildTypeAttr 'debug' but wasn't required. - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required. - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Apk' but wasn't required. - Found org.gradle.usage 'java-runtime' but wasn't required. - Found org.jetbrains.kotlin.platform.type 'androidJvm' but wasn't required. - Variant 'releaseRuntimeElements': - Found com.android.build.api.attributes.BuildTypeAttr 'release' but wasn't required. - Found com.android.build.api.attributes.VariantAttr 'release' but wasn't required. - Found com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Apk' but wasn't required. - Found org.gradle.usage 'java-runtime' but wasn't required. - Found org.jetbrains.kotlin.platform.type 'androidJvm' but wasn't required.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 0s

My build.gradle for separate module is here:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'

    compileOnly "com.pinterest:ktlint:$ktlintVersion"

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Upvotes: 6

Views: 14314

Answers (2)

Morozov
Morozov

Reputation: 5250

So, this is how i resolved my problem and run ktlint

First I was update my root build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.41'
    repositories {
        google()
        jcenter()
        gradlePluginPortal()

    }
    dependencies {
        classpath "org.jlleitschuh.gradle:ktlint-gradle:8.2.0"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

subprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent

    // Optionally configure plugin
    ktlint {
        debug = true
    }

    dependencies {
        ktlintRuleset project(":custom_ktlint_rules")
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And in build.gradle added next dependencies:

apply plugin: 'kotlin'

dependencies {
    compileOnly("org.jetbrains.kotlin:kotlin-stdlib")
    compileOnly("org.jetbrains.kotlin:kotlin-reflect")
    compileOnly("org.jetbrains.kotlin:kotlin-script-runtime")
    compileOnly("com.pinterest.ktlint:ktlint-core:0.34.2")
}

Upvotes: 1

GOVIND DIXIT
GOVIND DIXIT

Reputation: 1828

Use the latest version 0.34.2 This problem is resolved in recent versions.

For more details look here

Use this library for ktlint "com.pinterest:ktlint:0.34.2"

Try this:

ktlint project(':custom_ktlint_rules', configuration: 'default')

Upvotes: 1

Related Questions