9、androidstudio中运用aop

这篇文章写得很好:https://blog.csdn.net/xwh_1230/article/details/78225258
可以在这里看一些语法:
http://www.eclipse.org/aspectj/docs.php

9、androidstudio中运用aop_第1张图片
image.png

点击进入: http://www.eclipse.org/aspectj/doc/released/runtime-api/index.html
9、androidstudio中运用aop_第2张图片
image.png

官网地址:https://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/
里面有描述怎么新建module的方式引入aspectj
也可以去github下载demo

9、androidstudio中运用aop_第3张图片
image.png

配置app的gradle和配置aop Library的gradle的方式在可以直接复制demo中的

运行会报错:

Error:(59, 1) Execution failed for task ':authcheck:compileDebugJavaWithJavac'.
> No such property: project for class: com.android.build.gradle.LibraryPlugin

修改Library这个module的gradle文件,修改为如图


9、androidstudio中运用aop_第4张图片
image.png

有个地方需要说明:

private static final String POINTCUT_METHOD =
      "execution(@org.android10.gintonic.annotation.DebugTrace * *(..))";

@org.android10.gintonic.annotation.DebugTrace 这个意思是说在声明了DebugTrace这个注解的方法上才会作为切入点,如果不写的话,所有的方法都会作为切入点
举例:


9、androidstudio中运用aop_第5张图片
image.png

AuthorityTrace.java

package com.example.authcheck.authority;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD })
public @interface AuthorityTrace {}

 private static final String POINTCUT_METHOD = "execution(* onClick(..))";

这个的意思是说所有的onClick方法都会作为切入点,如果只想要有AuthorityTrace注解的方法才作为切入点,那么修改为

private static final String POINTCUT_METHOD =
      "execution(@com.example.authcheck.authority.AuthorityTrace * onClick(..))";
    

你可能感兴趣的:(9、androidstudio中运用aop)