Android and Dependency Injection

24,869

Solution 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.

Solution 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.

Solution 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

Share:
24,869
binarydreams
Author by

binarydreams

Updated on December 10, 2020

Comments

  • binarydreams
    binarydreams over 3 years

    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?

  • binarydreams
    binarydreams over 14 years
    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.
  • emmby
    emmby over 14 years
    By the way Richard, RoboGuice now does injection in two steps. Once during onCreate for most injections, and once during setContentView for View injections.
  • Josh
    Josh about 13 years
    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.
  • Wilfred Springer
    Wilfred Springer almost 13 years
    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)
  • Josh
    Josh almost 13 years
    Trying to find it now to give it a try and I'm not coming up with much. Is it still being actively developed?
  • Wilfred Springer
    Wilfred Springer almost 13 years
    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.
  • Jet Geng
    Jet Geng about 11 years
    spring for android did not support ioc for android, just provide A Rest Client and Auth support for accessing secure APIs.
  • Snicolas
    Snicolas over 7 years
    RoboGuice, is now deprecated, you should give a try at Toothpick, they have almost the same API. github.com/stephanenicolas/toothpick