问题
As a Java developer I've become accustomed to having dependency injection available in applications. For Android though, I am especially wary of performance. What are the performance implications for using Guice in an Android app? I assume there is some overhead, but is it significant enough that I should avoid using Guice?
My use of it would likely just be to inject a few shared objects into various activities.
回答1:
As of version 3, Guice caches reflective objects to improve performance. There's at least one bug out against dalvik to make annotation lookups faster, but the current performance is workable.
回答2:
I would avoid using DI in android as google suggests: http://developer.android.com/training/articles/memory.html#DependencyInjection
Dagger solves some timing problems but still wastes memory for no real benefit.
回答3:
Roboguice (and Guice) can significantly degrade app startup time. My moderately sized and not-yet-complete app took almost 15 seconds to start on an HTC EVO 4g.
Take a look at Dagger as an alternative that should provide virtually no startup overhead.
回答4:
You should definitely use Dagger 2 for Android projects if you're concerned with performance.
Dagger 2 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.
来源:https://stackoverflow.com/questions/5067681/guice-performance-on-android