36

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?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • If you do end up using http://code.google.com/p/roboguice roboguice, I'd love to have your feedback. I encourage you to join the google group and let us know what your experience is like. – emmby Mar 02 '10 at 23:37

4 Answers4

25

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.

Jay Askren
  • 10,282
  • 14
  • 53
  • 75
  • Thanks, it looks like roboguice is the closest I'm going to get it. It uses inheritance (`GuiceActivity`) and performs injection on `setContentView`, but after checking out the `Activity` creation callstack I can see there's no other way. – Richard Szalay Jan 26 '10 at 07:51
  • By the way Richard, RoboGuice now does injection in two steps. Once during onCreate for most injections, and once during setContentView for View injections. – emmby Mar 03 '10 at 06:19
  • 1
    RoboGuice, is now deprecated, you should give a try at Toothpick, they have almost the same API. https://github.com/stephanenicolas/toothpick – Snicolas Dec 09 '16 at 06:19
3

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.

Wilfred Springer
  • 10,869
  • 4
  • 55
  • 69
  • Spring ME depends on code generation. Have you found any resources for integrating it with the build process. For example it cannot interfere with my ability to Run As... Android App from within eclipse. And the ant build process would need to be modified to support it as well. – Mark Renouf Jun 26 '11 at 13:04
  • Hi Mark, I am not entirely sure what you're saying. We used Spring IDE extensions to support development (completion, validation, etc.). I can see that if you're using Ant, things might get a little bit more complicated. Let me know if you need help. We could talk about it. (wilfred dot springer at gmail dot com) – Wilfred Springer Jun 28 '11 at 11:22
  • Trying to find it now to give it a try and I'm not coming up with much. Is it still being actively developed? – Mark Renouf Jul 12 '11 at 02:00
  • Quite honestly, it's not, but I know of a few guys using it in their products. On the plus side: if you feel like taking some ownership, I'd be happy to accept you as a fellow committer. – Wilfred Springer Jul 21 '11 at 11:26
1

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

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
0

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.

brian.hgt
  • 28
  • 5
  • 1
    spring for android did not support ioc for android, just provide A Rest Client and Auth support for accessing secure APIs. – Jet Geng Apr 14 '13 at 01:21