18

I'm a newbie in dagger 2 and dependence injection in android. I'm hearing that a lot of android developers use dagger 2. I understand that It manages dependencies between classes and with it we will not use anymore the keyword "new" but I want to know why should I use it really? Does it manage memory allocation and minimize leaks? Does it benefit the performance of the application?

nimmp
  • 329
  • 2
  • 9

4 Answers4

14

I want to concentrate on memory and performance of the application with dependency injection. So, I found this answer in android developer website. https://developer.android.com/topic/performance/memory.html#DependencyInjection :

Use Dagger 2 for dependency injection frameworks

can simplify the code you write and provide an adaptive environment that's useful for testing and other configuration changes.

If you intend to use a dependency injection framework in your app, consider using Dagger 2. Dagger does not use reflection to scan your app's code. Dagger's static, compile-time implementation means that it can be used in Android apps without needless runtime cost or memory usage.

Other dependency injection frameworks that use reflection tend to initialize processes by scanning your code for annotations. This process can require significantly more CPU cycles and RAM, and can cause a noticeable lag when the app launches.

TylerH
  • 20,799
  • 66
  • 75
  • 101
nimmp
  • 329
  • 2
  • 9
12

Performance is up to developer to avoid inefficient use of resources, such as the CPU, memory, graphics, network, and device battery. But dependence injection is a design pattern that is based on the concept of Inversion of Control which says that a class should get its dependencies from outside. In simple words, no class should instantiate another class but should get the instances from a configuration class instead of constructing them internally. So, this pattern allows developers to write code that has low coupling and which can therefore be easily tested and maintained.

But to compare the Performance of Dependency Injection Libraries , see this good link : http://blog.nimbledroid.com/2016/03/07/performance-of-dependency-injection-libraries.html

Imene Noomene
  • 3,035
  • 5
  • 18
  • 34
5

The main idea is to decouple the classes to make the code easier to test and maintain as there are no hard dependecies. As I am not that good with explanations, I suggest you take a look at the following articles in case you haven't read them yet.

MVP and DI:

Dagger 2:

Dagger 2.11:

I hope this answers any of your questions!

1

I guess this is a good place to find all the discussion about benefits of using Dagger. find what other developers think about using dagger on reddit. Reddit link.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52