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?
Benefits of using Dagger Dagger frees you from writing tedious and error-prone boilerplate code by: Generating the AppContainer code (application graph) that you manually implemented in the manual DI section. Creating factories for the classes available in the application graph.
Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.
x: It is an adaptation of an earlier version created by Square and now maintained by Google. Dagger2 is compile time Dependency injection framework that generates code to connect dependencies at compile time.
I know that dagger creates injection at compile time by generating code and hence its performance is better than Guice, which do it at runtime.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With