17

What would be the best way to implement Aspect-oriented programming (AOP) in an Android application?

Would it be efficient with the mobile battery?

Macarse
  • 91,829
  • 44
  • 175
  • 230
Javi
  • 19,387
  • 30
  • 102
  • 135
  • 2
    How does AOP relate to the battery? While AOP may be technically implemented as injecting code in methods, surely the effect on the battery would be the same as if said code was run some other way? – bzlm Sep 21 '10 at 10:11
  • I guess it's just down to efficiency. If AOP is a more efficient way of implementing your code it'll be easier on the battery. However this would be a very hard thing to measure, and the effect probably very slight. – fredley Sep 21 '10 at 10:14
  • Fernando Cejas has written a blog post about AOP in Android. Basically, you can use two approaches: 1. Using AspectJ 2. Using Dynamic Proxy. https://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/ – aldok May 02 '18 at 09:06

1 Answers1

17

It depends in how it is implemented.

For instance, AspectJ's compile-time weaving would work on android but runtime weaving do not. Android does not support bytecode generation.

Check the Guice wiki:

Behind the scenes, method interception is implemented by generating bytecode at runtime. Guice dynamically creates a subclass that applies interceptors by overriding methods. If you are on a platform that doesn't support bytecode generation (such as Android), you should use Guice without AOP support.

Macarse
  • 91,829
  • 44
  • 175
  • 230