0

I have three classes which are using different dependencies (SDK and libraries) in their code. How can I make Gradle not to compile (and include) the unused dependencies while building?

For example my classes are:

SDKObjectBuilder1, SDKObjectBuilder2, SDKObjectBuilder3

And in my code under some condition, I will create an object only from one of these classes. Like:

Object sdk;
if(..) {
    sdk = new SDKObjectBuilder1();
} else if (...) {
    sdk = new SDKObjectBuilder2();
} else {
    sdk = new SDKObjectBuilder3();
}

Now if sdk is an object of SDKObjectBuilder1 then the other two are unused (since no object is referring to them).

Can I make Gradle to not compile the unused dependencies in the other two classes?

Mehdi Satei
  • 1,225
  • 9
  • 26
  • 2
    But how will Gradle know not to compile code that you're evaluating POST-compilation? The way I understand your problem is: I baked 3 cakes, and once I arrive at the birthday party, people will pick one, and I want my oven to only cook that one... and return the ingredients for the other two untouched and uncooked. (?) Am I missing a bit here? :D – Martin Marconcini Sep 30 '19 at 12:33
  • nice analogy. In his scenario it is possible that he knows what kind of cake the people want at the time he is in the kitchen about to bake them, but that depends on the contents of the if/else and he left those out. Probably you are right and they are runtime checks – Tim Sep 30 '19 at 12:36
  • check this link : https://stackoverflow.com/questions/19379517/how-to-find-remove-unused-dependencies-in-gradle – YuvrajsinhJadeja Sep 30 '19 at 12:38
  • ^ not appliccable here – Tim Sep 30 '19 at 12:39
  • Thanks for your replies guys. It is exactly what @TimCastelijns mentioned. I tried the nebula but couldn't make it work. The content of the if/else is fetched from the build files. Let's say it will be mentioned in the String.xml file in the resource file – Mehdi Satei Sep 30 '19 at 12:42

0 Answers0