首页 > 解决方案 > com.diffplug.gradle.spotless && task clean(type: Delete){} Error:Cannot add task 'clean' as a task with the name already exists

问题描述

这是我的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
}

标签: androidandroid-studiobuild.gradlegradle-plugin

解决方案


您在项目级别定义了此任务build.gradle(如您的评论中发布的那样):

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

Android StudioGradle wrapper已经clean定义了一个任务,所以不需要重新定义它。只需从项目级build.gradle文件中删除该任务即可。


推荐阅读