Do not compile dead code

Is there a way to make gradle not compile dead code?
like unused class

Gradle cannot know what might be dead code.
Even if nothing in the codebase uses it, some other project could access it.
And even if no other project uses the code, even the same project could for example use reflection to access the code.

And even if you would put up the axiom that none of that is the case,
Gradle would need to analyse all the source code in the complete project to find out what is used and what not, which would probably need a very significant time to be done.

So if your goal is to save build time, such a feature would most probably be counter-productive.
If your goal ist to save space in the resulting product, there are tools that can analyse the final result and throw out potentially unused things.
But nowadays it should not really bet that relevant to save a few KiB.

The easiest would be, if you simply delete the dead code and thus already save the compilation time and all the dangerous hassle to throw it out later.