Detailed explanation of using AspectJ in Android

What is AOP

AOP is the abbreviation of aspect oriented programming, that is, "aspect oriented programming". It is different from OOP that we usually come into contact with. OOP, that is, "Object-Oriented Programming", advocates modularization and objectification of functions, while AOP's idea is different. It advocates unified treatment of the same kind of problems. Of course, in the actual programming process, we can't simply install AOP or OOP to program. Many times, You may mix a variety of programming ideas, and you don't have to worry about which idea to use. Taking the advantages of a hundred schools is the right way.

So what's the use of AOP? Generally speaking, it is mainly used in scenarios where you don't want to invade the original code. For example, the SDK needs to insert some code into the host without invasion, such as log embedding, performance monitoring, dynamic permission control, and even code debugging.

AspectJ

AspectJ is actually a practice of AOP programming ideas. Of course, in addition to AspectJ, there are many other AOP implementations, such as asmdex, but at present, AspectJ is still the best and most convenient.

Using AspectJ in Android projects

Configuring AspectJ in Android is particularly troublesome. I tried to configure AspectJ directly in Android studio. I tossed for a long time without success and gave up. Fortunately, there is an open source on GitHub sdkhttps://github.com/HujiangTechnology/gradle_ plugin_ android_ Aspectjx, can be used directly, very convenient.

Access description

Add dependencies in the build.gradle file in the root directory of the project. The modified file is as follows

Then add AspectJ dependencies to the build. Gradle file of the project or library

compile 'org.aspectj:aspectjrt:1.8.9'

At the same time, the aspectjx module is added to the file

apply plugin: 'android-aspectjx'

Aspectjx will traverse all. Class files and dependent third-party libraries after the project is compiled by default to find the cut points that meet the weaving conditions. In order to improve the compilation efficiency, filter conditions can be added to specify whether to traverse some libraries or not.

Includejarfilter and excludejarfilter can support groupid filtering, artifactid filtering, or path dependent matching filtering

use

Create an aspecttest class,

The code of mainactivity is as follows

The log is as follows

log

After building, an aspected. Jar file will be generated

screenshot

Decompile the jar and you will find that it inserts a few lines of code into oncreate of mainactivity.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>