Home » Android » java – What makes Guice unable to run on Android except as Guice-no-aop?

java – What makes Guice unable to run on Android except as Guice-no-aop?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I see on Guice’s download page a module called guice-no-aop, whose intention is clearly marketing towards Android developers. Several online searches pulled back libraries like RoboGuice that look like they do similar AOP-based IoC, and several articles even give great code examples.

But my question is: Why won’t Guice run on an Android app without these special libraries? I was expecting to find something on Guice’s site/wiki, but to my surprise, couldn’t find a single reason.

Anybody know?

Edit
Ancillary to this question is a broader one:

  • What other Java frameworks won’t run on Androids?!?! (What’s the rule of thumb?)
How to&Answers:

This page lists a few standard packages that are not supported. Anything that relies on those packages would likely not work…

Not supported These packages, normally a part of the Java 2 Platform Standard Edition, are not supported by Android.

  • java.applet
  • java.awt
  • java.beans
  • java.lang.management
  • java.rmi
  • javax.accessibility
  • javax.activity
  • javax.imageio
  • javax.management
  • javax.naming
  • javax.print
  • javax.rmi
  • javax.security.auth.kerberos
  • javax.security.auth.spi
  • javax.security.sasl
  • javax.swing
  • javax.transaction javax.xml (except javax.xml.parsers)
  • org.ietf.*
  • org.omg.*
  • org.w3c.dom.* (sub-packages)

Also as has already been pointed out AOP that relies on byte code weaving at runtime will not work (not all do, eg. Spring AOP).

Answer:

AOP is going to do byte code weaving at runtime. The Dalvik machine on Androids don’t run straight JVM byte code. They run a translated version of it. I used and liked the Android version of Guice called roboguice. http://code.google.com/p/roboguice/ It doesn’t do any AOP and your activities inherit from a RoboGuice activity called RoboActivity which does the actual injection based on the life cycle of the activity at the time.

P.S. Most mocking frameworks do byte code generation as well and won’t work or won’t work fully.

Answer:

Have a look at the comparison table here. It’s for Guice 2.0 and 1.0, but it should still apply to Guice 3 as well.

The main functional difference between Guice with AOP and without is method interceptors. Based on the annotation you use on a method, an what you’ve bound in guice to handle the annotation, guice will generate code at runtime to do what you’ve intended. It’s the runtime code generation that Guice can’t do on Android, as there’s no API (yet) for generating dalvik bytecode on the fly.