Method of Android studio configuring AspectJ environment to implement AOP

Yesterday, I saw a live video of Android configuring AspectJ to realize AOP, and tried to configure it myself. It may be because of the problem of my own Android studio environment and encountered many pits (in fact, I don't understand much about gradle), but after all, it's configured. Let's share it.

I tried two methods, but there is no change in build.gradle under the project. Just look at the code:

Build.gradle (under the project)

Look at a lot, mainly the following lines of configuration. Others are used in my own project. Just configure them according to my own needs.

In fact, these lines can be configured in the build.gradle of the app, but because there are already buildscript {} and allprojects {repositories {}} in the build.gradle of the project, they are configured here.

Then there are two configuration methods:

First kind

If there is only one main module app, configure the build.gradle of the app:

These are the main things of this gradle:

The following pile is related with commands at the end of compilation. I don't understand the specific ones. Just add them.

Second

AspectJ is required for multiple modules, especially in the case of component development. It is impossible to configure each module, so it is necessary to create a new AspectJ module as the library of the project.

Build.gradle under app needs to be modified:

take

Delete and replace with

However, the above sentence will be generated automatically when you add a module dependency.

The build.gradle configuration of the new library is as follows:

Note: the following pile is slightly different from the gradle in app. One is module and the other is library. The things in gradle are different.

The two configuration methods are basically like this. I've just learned a little about the usage methods. Record the usage of simple calculation performance

Custom annotation class:

@Target annotation target, indicating where the annotation is used. Here is the method method@ Retention retention policy, which indicates the annotation invocation time. Here, runtime runtime

Section class

@Aspect specifies the aspect class@ Pointcut entry point@ Around is a kind of advice, which means inserting code before and after the pointcut, as well as @ before and @ after; Pointcut syntax, execution, means to insert code before and after executing the internal code of the method according to advice, and call means to insert code before and after calling the method according to advice

Page call

@Ifirstannotation calls annotation, () is internally the value of value written in ifirstannotation. After removing value (), remove () here

Note: if context is available in the methodbehavioraspect class, it can be directly converted to context by using joinpoint. Gettarget() type. Here, because the project uses databinding, some values obtained by gettarget() cannot be forcibly converted to context, so the context obtained by myapplication is used here

This is just a personal preliminary attempt. Of course, there are still a lot of contents to learn. I just read several articles about AOP written by someone on CSDN, which are very detailed. Give one of the addresses and see for yourself: http://www.jb51.net/article/110560.htm

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>