Suragch
Suragch

Reputation: 511566

Declaring custom 'clean' task when using the standard Gradle lifecycle plugins is not allowed

I am adding a library to jCenter so to do that I needed to add some plugins to my project's build.gradle file. However, I am getting the error

Declaring custom 'clean' task when using the standard Gradle lifecycle plugins is not allowed.

I can see the task clean block and when I delete it the error goes away. I assume that is all I need to do, but was it doing something important before? If I remove the plugins sometime and forget to add the clean block back in, what dire consequences are in store?

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id "com.jfrog.bintray" version "1.7.3"
    id "com.github.dcendents.android-maven" version "1.5"
}

allprojects {
    repositories {
        jcenter()
    }
}

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

This and this this did not satisfactorily answer the question.

Upvotes: 34

Views: 41127

Answers (8)

Binh Ho
Binh Ho

Reputation: 4936

Since buildDir is deprecated.

Let's use

tasks.register('clean', Delete) {
    delete rootProject.layout.buildDirectory
}

Upvotes: 3

Laksh
Laksh

Reputation: 31

In the given code:

allprojects {
repositories {
    google()
    jcenter()
    mavenCentral()
}

tasks.withType(JavaCompile) {
    sourceCompatibility = "1.8"
    targetCompatibility = "1.8"
}


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

replace the task clean with task delete then it will work:

allprojects {
repositories {
    google()
    jcenter()
    mavenCentral()
}

tasks.withType(JavaCompile) {
    sourceCompatibility = "1.8"
    targetCompatibility = "1.8"
}


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

Upvotes: 3

dilip sarkar
dilip sarkar

Reputation: 9

enter code hereallprojects {
repositories {
    google()
    jcenter()
    maven {
        url 'https://maven.google.com'
    }
}

}

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

if you have added maven in your "build.gradle" then checkout all the brackets. The code should be as above. It should solve the problem. It is possible that brackets are placed in different places.

Upvotes: 0

John
John

Reputation: 114

6m Okay, I get an error in Gradle that it cannot clean the root project file.

:25 AM Gradle sync failed: Declaring custom ‘clean’ task when using the standard Gradle lifecycle plugins is not allowed.

error code info-ide.actionsShowFilePathAction Exit Code 1

Clean Triggers nom run clean dependsOn"npmInstall"

In the Gradle doc 4.7 The Project in the directory where the Build is executed is also configured but only when Gradle is executed without any tasks. This way the default tasks behave correctly when projects are configured on demand.

Configuration on demand Gradle 4.6 Android plugin for Gradle 3.01 or 3.1.0 with Gradle Gradle properties file org.gradleconfigureondemand=false

So what I did was to comment out the task clean and now I get a build that words,

I think what happened is that this script works if you had built and modified the project. When you download the project you have not built it for the first time. Therefore there is no Root Project Path to clean as it has never been built yet. This causes it to fail. By commenting it out the first time you don’t get the error.

Here is the code. // Top-level build file where you can add configuration options common to all sub-projects/modules.

` buildscript { ext.kotlin_version = ‘1.2.41’

     ext.support_version = '26.1.0'

repositories {
    google()
    jcenter()
}

dependencies {
    classpath 'com.android.tools.build:gradle:3.1.2'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: ‘kotlin’

allprojects {
repositories {
google()
jcenter()
}
}

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

} */

'

Upvotes: -1

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

I had this same issue, unfortunately, for me I put the task clean in the wrong place. I had this:

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    tasks.withType(JavaCompile) {
        sourceCompatibility = "1.8"
        targetCompatibility = "1.8"
    }


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

Which needed to be this:

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }

    tasks.withType(JavaCompile) {
        sourceCompatibility = "1.8"
        targetCompatibility = "1.8"
    }
}

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

Upvotes: 2

Chaudhary Nouman
Chaudhary Nouman

Reputation: 200

Remove these lines from your code.

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

Upvotes: 5

Bharat Kumar Emani
Bharat Kumar Emani

Reputation: 3434

tools>kotlin>configure in the project you have to select Android Gradle not gradle

It worked for me

Upvotes: 1

Vampire
Vampire

Reputation: 38619

You should not try to override the default clean task, but instead configure it to delete additional stuff like

clean {
    delete rootProject.buildDir
}

But check first whether this is not the default behavior of the clean task anyway.

Alternatively if you want to be able to do a specific clean action individually, you can also define a separate task and add a dependency like

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

Upvotes: 36

Related Questions