【问题标题】:com.diffplug.gradle.spotless && task clean(type: Delete){} Error:Cannot add task 'clean' as a task with that name already existscom.diffplug.gradle.spotless && task clean(type: Delete){} Error:Cannot add task 'clean' as a task with the name already exists
【发布时间】:2019-11-22 08:48:16
【问题描述】:

这是我的build.gradle


buildscript {
    ext {
        ...
    }
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id "com.diffplug.gradle.spotless" version "3.26.0"
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

spotless {
    kotlin {
        target "**/*.kt"
        ktlint(ktlintVersion).userData(['max_line_length' : '100'])
    }
}

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

原来编译成功,但是在引入spotless的时候,导致了这个错误:

com.diffplug.gradle.spotless && task clean(type: Delete){} Error:Cannot add task 'clean' as a task with that name already exists

所以,我猜这两个部分是冲突的,但我不知道具体原因。

plugins {
    id "com.diffplug.gradle.spotless" version "3.26.0"
}
//...
task clean(type: Delete) {
    delete rootProject.buildDir
}

【问题讨论】:

    标签: android android-studio build.gradle gradle-plugin


    【解决方案1】:

    您已在项目级别 build.gradle 中定义了此任务(在您的 cmets 中发布):

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

    Android Studio 的Gradle wrapper 已经定义了clean 任务,因此无需重新定义。只需从您的项目级 build.gradle 文件中删除该任务即可。

    【讨论】:

    • 是的,这正是我的理解,谢谢你回答我的疑惑:)
    【解决方案2】:

    添加到 R.Desai 的回答中,

    实际上 Gradle 的 BasePlugin 只清理应用级别的构建目录。医生说

    clean — Delete 删除构建目录及其中的所有内容,即 Project.getBuildDir() 项目属性指定的路径。

    Check the docs here for more info.

    但是,解决方案是相同的。一尘不染的插件与您定义的“干净”任务相冲突,因为它在后台具有相同的实现,它会清理项目级别的构建目录。这是他们的 github repo "Duplicate clean task #521"

    中的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      相关资源
      最近更新 更多