Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a custom 'clean' task in build.gradle for Android projects?

When creating a new project in Android Studio (version 2.1), the root build.gradle file has the following task:

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

I've noticed that some Android projects does not have this custom task and uses the default gradle clean task. What is the drawback with not having the above custom clean task?

like image 395
fornwall Avatar asked Sep 14 '16 09:09

fornwall


People also ask

What does Gradle clean task do?

clean will delete a build directory, all your classes will be removed for a fresh compile. assemble will create an archive (JAR) file from your Java project and will run all other tasks that require compile, test and so on.

Is Gradle clean necessary?

You don't need to run the clean task. Gradle will track task dependencies and clean appropriate parts for you. Here's an example Gradle project I created to show that the accepted answer is incorrect. If custom tasks don't track their dependencies well (they're bugged), then clean is a workaround.

What does clean build do Android studio?

Clear your project directory Obviously, try to clean your project from android studio : “Build -> Clean Project”. This will clear your build folders. Clear the cache of Android Studio using “File -> Invalidate Caches / Restart” choose “Invalidate and restart option” and close Android Studio.

What is task clean?

In this way the clean task (with the type=delete) deletes the build directory when it runs. It is useful when you modify some config files like the settings.


1 Answers

This will delete all the build directory when you compile the code and run the project. This is done in order to do a complete clean if you have modified some Gradle config files. For this reason it is important to not create any directory inside the /build/ folder as it will be deleted when Gradle runs the clean task. Some more info in https://developer.android.com/studio/build/build-cache.html#clear_the_build_cache

like image 96
Javi Avatar answered Dec 24 '22 14:12

Javi