使用AspectJ

集成步骤:

  1、AS配置Aspectj环境

  2、配置使用ajc编译

  4、定义注解

  5、Aspect

  6、使用

  7、Example

AS配置Aspectj环境。Aspect目前最新版本为 1.8.10,AS中需要集成aspectjtool及aspectjrt并配置ajc编译,具体如下:

build.gradle(project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {repositories {jcenter()}dependencies {classpath "com.android.tools.build:gradle:${GRADLE_VERSION}"classpath "org.aspectj:aspectjtools:${ASPECT_VERSION}"// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files}
}allprojects {repositories {jcenter()}
}task clean(type: Delete) {delete rootProject.buildDir
}

build.gradle(app)

dependencies {compile "org.aspectj:aspectjrt:${ASPECT_VERSION}"
}android.libraryVariants.all { variant ->LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)JavaCompile javaCompile = variant.javaCompilejavaCompile.doLast {String[] args = ["-showWeaveInfo","-1.5","-inpath", javaCompile.destinationDir.toString(),"-aspectpath", javaCompile.classpath.asPath,"-d", javaCompile.destinationDir.toString(),"-classpath", javaCompile.classpath.asPath,"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]def log = project.loggerlog.error("aspectj", "----------------------aspect ajc args-------------------" + Arrays.toString(args))MessageHandler handler = new MessageHandler(true);new Main().run(args, handler)for (IMessage message : handler.getMessages(null, true)) {switch (message.getKind()) {case IMessage.ABORT:case IMessage.ERROR:case IMessage.FAIL:log.error message.message, message.thrownbreak;case IMessage.WARNING:case IMessage.INFO:log.info message.message, message.thrownbreak;case IMessage.DEBUG:log.debug message.message, message.thrownbreak;}}}
}

  

定义注解

package com.xiaosw.aspectj.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** <p><br/>ClassName : {@link UserBehaviour}* <br/>Description : 用户行为统计* <br/>* <br/>Author : xiaosw<xiaosw0802@163.com>* <br/>Create date : 2017-03-21 11:11:16</p>*/@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface UserBehaviour {String value() default "";}

  

Aspect

package com.xiaosw.aspectj.aspacet;import android.util.Log;import com.xiaosw.aspectj.annotation.UserBehaviour;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;import java.lang.reflect.Method;/*** <p><br/>ClassName : {@link UserBehaviourAspacet}* <br/>Description :* <br/>* <br/>Author : xiaosw<xiaosw0802@163.com>* <br/>Create date : 2017-03-21 11:11:17</p>*/@Aspect
public class UserBehaviourAspacet {/** @see UserBehaviourAspacet#getClass().getSimpleName() */private static final String TAG = "UserBehaviourAspacet";
   // 任意类任意方法任意参数使用UserBehaivour注解private static final String METHOD_INPUTCUT = "execution(@com.xiaosw.aspectj.annotation.UserBehaviour * *(..))";   // 任意类任意构造方法使用UserBehaivour注解private static final String CONSTRUCTOR_INPUTCUT = "execution(@com.xiaosw.aspectj.annotation.UserBehaviour *.new(..))";
   // 普通方法切点@Pointcut(METHOD_INPUTCUT)public void methodAnnotatedWithUserBehaviour() {}
   // 构造方法切点@Pointcut(CONSTRUCTOR_INPUTCUT)private void constructorAnnotatedWithUserBehaviour() {}/*** 同步方法不作为切点*/@Pointcut("execution(!synchronized * *(..))")private void noSynchronized() {}//    @Before("methodAnnotatedWithUserBehaviour() || constructorAnnotatedWithUserBehaviour()")
//    public void callBefore() {
//        Log.e(TAG, "callBefore()");
//    }
//
//    @After("methodAnnotatedWithUserBehaviour() || constructorAnnotatedWithUserBehaviour()")
//    public void callAfter() {
//        Log.e(TAG, "callAfter()");
//    }   // Advice@Around("noSynchronized() && (methodAnnotatedWithUserBehaviour() || constructorAnnotatedWithUserBehaviour())")public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {Object result = null;Log.e(TAG, "weaveJoinPoint: ");MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();Method method = methodSignature.getMethod();if (method.isAnnotationPresent(UserBehaviour.class)) {UserBehaviour userBehaviour = method.getAnnotation(UserBehaviour.class);Log.e(TAG, "weaveJoinPoint: parsms = " + userBehaviour.value());result = joinPoint.proceed();} else {Log.e(TAG, "weaveJoinPoint: method not instaceof UserBehaviour.class");}return result;}}

  

使用

@UserBehaviour("call testAop()")
private void testAop() {Log.e(TAG, "call -----------------> testAop()");
}

  

转载于:https://www.cnblogs.com/xiaosw/p/6594835.html

Android使用Aspectj(AOP)相关推荐

  1. Spring AOP / AspectJ AOP 的区别?

    Spring AOP / AspectJ AOP 的区别? Spring AOP属于运行时增强,而AspectJ是编译时增强. Spring AOP基于代理(Proxying),而AspectJ基于字 ...

  2. AspectJ AOP的使用(@Before、@PointCut、@Around等)

    AspectJ AOP的使用@Before.@PointCut.@Around等 AOP概念 使用AspectJ面向切面编程 详细说明 @Aspect @Pointcut.execution 各种通知 ...

  3. Spring之—— AspectJ AOP 完整示例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/49744721 这里我们用一个完整的例子演示spring aspectj aop的使 ...

  4. Android中的AOP编程之AspectJ实战实现数据埋点

    文章背景 最近在给某某银行做项目的时,涉及到了数据埋点,性能监控等问题,那我们起先想到的有两种方案,方案之一就是借助第三方,比如友盟.Bugly等,由于项目是部署在银行的网络框架之内的,所以该方案不可 ...

  5. android studio aspectj使用,androidStudio中使用 AspectJ

    关于 AOP 的详细介绍请参考 深入理解Android之AOP 本文主要介绍 怎么在 AndroidStudio 中使用 AspectJ 1 . 在android studio 中新建一个项目 MyA ...

  6. android studio aspectj 配置,Android studio配置AspectJ

    一.首先说说使用AspectJ的意义 使用切点插入,减少冗余代码,但是因为是编译时插入,所以运行时会有性能损耗,但是总的来说微乎其微 二.运行原理 这是我原来的代码 @BehaviorTrace(&q ...

  7. Spring AOP and AspectJ AOP 有什么区别

    AOP实现的关键在于 代理模式,AOP代理主要分为静态代理和动态代理.静态代理的代表为AspectJ:动态代理则以Spring AOP为代表. (1)AspectJ是静态代理的增强,所谓静态代理,就是 ...

  8. android 无法添加依赖关系,Android无法添加aop库依赖

    gradle版本4.4 在app的build.gradle中添加如下两个依赖 debugImplementation 'com.didichuxing.doraemonkit:doraemonkit: ...

  9. b aspectJ AOP简单介绍(概念模型)

    文章目录 概述 动态切点模型 pointscuts advice 暴露pointcuts的context Inter-type declaratios aspects 参考资料 概述 下来会以这个方法 ...

最新文章

  1. 力扣(LeetCode)刷题,简单题(第19期)
  2. 梯度下降法求多元线性回归及Java实现
  3. 002_XMLHttpRequest对象
  4. 高通 8x12 添加 TP和按键
  5. MySQL 笔记6 -- 函数与事务
  6. 第四范式推出业界首个基于持久内存、支持毫秒级恢复的万亿维线上预估系统...
  7. 工业4.0的小小思考
  8. 巡回沙龙_美浮特全国巡回沙龙第一期结束撒花!
  9. 标准exception类层次图
  10. 华为云一站式AI开发平台ModelArts获2019全球智博会金奖
  11. BZOJ 1070: [SCOI2007]修车(费用流)
  12. 考研高等数学张宇30讲笔记——第七讲 零点问题与微分不等式
  13. 阿里云语音合成1.0版
  14. Ameya:蔡司激光共聚焦显微镜的优势特点及应用领域
  15. 【笔记】马克思主义哲学(二)-- 唯物论
  16. 【ES6学习】对象的解构赋值
  17. 图像分类halcon
  18. wait()、notify()、notifyAll()使用详解
  19. 好看简洁的网站跳转页面
  20. 折腾StatusNet(原laconica),搭建个人微博平台

热门文章

  1. 解决PyCharm调试查看变量时一直显示collecting data并报错Timeout waiting for response且看不到任何内容
  2. PDE9 wave equation: general solution
  3. 3D数学之快速傅立叶变换(Fast Fourier Transform-FFT)
  4. 机器学习、数据挖掘、计算机视觉等领域经典书籍推荐
  5. Matlab绘图设置总结
  6. pytorch CNN
  7. Java微信运动步数排序设计_微信小程序仿微信运动步数排行-交互
  8. 【最新】2021年自然语言处理 (NLP) 算法学习路线!
  9. 注意力机制Attention Model(mechanism) 的 套路
  10. 行车环境实时语义分割与深度估计