来自模块的问题 运行 ktlint

Problems running ktlint from module

已更新,已启动任务,一切正常。

这是我来自 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"
}

但是当我改变对我的模块的依赖时 custom_ktlint_rules

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

和 运行 任务,我得到这个错误:

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.

  • 尝试:运行 使用 --stacktrace 选项获取堆栈跟踪。 运行 使用 --info 或 --debug 选项以获得更多日志输出。 运行 使用 --scan 以获得完整的见解。

  • https://help.gradle.org

  • 获取更多帮助

0 秒后构建失败

我的单独模块 build.gradle 在这里:

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

使用最新版本0.34.2此问题已在最新版本中解决。

有关详细信息,请查看 here

为 ktlint 使用 this"com.pinterest:ktlint:0.34.2"

试试这个:

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

所以,这就是我解决问题的方式 运行 ktlint

首先我更新了我的根 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
}

并在 build.gradle 中添加了下一个依赖项:

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