concept
Compilation stubs are changes that modify existing code or generate new code during code compilation. Compiling pile-in techniques can be divided into two types in terms of the timing of code changes: Java files: You step in at the beginning of the compilation to dynamically generate the Java file, and then the compiler compiles the generated Java file into class files, such as ButterKnife and Dagger. The corresponding technology is APT (Annotation Process Tools)
Bytecode file
: Intervenes after the class file is generated and directly modifies the bytecode of the class file to modify the code. Common bytecode programming frameworks areAspectJ
,Javassist
,ASM
Etc.
What is bytecode?
Java bytecode is an instruction format executed by the Java Virtual Machine. Java source files pass through the Java compiler to obtain Java bytecode (.class) files. Java bytecode (.class) files can be thought of as executable files for the Java virtual Machine. These bytecode (.class) files have enough metadata to resolve all the elements in the class: class names, methods, attributes, and Java bytecode (instructions)
How do I view bytecode
Taking AndroidStudio as an example, all idea-based ides should be the same. Go to Settings -> Tools and add External Tools. Enter Name and the following three elements and save them
$JDKPath$\bin\javap
-c -verbose $FileClass$
$OutputPath$
Remember before you generate bytecodebuild/rebuild
Take the project, or it will fail
A quick analysis of the bytecode composition
- Kotlin How to view files
Android bytecode programming
In Android, the compilation process is performed by gradle Task. Gradle1.5 provides transform-API to process code before it is converted into a. Class file and then packaged into a dex file, so we can customize our own transform. In the gradle appcompileDebugJavaWithJavac task will go we custom the transform.
Usage scenarios
Code generation
. In addition to the Dagger and ButterKnife annotation generation frameworks, Protocol Buffers and database ORM frameworks also generate code during compilation. Code generation isolates complex internal implementations, making development easier and more efficient. It also reduces manual duplication and error probability.Code to monitor
. In addition to network monitoring and power consumption monitoring, we can use compilation pile technology to achieve a variety of performance monitoring. Why not implement the monitoring functionality directly in the source code? First of all, we don’t necessarily have the source code for the third-party SDK. Second, some call points may be very scattered. For example, if you want to monitor all new Thread() calls in your code, it’s not easy to do it through the source code.Code changes
. For example, if some third-party SDK library has no source code, we can add a try catch to a crash function inside it, or replace its image library, etc. We can also implement a non-trace burial point through code modification.The code analysis
. In the last installment, I covered continuous integration, where custom code checks can be implemented using a compiler pile-in technique. Examples include checking for new Thread() calls in your code, checking for some sensitive permission usage in your code, and so on. In fact, Findbugs and other third-party code inspection tools also use the compile pile technique.
Insert pile practice
Javassist+ Gradle Transform + Annotation method time statistics