【问题标题】:Problems running ktlint from module从模块运行 ktlint 的问题
【发布时间】:2019-08-01 12:07:04
【问题描述】:

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

但是当我将依赖项更改为我的模块时 custom_ktlint_rules

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

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

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

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

    无法解析配置“:app:ktlint”的所有任务依赖项。 无法解析项目:custom_ktlint_rules。 要求: 项目:应用程序 无法在以下项目变体中进行选择:custom_ktlint_rules: - 调试运行时元素 - 发布运行时元素 它们都符合消费者属性: - 变体'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"
}

【问题讨论】:

  • 您使用的是最新版本吗?最新版本解决了这个问题
  • @GOVINDDIXIT ext.ktlintVersion = '0.29.0'
  • 试试这个版本'0.34.2'
  • 我已经添加了一个答案,希望它能解决这个问题。

标签: android android-gradle-plugin


【解决方案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
}

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

【讨论】:

    【解决方案2】:

    使用最新版本0.34.2 这个问题在最近的版本中得到解决。

    更多详情请看here

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

    试试这个:

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

    【讨论】:

    • 我试过了,但这个更改又出现了一个错误:ERROR: Failed to resolve: com.github.shyiko.ktlint:ktlint-core:0.34.2 Show in Project Structure dialog Affected Modules: custom_ktlint_rules
    • 将这个库用于 ktlint "com.pinterest:ktlint:0.34.2",用这个替换现有的
    • 更新了 ktlint,开始了任务,一切正常。但是与我的模块的链接不起作用,我不明白为什么。我现在将更新问题。但是对于我加号版本的评论,谢谢
    • 更新问题。现在似乎一切都描述得很好
    • 已更新,试试看是否有帮助
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多