32

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.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • @Suragch I am having the same problem by using com.github.dcendents.android-maven, how you resolved the issue? – Nainal Jul 10 '18 at 10:14
  • @Nainal, I think at the time I just removed the `clean` block. I don't think that is the solution, though, and I never felt like I understood the problem or how to solve it correctly. – Suragch Jul 11 '18 at 00:18
  • @Suragch Thanks – Nainal Jul 11 '18 at 06:44

7 Answers7

36

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
Vampire
  • 35,631
  • 4
  • 76
  • 102
  • 6
    I have the same problem and I didn't try to override anything. It's in the Gradle script generated by Android Studio. – Konrad Morawski Jun 30 '17 at 20:58
  • Doesn't make my answer less valid. Change the generated script to be valid and complain to AS bug tracker. – Vampire Jun 30 '17 at 21:21
  • @Vampire, you are quite correct. Thanks for the insight. Still need to figure out the origin of why out-of-the-box Android app adds the `task clean...` – Quantium May 13 '18 at 05:41
  • Because it worked up to Gradle 2.3, then got [deprecated in Gradle 2.4](https://docs.gradle.org/2.4/release-notes.html#lifecycle-plugin-changes), and then [forbidden in Gradle 3.0](https://docs.gradle.org/3.0/release-notes.html#changes-to-previously-deprecated-apis) – Vampire Apr 04 '23 at 11:35
5

Remove these lines from your code.

task clean(type: Delete) {
    delete rootProject.buildDir
}
James Brewer
  • 1,745
  • 14
  • 17
Chaudhary Nouman
  • 202
  • 1
  • 4
  • 12
3

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`
}
}
CKE
  • 1,533
  • 19
  • 18
  • 29
Laksh
  • 31
  • 1
  • Is there any difference between the `task delete` and `task clean` ? –  Sep 24 '18 at 22:10
2

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

It worked for me

Bharat Kumar Emani
  • 3,436
  • 3
  • 16
  • 30
2

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
}
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0
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.

-1

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

} */

'
John
  • 104
  • 2