Does "Avoid dependency injection frameworks" in the Android Memory Guide apply to Dagger as well?

Solution 1:

This recommendation does not apply equally to all dependency injection frameworks.

..frameworks [that work like Guice] tend to perform a lot of process initialization by scanning your code for annotations, which can require significant amounts of your code to be mapped into RAM even though you don't need it..

Thus, if using a DI/IoC framework that doesn't scan for said [run-time] annotations, implying the [excessive] use of reflection, then this reason doesn't apply. While Dagger does use annotations these are used differently than by Guice1 and avoid the problem stated.

Since Dagger was written as "A fast dependency injector for Android and Java", the authors have designed it for this purpose and believe that it is suitable for such a target - go ahead, give it a try.


1 Dagger uses compile-time annotations (well, mostly) instead of relying on run-time annotations and reflection; it is the run-time annotation scanning and reflection that causes the issue the memory guide was warning about.

Solution 2:

The Android team has recently updated their recommendation to suggest developers use Dagger 2.

The previous recommendation was based on the high cost of reflection. Since Dagger 2 no longer uses reflection - Dagger 1 did - they believe it "can be used in Android apps without needless runtime cost or memory usage".

(Disclaimer: I'm the Dagger 2 team manager.)