1. What is AOP(Faceted Programming)

  • OOP(object oriented) When we need to complete a task, we usually want to encapsulate some operations into a class, all variables and operations are encapsulated in a class, so this class is our object, we want to implement a particular function, first also want to implement in this object

Disadvantages such as we want to implement some functions that are not commonly used, we need to implement these functions in the required objects one by one, and we have to constantly maintain these functions, once more we will be very tired. For example, in Android, some key statistics, life cycle statistics, specific statistics are relatively trivial things, to use object-oriented thought to achieve are not very perfect, which requires to achieve one by one, is very trivial

  • AOP(aspect oriented) when I use is to focus on the specific method and function pointcut, do not need to know and do not care about what class or what object, we only focus on the implementation of the function, the specific object is who, do not care

2. Implementation of AOP

In a broad sense, AOP techniques can be any technology or framework that enables code weaving, where changes to code are ultimately reflected in bytecode. These techniques can also be called bytecode enhancement.

  • AOP focuses on method function points without knowing in advance who the object is, of course, the program is running to get the object is running
  • To take an object and execute it knowing the function points of the method, Java’s dynamic proxy is required
  • Dynamic proxies can change the logic of an object without modifying it

Java dynamic Proxy: There are two important classes or interfaces, one is InvocationHandler(Interface) and the other is Proxy(Class). We could write our own code to define our own dynamic Proxy to implement AOP, but it is too cumbersome, so we need a third-party tool

Use a static proxy example to quickly understand proxies

public interface HelloInterface {
    void sayHello();
}
public class Hello implements HelloInterface{
    @Override
    public void sayHello() {
        System.out.println("Hello zhanghao!");
    }
}
public class HelloProxy implements HelloInterface{
    private HelloInterface helloInterface = new Hello();
    @Override
    public void sayHello() {
        System.out.println("Before invoke sayHello" );
        helloInterface.sayHello();
        System.out.println("After invoke sayHello"); HelloProxy = new HelloProxy(); helloProxy.sayHello(); Result: Before invoke sayHello Hello Zhanghao! After invoke sayHelloCopy the code

3. Comparison of ways to implement AOP

  • First, from the perspective of weaving timing, can be divided into source stage, class stage, DEX stage, runtime weaving
    • For the first three, the source phase, the class phase, and dex weaving, we collectively call them static weaving because they all happen before the class is loaded into the virtual machine
    • Changes that occur at run time are collectively referred to as dynamic weaving
Woven into the timing Technical framework
Static weave APT, AspectJ, ASM, Javassit
Dynamic weave Java dynamic proxy, Cglib, Javassit
  • Static weaving occurs at the compiler, so it has little impact on runtime efficiency;
  • Dynamic weaving occurs at run time, bytecode can be written directly to memory, and classes can be loaded through reflection, so it is less efficient but more flexible.

Dynamic weaving is only possible if the class has not yet been loaded. You cannot load an already loaded class with modifications. This is a ClassLoader limitation. The virtual machine allows two classes with the same class name to be loaded by different classLoaders, which are considered to be two different classes at runtime. Therefore, do not assign values to each other, or ClassCastException will be thrown.

Java dynamic proxy, Cglib only creates a new proxy class rather than directly modifying the bytecode of the original class. Javassit can modify the bytecode of the original class.

In fact, the use of reflection or hook technology can also achieve code behavior change, but because this kind of technology did not really change the original bytecode, so not to talk about the scope, such as Xposed, dexposed.

AOP contrast selection

First Header

Hook the timing

Application scenarios in Android advantages disadvantages
Dexposed Run-time dynamic hooks Slide fluency monitoring, event execution monitoring, hot repair A variety of methods that can dynamically monitor and communicate with the system. Mobile phone 5.0 or higher is not supported
Xposed Run-time dynamic hooks With Dexposed A variety of methods that can dynamically monitor and communicate with the system. Mobile phone 5.0 or higher is not supported. Root is required
Java Proxy Run-time dynamic hooks Hook and system communication interface such as plug-in SDK Java native API, no compatibility issues You can only hook classes that have interfaces
AspactJ Modify the code at compile time Count the execution time of methods and inject logic before and after methods Sprint’s open source AOP framework is powerful. There are many notes. Basically all compile-time injection methods are included The 118K JAR needs to be introduced
ASM Modify the code at compile time Same as above Bytecode manipulation library You need to write your own annotations and compile your own scripts. Bytecode insertion is laborious to write
Javassit Modify the code at compile time Same as above Java reflection – based bytecode manipulation class library. Compared with ASM, it is simple to write Compared with ASM, class modification takes longer time
  • The mainstream usesAspectJ
    • powerful
    • Support code injection at compile time and load time
    • Easy to use
  • defects
    • Kotlin is not officially supported. Most of our projects are written by Kotlin

However, Hujiang has opened its own optimized AspectJ with Kotlin support and 2.8K likes

gradle_plugin_android_aspectjx

  • Gradle_plugin_android_aspectjx was originally developed
  1. The current open source library has not found that can be applied to the Android platform of the better AOP framework or tools, although Xposed, dexposed is very powerful, but based on the serious fragmentation of the status quo, compatibility problem is always an insurmountable mountain.
  2. Other AspectJ-related add-ons and frameworks don’t support AAR or JAR cuts, and Kotlin, who is so popular in the Android community, can’t help it.

4. Introduce AOP advantages and disadvantages

  • advantage
    • Can achieve no trace burying point
    • AOP is inherently action-oriented, so the same type of operations can be added to any object quickly and without incursion
    • Completely decoupled, the extracted operations are no longer associated with the object
  • disadvantage
    • As shown in the table above, the jar packages introduced are relatively large
    • Will the use of hujiang open source AOP tools continue to be stable

Good explanation

  1. Android AspectJ,
    • Rounding out the AspectJ
  2. Android AOP summary
    • AOP contrast selection
  3. Talk about Android AOP technology scheme
    • AOP contrast selection
    • Java8 is not supported
  4. In-depth understanding of Android AOP
    • A great god,
  5. Android Aspectj from the beginning to the field
    • give a like

5. Final conclusion

It is not recommended

  • The ultimate goal of AOP is to detach from objects and extract function points
  • This withdrawal is not a pain point for our APP yet
  • But in order to adapt AOP to our APP, we need to make some compromises, as follows
  1. Native AspectJ was introduced smoothly, but Kotlin was not supported, and our App was written almost entirely in Kotlin
  2. Support for AOP in AspectJ is currently only found in AspectJX of Hujiang Technology
  3. Although AspectJx of Hujiang Technology supports Kotlin, it seems that there is only one maintenance staff. Several problems occurred during my introduction and have been mentioned many times in the issue, but the author did not pay attention to them, which inevitably makes people doubt whether the maintainer’s enthusiasm for this project will affect our later use