APT and AOP in Android those things

Business background

apt

Eventbus is an excellent eventbus framework. In design mode, it is similar to the observer mode, but an upgraded version of the observer mode, and the sending and receiving mode is cut, eliminating many of the most annoying memory leaks. The technology used by EventBus is APT. If you are using observer mode in your project, you can use EventBus to improve it.

aop

Imagine a business scenario where the business counts the time it takes to start an activity, namely, oncreate, onStart, and other lifecycle methods. A primitive way to do this is to add repetitive code to each activity lifecycle, such as:

@Override protected void onCreate(Bundle savedInstanceState) { long start=System.currentTimeMillis(); super.onCreate(savedInstanceState); / / 1setContentView(R.layout.activity_main); long spend=System.currentTimeMillis()-start; / / 2}Copy the code

Think of an activity so many life cycles, an Android project so many activities, is not very cumbersome. This is where AOP comes in. Why not use Apt, Google’s parent, because one of the biggest limitations of APT is that you can’t add or delete code to the current class, you can only create a new one. The short summary is that AOP is a more powerful code generation framework than APT.

Introduction to the

  • APT (Annotation Processing Tool) is an Annotation Processing Tool used to scan and process annotations in the compiler. Using annotations as a bridge, the corresponding Java code is generated through predefined code rules
  • AOP:AOP is the abbreviation of Aspect Oriented Programming, meaning: Aspect Oriented Programming. So what is represented here is actually an idea, and the technology to realize this idea includes :ASPECTJ,ASM,JAVASIST, mainly the comparison of these three factions
Ability to APT AOP
Create a new class y y
Modifying the current class n y
Implementation technology AutoService ASPECTJ,ASM,JAVASIST
annotations y y
SDK code modification n y

Android Packaging Process

Android Official Documentation

In the previous figure, the compilers stage is actually made up of many transforms

Apt, aop is in stage of compilers apt the role in class — >. Dex stage, aop role in class — >. Dex or jar – – >. Dex phase. You can further see that AOP is more powerful

Use tips

Apt, AOP although can reduce a lot of repeated code, code structure optimization, but also see that there is no source code, is not conducive to the investigation of problems, so users must restrain, not these two technologies are very black technology like to use, otherwise it is to bury the risk of their own projects. Students who write these APT and AOP codes must leave enough debugging information to facilitate follow-up problem tracking

Fine speak apt

In fact, there are many excellent APT cases on the network, I don’t want to write this demo repeatedly. Post a recommended apt case

Fine about aop

Asm implementation

ASM can be used to dynamically generate classes or enhance the functionality of existing classes. ASM can generate binary class files directly or dynamically change the behavior of classes before they are loaded into the Java Virtual machine. The underlying bytecode framework operates at the assembly instruction level of the underlying JVM. This requires ASM users to have some knowledge of the class organization structure and JVM assembly instructions.

General idea explanation

  • Register a Transform to receive input from compile-time code, mainly class and JAR packages
  • Use Java’s Jarfile interface to parse the JAR package and get the class file inside the JAR package
 JarFile jarFile = new JarFile(inFile)
  Enumeration<JarEntry> entries = jarFile.entries()
Copy the code
  • Use the ASM excuse to parse the jar classes and change the code operations you want. We recommend using the ASM5 version. Gradle has this SDK by default.
ClassReader reader = new ClassReader(entryBytes)
ClassWriter classWriter = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS);
ClassVisitor visitor = new MyClassVisitor(classWriter)
Copy the code
  • Copy the file to the next input location regardless of whether the jar package or class is changed, as shown in the following example:
 File outFile = transformInvocation.outputProvider.getContentLocation(
                                    jarInput.file.absolutePath,
                                    jarInput.contentTypes,
                                    jarInput.scopes,
                                    Format.JAR)
FileUtils.copyFile(jarInput.file, outFile)
Copy the code

There is a very good explanation online, and there are examples of source code: cases

Aspectj implementation

Aspectj is a faceted approach to implementing AOP through annotations, and one of the easiest to get started with

Javassit implementation

case

conclusion

Above pay attention to are cited by others to explain the results, after the output of personal demo article, more detailed explanation of AOP this piece, of course, the estimate may not be as good as I now cite the case, thorough.

I hope my words can bring you help, but also welcome to pay attention to the personal public number, all my articles back to the first hand information priority published here. Personal public account: small black house by the river