Android and Dependency Injection [closed]

2019-01-16 06:37发布

问题:

I've been looking around, in vain, for some information on using a dependency injection container in Android development. Specifically, how to override the creation of an Activity in a way that will also work when coming back from being killed (for whatever reason).

Has anyone got any experience in this area?

回答1:

It appears you can use Google Guice 2.0 with Android. You might also look into roboguice.

Edit:

Spring is also now available for Android

Edit: Roboguice is now deprecated. You might try Toothpick as mentioned in the comments by the developer behind Toothpick.



回答2:

You might also want to consider Spring ME. Although originally intended for Java ME, I have seen reports from people using it for Android as well. The benefit would be that you have a familiar programming model (Spring) without the penalty: Spring ME has a 0k footprint.



回答3:

If you are used to use Spring in other projects, you won't be very happy with Spring for Android (it's only a REST library with Auth support), Spring ME (completely different workflow and reduced featureset) or Tiny Spring (e.g. only Spring-like configuration).

You could however give my project RoboSpring a try. From the description:

RoboSpring is a (real) port of the Spring Framework to the Android platform. Additionally it offers preliminary support for functionality introduced by RoboGuice like injecting View references into Activities and more. RoboSpring is based on version 3.1.0 RELEASE of Spring's core, beans, context and aop components. It offers the following functionality:

  • Configure application components with a Spring configuration file (XML)
  • Autowire your Android components with beans from the Spring application context.
  • Inject the Android application context into your Spring Beans.
  • Inject views into Activities.
  • … and more

https://github.com/dthommes/RoboSpring



回答4:

There is also a new Spring project for Android: Tiny Spring. It solves the very basics of XML configuration but doesn't do everything that Spring does.