Dagger 2 对性能有什么好处吗?

Does dagger 2 has any benefit for performance?

我是 Dagger 2 和 android 依赖注入的新手。我听说很多 android 开发人员使用 dagger 2。我知道它管理 类 之间的依赖关系,我们将不再使用关键字“new”,但我想知道为什么我真的会用吗?它是否管理内存分配并最大限度地减少泄漏?它对应用程序的性能有好处吗?

主要思想是解耦 类 以使 代码更易于测试和维护 因为没有硬依赖。由于我不太擅长解释,建议你看看下面的文章,以防你还没有读过它们。

MVP 和 DI:

匕首 2:

匕首 2.11:

我希望这能回答您的任何问题!

我想这是找到所有关于使用 Dagger 的好处的讨论的好地方。在 reddit 上找到其他开发人员对使用 dagger 的看法。 Reddit link.

性能取决于开发人员,以避免资源的低效使用,例如 CPU、内存、图形、网络和设备电池。 但是依赖注入是一种设计模式,它基于控制反转的概念,它说a class 应该从外部获取它的依赖项。简而言之,class 不应该实例化另一个 class,而是应该从配置 class 中获取实例,而不是在内部构造它们。 因此,这种模式允许开发人员编写低耦合的代码,因此可以轻松地进行测试和维护。

但是为了比较依赖注入库的性能,看到这个很好 link : http://blog.nimbledroid.com/2016/03/07/performance-of-dependency-injection-libraries.html

我想专注于依赖注入应用程序的内存和性能。所以,我在 android 开发者网站上找到了这个答案。 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.