从模块运行 ktlint 时出现问题

Mor*_*zov 6 android android-gradle-plugin

更新了ktlint,启动了任务,一切都按预期进行。

这是我的代码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"
}
Run Code Online (Sandbox Code Playgroud)

但是当我更改对模块custom_ktlint_rules的依赖关系时

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

并运行任务,我收到此错误:

失败:构建失败并出现异常。

  • 出了什么问题:无法确定任务“:app:ktlint”的依赖关系。

    无法解析配置“:app:ktlint”的所有任务依赖项。无法解析项目:custom_ktlint_rules。要求者:project :app 无法在项目 :custom_ktlint_rules 的以下变体之间进行选择: - debugRuntimeElements - releaseRuntimeElements 所有这些变体都与消费者属性匹配: - Variant 'debugRuntimeElements': - 找到 com.android.build.api.attributes.BuildTypeAttr 'debug ' 但不是必需的。- 找到 com.android.build.api.attributes.VariantAttr 'debug' 但不是必需的。- 找到 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Apk' 但不是必需的。- 找到 org.gradle.usage 'java-runtime' 但不是必需的。- 找到 org.jetbrains.kotlin.platform.type 'androidJvm' 但不是必需的。- 变体“releaseRuntimeElements”: - 找到 com.android.build.api.attributes.BuildTypeAttr“release”,但不是必需的。- 找到 com.android.build.api.attributes.VariantAttr 'release' 但不是必需的。- 找到 com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Apk' 但不是必需的。- 找到 org.gradle.usage 'java-runtime' 但不是必需的。- 找到 org.jetbrains.kotlin.platform.type 'androidJvm' 但不是必需的。

  • 尝试:使用 --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"
}
Run Code Online (Sandbox Code Playgroud)

Mor*_*zov 1

所以,这就是我解决问题并运行 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
}
Run Code Online (Sandbox Code Playgroud)

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")
}
Run Code Online (Sandbox Code Playgroud)